blob: 1b40f458f4aa9ad0af346a06ed5272afb1bcc513 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_ssl_session *session = ssl->session_negotiate;
1006 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
1007 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
Jaeden Amero2de07f12019-06-05 13:32:08 +01001011#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1012 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001013 transform->encrypt_then_mac = session->encrypt_then_mac;
1014#endif
1015 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001016
1017 ciphersuite_info = handshake->ciphersuite_info;
1018 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001019 if( cipher_info == NULL )
1020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001022 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001024 }
1025
Hanno Beckere694c3e2017-12-27 21:34:08 +00001026 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001027 if( md_info == NULL )
1028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001030 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001032 }
1033
Hanno Beckera0e20d02019-05-15 14:03:01 +01001034#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001035 /* Copy own and peer's CID if the use of the CID
1036 * extension has been negotiated. */
1037 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1038 {
1039 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001040
Hanno Becker05154c32019-05-03 15:23:51 +01001041 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001042 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001043 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001044 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001045
1046 transform->out_cid_len = ssl->handshake->peer_cid_len;
1047 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1048 ssl->handshake->peer_cid_len );
1049 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1050 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001051 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001052#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001053
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 * Swap the client and server random values.
1056 */
Paul Bakker48916f92012-09-16 19:57:18 +00001057 memcpy( tmp, handshake->randbytes, 64 );
1058 memcpy( handshake->randbytes, tmp + 32, 32 );
1059 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001060 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001061
1062 /*
1063 * SSLv3:
1064 * key block =
1065 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1066 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1067 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1068 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1069 * ...
1070 *
1071 * TLSv1:
1072 * key block = PRF( master, "key expansion", randbytes )
1073 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001074 ret = handshake->tls_prf( session->master, 48, "key expansion",
1075 handshake->randbytes, 64, keyblk, 256 );
1076 if( ret != 0 )
1077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001079 return( ret );
1080 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1083 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1084 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1085 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1086 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 /*
1089 * Determine the appropriate key, IV and MAC length.
1090 */
Paul Bakker68884e32013-01-07 18:20:04 +01001091
Hanno Becker88aaf652017-12-27 08:17:40 +00001092 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001093
Hanno Becker8031d062018-01-03 15:32:31 +00001094#if defined(MBEDTLS_GCM_C) || \
1095 defined(MBEDTLS_CCM_C) || \
1096 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001098 cipher_info->mode == MBEDTLS_MODE_CCM ||
1099 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001101 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001102
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001103 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001104 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001105 transform->taglen =
1106 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001107
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001108 /* All modes haves 96-bit IVs;
1109 * GCM and CCM has 4 implicit and 8 explicit bytes
1110 * ChachaPoly has all 12 bytes implicit
1111 */
Paul Bakker68884e32013-01-07 18:20:04 +01001112 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001113 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1114 transform->fixed_ivlen = 12;
1115 else
1116 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001117
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001118 /* Minimum length of encrypted record */
1119 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001120 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001121 }
1122 else
Hanno Becker8031d062018-01-03 15:32:31 +00001123#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1124#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1125 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1126 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001127 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001128 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1130 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001133 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001134 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001136 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001137 mac_key_len = mbedtls_md_get_size( md_info );
1138 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001141 /*
1142 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1143 * (rfc 6066 page 13 or rfc 2104 section 4),
1144 * so we only need to adjust the length here.
1145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001149
1150#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1151 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001152 * HMAC implementation which also truncates the key
1153 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001154 mac_key_len = transform->maclen;
1155#endif
1156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001158
1159 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001160 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001162 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001164 transform->minlen = transform->maclen;
1165 else
Paul Bakker68884e32013-01-07 18:20:04 +01001166 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001167 /*
1168 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001169 * 1. if EtM is in use: one block plus MAC
1170 * otherwise: * first multiple of blocklen greater than maclen
1171 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1174 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001175 {
1176 transform->minlen = transform->maclen
1177 + cipher_info->block_size;
1178 }
1179 else
1180#endif
1181 {
1182 transform->minlen = transform->maclen
1183 + cipher_info->block_size
1184 - transform->maclen % cipher_info->block_size;
1185 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1188 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1189 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001190 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001191 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1194 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1195 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001196 {
1197 transform->minlen += transform->ivlen;
1198 }
1199 else
1200#endif
1201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001203 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1204 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001205 }
Paul Bakker68884e32013-01-07 18:20:04 +01001206 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 }
Hanno Becker8031d062018-01-03 15:32:31 +00001208 else
1209#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1210 {
1211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1212 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
Hanno Becker88aaf652017-12-27 08:17:40 +00001215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1216 (unsigned) keylen,
1217 (unsigned) transform->minlen,
1218 (unsigned) transform->ivlen,
1219 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220
1221 /*
1222 * Finally setup the cipher contexts, IVs and MAC secrets.
1223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001225 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001227 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001228 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
Paul Bakker68884e32013-01-07 18:20:04 +01001230 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001231 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233 /*
1234 * This is not used in TLS v1.1.
1235 */
Paul Bakker48916f92012-09-16 19:57:18 +00001236 iv_copy_len = ( transform->fixed_ivlen ) ?
1237 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001238 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1239 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001240 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 }
1242 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#endif /* MBEDTLS_SSL_CLI_C */
1244#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001245 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001247 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001248 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Hanno Becker81c7b182017-11-09 18:39:33 +00001250 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001251 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001253 /*
1254 * This is not used in TLS v1.1.
1255 */
Paul Bakker48916f92012-09-16 19:57:18 +00001256 iv_copy_len = ( transform->fixed_ivlen ) ?
1257 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001258 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1259 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001260 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001262 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001266 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1267 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001268 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Hanno Beckerd56ed242018-01-03 15:32:51 +00001270#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#if defined(MBEDTLS_SSL_PROTO_SSL3)
1272 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001273 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001274 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001277 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1278 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001279 }
1280
Hanno Becker81c7b182017-11-09 18:39:33 +00001281 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1282 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001283 }
1284 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1286#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1287 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1288 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001289 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001290 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1291 For AEAD-based ciphersuites, there is nothing to do here. */
1292 if( mac_key_len != 0 )
1293 {
1294 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1295 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1296 }
Paul Bakker68884e32013-01-07 18:20:04 +01001297 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001298 else
1299#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001302 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1303 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001304 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001305#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1308 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001309 {
1310 int ret = 0;
1311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001313
Hanno Becker88aaf652017-12-27 08:17:40 +00001314 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001315 transform->iv_enc, transform->iv_dec,
1316 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001317 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001318 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001321 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1322 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001323 }
1324 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001325#else
1326 ((void) mac_dec);
1327 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001329
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001330#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1331 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001332 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001333 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1334 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001335 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001336 iv_copy_len );
1337 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001338
1339 if( ssl->conf->f_export_keys_ext != NULL )
1340 {
1341 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1342 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001343 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001344 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001345 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001346 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001347 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001348 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001349#endif
1350
Hanno Beckerf704bef2018-11-16 15:21:18 +00001351#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001352
1353 /* Only use PSA-based ciphers for TLS-1.2.
1354 * That's relevant at least for TLS-1.0, where
1355 * we assume that mbedtls_cipher_crypt() updates
1356 * the structure field for the IV, which the PSA-based
1357 * implementation currently doesn't. */
1358#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1359 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001360 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001361 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001362 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001363 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1364 {
1365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001366 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001367 }
1368
1369 if( ret == 0 )
1370 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001371 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001372 psa_fallthrough = 0;
1373 }
1374 else
1375 {
1376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1377 psa_fallthrough = 1;
1378 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001379 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001380 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001381 psa_fallthrough = 1;
1382#else
1383 psa_fallthrough = 1;
1384#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001385
Hanno Beckercb1cc802018-11-17 22:27:38 +00001386 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001387#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001388 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001389 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001392 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001393 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001394
Hanno Beckerf704bef2018-11-16 15:21:18 +00001395#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001396 /* Only use PSA-based ciphers for TLS-1.2.
1397 * That's relevant at least for TLS-1.0, where
1398 * we assume that mbedtls_cipher_crypt() updates
1399 * the structure field for the IV, which the PSA-based
1400 * implementation currently doesn't. */
1401#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1402 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001403 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001404 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001405 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001406 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1407 {
1408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001409 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001410 }
1411
1412 if( ret == 0 )
1413 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001414 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001415 psa_fallthrough = 0;
1416 }
1417 else
1418 {
1419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1420 psa_fallthrough = 1;
1421 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001422 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001423 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001424 psa_fallthrough = 1;
1425#else
1426 psa_fallthrough = 1;
1427#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001428
Hanno Beckercb1cc802018-11-17 22:27:38 +00001429 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001430#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001431 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001432 cipher_info ) ) != 0 )
1433 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001435 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001436 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001439 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001443 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001444 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001447 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001451 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001452 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_CIPHER_MODE_CBC)
1455 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1458 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001461 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001462 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1465 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001468 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001469 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001475 // Initialize compression
1476 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001478 {
Paul Bakker16770332013-10-11 09:59:44 +02001479 if( ssl->compress_buf == NULL )
1480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001482 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001483 if( ssl->compress_buf == NULL )
1484 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001486 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001487 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1488 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001489 }
1490 }
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001493
Paul Bakker48916f92012-09-16 19:57:18 +00001494 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1495 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001496
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001497 if( deflateInit( &transform->ctx_deflate,
1498 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001499 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001502 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1503 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001504 }
1505 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001509end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001510 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1511 mbedtls_platform_zeroize( handshake->randbytes,
1512 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001513 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514}
1515
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001516/*
1517 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
1518 *
1519 * Inputs:
1520 * - SSL/TLS minor version
1521 * - hash associated with the ciphersuite (only used by TLS 1.2)
1522 *
1523 * Ouputs:
1524 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1525 */
1526static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1527 int minor_ver,
1528 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001529{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001530#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1531 (void) hash;
1532#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001533
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001534#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001535 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001536 {
1537 handshake->tls_prf = ssl3_prf;
1538 handshake->calc_verify = ssl_calc_verify_ssl;
1539 handshake->calc_finished = ssl_calc_finished_ssl;
1540 }
1541 else
1542#endif
1543#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001544 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001545 {
1546 handshake->tls_prf = tls1_prf;
1547 handshake->calc_verify = ssl_calc_verify_tls;
1548 handshake->calc_finished = ssl_calc_finished_tls;
1549 }
1550 else
1551#endif
1552#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1553#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001554 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1555 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001556 {
1557 handshake->tls_prf = tls_prf_sha384;
1558 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1559 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1560 }
1561 else
1562#endif
1563#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001564 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001565 {
1566 handshake->tls_prf = tls_prf_sha256;
1567 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1568 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1569 }
1570 else
1571#endif
1572#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1573 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1575 }
1576
1577 return( 0 );
1578}
1579
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001580/*
1581 * Compute master secret
1582 */
1583static int ssl_compute_master( mbedtls_ssl_context *ssl )
1584{
1585 int ret;
1586 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1587 mbedtls_ssl_session *session = ssl->session_negotiate;
1588 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
1589
1590 /* cf. RFC 5246, Section 8.1:
1591 * "The master secret is always exactly 48 bytes in length." */
1592 size_t const master_secret_len = 48;
1593
1594#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1595 unsigned char session_hash[48];
1596#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1597
1598 /*
1599 * SSLv3:
1600 * master =
1601 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1602 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1603 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
1604 *
1605 * TLSv1+:
1606 * master = PRF( premaster, "master secret", randbytes )[0..47]
1607 */
1608 if( handshake->resume != 0 )
1609 {
1610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1611 }
1612 else
1613 {
1614 /* The label for the KDF used for key expansion.
1615 * This is either "master secret" or "extended master secret"
1616 * depending on whether the Extended Master Secret extension
1617 * is used. */
1618 char const *lbl = "master secret";
1619
1620 /* The salt for the KDF used for key expansion.
1621 * - If the Extended Master Secret extension is not used,
1622 * this is ClientHello.Random + ServerHello.Random
1623 * (see Sect. 8.1 in RFC 5246).
1624 * - If the Extended Master Secret extension is used,
1625 * this is the transcript of the handshake so far.
1626 * (see Sect. 4 in RFC 7627). */
1627 unsigned char const *salt = handshake->randbytes;
1628 size_t salt_len = 64;
1629
1630#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1631 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
1632 {
1633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
1634
1635 lbl = "extended master secret";
1636 salt = session_hash;
1637 ssl->handshake->calc_verify( ssl, session_hash );
1638#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1639 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1640 {
1641#if defined(MBEDTLS_SHA512_C)
1642 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1643 {
1644 salt_len = 48;
1645 }
1646 else
1647#endif /* MBEDTLS_SHA512_C */
1648 salt_len = 32;
1649 }
1650 else
1651#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1652 salt_len = 36;
1653
1654 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
1655 }
1656#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1657
1658#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1659 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1660 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1661 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1662 ssl_use_opaque_psk( ssl ) == 1 )
1663 {
1664 /* Perform PSK-to-MS expansion in a single step. */
1665 psa_status_t status;
1666 psa_algorithm_t alg;
1667 psa_key_handle_t psk;
1668 psa_key_derivation_operation_t derivation =
1669 PSA_KEY_DERIVATION_OPERATION_INIT;
1670
1671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1672
1673 psk = ssl->conf->psk_opaque;
1674 if( ssl->handshake->psk_opaque != 0 )
1675 psk = ssl->handshake->psk_opaque;
1676
1677 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1678 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1679 else
1680 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1681
1682 status = psa_key_derivation( &derivation, psk, alg,
1683 salt, salt_len,
1684 (unsigned char const *) lbl,
1685 (size_t) strlen( lbl ),
1686 master_secret_len );
1687 if( status != PSA_SUCCESS )
1688 {
1689 psa_key_derivation_abort( &derivation );
1690 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1691 }
1692
1693 status = psa_key_derivation_output_bytes( &derivation,
1694 session->master,
1695 master_secret_len );
1696 if( status != PSA_SUCCESS )
1697 {
1698 psa_key_derivation_abort( &derivation );
1699 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1700 }
1701
1702 status = psa_key_derivation_abort( &derivation );
1703 if( status != PSA_SUCCESS )
1704 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1705 }
1706 else
1707#endif
1708 {
1709 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1710 lbl, salt, salt_len,
1711 session->master,
1712 master_secret_len );
1713 if( ret != 0 )
1714 {
1715 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1716 return( ret );
1717 }
1718
1719 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1720 handshake->premaster,
1721 handshake->pmslen );
1722
1723 mbedtls_platform_zeroize( handshake->premaster,
1724 sizeof(handshake->premaster) );
1725 }
1726 }
1727
1728 return( 0 );
1729}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001730
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001731int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1732{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001733 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001734 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1735 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001736
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001737 ret = ssl_set_handshake_prfs( ssl->handshake,
1738 ssl->minor_ver,
1739 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001740 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001741 {
1742 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001743 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001744 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001745
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001746 ret = ssl_compute_master( ssl );
1747 if( ret != 0 )
1748 {
1749 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1750 return( ret );
1751 }
1752
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001753 return( ssl_populate_transform( ssl ) );
1754}
1755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756#if defined(MBEDTLS_SSL_PROTO_SSL3)
1757void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001758{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001759 mbedtls_md5_context md5;
1760 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 unsigned char pad_1[48];
1762 unsigned char pad_2[48];
1763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001766 mbedtls_md5_init( &md5 );
1767 mbedtls_sha1_init( &sha1 );
1768
1769 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1770 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
Paul Bakker380da532012-04-18 16:10:25 +00001772 memset( pad_1, 0x36, 48 );
1773 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001775 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1776 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1777 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001779 mbedtls_md5_starts_ret( &md5 );
1780 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1781 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1782 mbedtls_md5_update_ret( &md5, hash, 16 );
1783 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001785 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1786 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1787 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001788
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001789 mbedtls_sha1_starts_ret( &sha1 );
1790 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1791 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1792 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1793 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001797
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001798 mbedtls_md5_free( &md5 );
1799 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001800
Paul Bakker380da532012-04-18 16:10:25 +00001801 return;
1802}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1806void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001807{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001808 mbedtls_md5_context md5;
1809 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001812
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001813 mbedtls_md5_init( &md5 );
1814 mbedtls_sha1_init( &sha1 );
1815
1816 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1817 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001818
Andrzej Kurekeb342242019-01-29 09:14:33 -05001819 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001820 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001824
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001825 mbedtls_md5_free( &md5 );
1826 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001827
Paul Bakker380da532012-04-18 16:10:25 +00001828 return;
1829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1833#if defined(MBEDTLS_SHA256_C)
1834void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001835{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001836#if defined(MBEDTLS_USE_PSA_CRYPTO)
1837 size_t hash_size;
1838 psa_status_t status;
1839 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1840
1841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1842 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1843 if( status != PSA_SUCCESS )
1844 {
1845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1846 return;
1847 }
1848
1849 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1850 if( status != PSA_SUCCESS )
1851 {
1852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1853 return;
1854 }
1855 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1857#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001858 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001859
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001860 mbedtls_sha256_init( &sha256 );
1861
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001863
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001864 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001865 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001869
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001870 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001871#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001872 return;
1873}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876#if defined(MBEDTLS_SHA512_C)
1877void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001878{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001879#if defined(MBEDTLS_USE_PSA_CRYPTO)
1880 size_t hash_size;
1881 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001882 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001883
1884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001885 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001886 if( status != PSA_SUCCESS )
1887 {
1888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1889 return;
1890 }
1891
Andrzej Kurek972fba52019-01-30 03:29:12 -05001892 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001893 if( status != PSA_SUCCESS )
1894 {
1895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1896 return;
1897 }
1898 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1900#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001901 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001902
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001903 mbedtls_sha512_init( &sha512 );
1904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001906
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001907 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001908 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001912
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001913 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001914#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 return;
1916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917#endif /* MBEDTLS_SHA512_C */
1918#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1921int 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 +02001922{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001923 unsigned char *p = ssl->handshake->premaster;
1924 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001925 const unsigned char *psk = ssl->conf->psk;
1926 size_t psk_len = ssl->conf->psk_len;
1927
1928 /* If the psk callback was called, use its result */
1929 if( ssl->handshake->psk != NULL )
1930 {
1931 psk = ssl->handshake->psk;
1932 psk_len = ssl->handshake->psk_len;
1933 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001934
1935 /*
1936 * PMS = struct {
1937 * opaque other_secret<0..2^16-1>;
1938 * opaque psk<0..2^16-1>;
1939 * };
1940 * with "other_secret" depending on the particular key exchange
1941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1943 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001944 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001945 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001947
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001948 *(p++) = (unsigned char)( psk_len >> 8 );
1949 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001950
1951 if( end < p || (size_t)( end - p ) < psk_len )
1952 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1953
1954 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001955 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001956 }
1957 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1959#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1960 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001961 {
1962 /*
1963 * other_secret already set by the ClientKeyExchange message,
1964 * and is 48 bytes long
1965 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001966 if( end - p < 2 )
1967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1968
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001969 *p++ = 0;
1970 *p++ = 48;
1971 p += 48;
1972 }
1973 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1975#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1976 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001977 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001978 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001979 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001980
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001981 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001983 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001984 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001987 return( ret );
1988 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001989 *(p++) = (unsigned char)( len >> 8 );
1990 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001991 p += len;
1992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001994 }
1995 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1997#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1998 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001999 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002000 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002001 size_t zlen;
2002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002004 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002005 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002008 return( ret );
2009 }
2010
2011 *(p++) = (unsigned char)( zlen >> 8 );
2012 *(p++) = (unsigned char)( zlen );
2013 p += zlen;
2014
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002015 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2016 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002017 }
2018 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002023 }
2024
2025 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002026 if( end - p < 2 )
2027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002028
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002029 *(p++) = (unsigned char)( psk_len >> 8 );
2030 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002031
2032 if( end < p || (size_t)( end - p ) < psk_len )
2033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2034
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002035 memcpy( p, psk, psk_len );
2036 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002037
2038 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2039
2040 return( 0 );
2041}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002045/*
2046 * SSLv3.0 MAC functions
2047 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002048#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002049static void ssl_mac( mbedtls_md_context_t *md_ctx,
2050 const unsigned char *secret,
2051 const unsigned char *buf, size_t len,
2052 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002053 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002054{
2055 unsigned char header[11];
2056 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002057 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2059 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002060
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002061 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002063 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002064 else
Paul Bakker68884e32013-01-07 18:20:04 +01002065 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002066
2067 memcpy( header, ctr, 8 );
2068 header[ 8] = (unsigned char) type;
2069 header[ 9] = (unsigned char)( len >> 8 );
2070 header[10] = (unsigned char)( len );
2071
Paul Bakker68884e32013-01-07 18:20:04 +01002072 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 mbedtls_md_starts( md_ctx );
2074 mbedtls_md_update( md_ctx, secret, md_size );
2075 mbedtls_md_update( md_ctx, padding, padlen );
2076 mbedtls_md_update( md_ctx, header, 11 );
2077 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002078 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002079
Paul Bakker68884e32013-01-07 18:20:04 +01002080 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 mbedtls_md_starts( md_ctx );
2082 mbedtls_md_update( md_ctx, secret, md_size );
2083 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002084 mbedtls_md_update( md_ctx, out, md_size );
2085 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002086}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002088
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002089/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002090 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002091#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002092 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2093 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2094 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2095/* This function makes sure every byte in the memory region is accessed
2096 * (in ascending addresses order) */
2097static void ssl_read_memory( unsigned char *p, size_t len )
2098{
2099 unsigned char acc = 0;
2100 volatile unsigned char force;
2101
2102 for( ; len != 0; p++, len-- )
2103 acc ^= *p;
2104
2105 force = acc;
2106 (void) force;
2107}
2108#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2109
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002110/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002112 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002113
Hanno Beckera0e20d02019-05-15 14:03:01 +01002114#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002115/* This functions transforms a DTLS plaintext fragment and a record content
2116 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002117 *
2118 * struct {
2119 * opaque content[DTLSPlaintext.length];
2120 * ContentType real_type;
2121 * uint8 zeros[length_of_padding];
2122 * } DTLSInnerPlaintext;
2123 *
2124 * Input:
2125 * - `content`: The beginning of the buffer holding the
2126 * plaintext to be wrapped.
2127 * - `*content_size`: The length of the plaintext in Bytes.
2128 * - `max_len`: The number of Bytes available starting from
2129 * `content`. This must be `>= *content_size`.
2130 * - `rec_type`: The desired record content type.
2131 *
2132 * Output:
2133 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2134 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2135 *
2136 * Returns:
2137 * - `0` on success.
2138 * - A negative error code if `max_len` didn't offer enough space
2139 * for the expansion.
2140 */
2141static int ssl_cid_build_inner_plaintext( unsigned char *content,
2142 size_t *content_size,
2143 size_t remaining,
2144 uint8_t rec_type )
2145{
2146 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002147 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2148 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2149 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002150
2151 /* Write real content type */
2152 if( remaining == 0 )
2153 return( -1 );
2154 content[ len ] = rec_type;
2155 len++;
2156 remaining--;
2157
2158 if( remaining < pad )
2159 return( -1 );
2160 memset( content + len, 0, pad );
2161 len += pad;
2162 remaining -= pad;
2163
2164 *content_size = len;
2165 return( 0 );
2166}
2167
Hanno Becker07dc97d2019-05-20 15:08:01 +01002168/* This function parses a DTLSInnerPlaintext structure.
2169 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002170static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2171 size_t *content_size,
2172 uint8_t *rec_type )
2173{
2174 size_t remaining = *content_size;
2175
2176 /* Determine length of padding by skipping zeroes from the back. */
2177 do
2178 {
2179 if( remaining == 0 )
2180 return( -1 );
2181 remaining--;
2182 } while( content[ remaining ] == 0 );
2183
2184 *content_size = remaining;
2185 *rec_type = content[ remaining ];
2186
2187 return( 0 );
2188}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002189#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002190
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002191/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002192 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002193static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002194 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002195 mbedtls_record *rec )
2196{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002197 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002198 *
2199 * additional_data = seq_num + TLSCompressed.type +
2200 * TLSCompressed.version + TLSCompressed.length;
2201 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002202 * For the CID extension, this is extended as follows
2203 * (quoting draft-ietf-tls-dtls-connection-id-05,
2204 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002205 *
2206 * additional_data = seq_num + DTLSPlaintext.type +
2207 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002208 * cid +
2209 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002210 * length_of_DTLSInnerPlaintext;
2211 */
2212
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002213 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2214 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002215 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002216
Hanno Beckera0e20d02019-05-15 14:03:01 +01002217#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002218 if( rec->cid_len != 0 )
2219 {
2220 memcpy( add_data + 11, rec->cid, rec->cid_len );
2221 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2222 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2223 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2224 *add_data_len = 13 + 1 + rec->cid_len;
2225 }
2226 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002227#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002228 {
2229 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2230 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2231 *add_data_len = 13;
2232 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233}
2234
Hanno Beckera18d1322018-01-03 14:27:32 +00002235int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2236 mbedtls_ssl_transform *transform,
2237 mbedtls_record *rec,
2238 int (*f_rng)(void *, unsigned char *, size_t),
2239 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002242 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002243 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002244 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002245 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002246 size_t post_avail;
2247
2248 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002249#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002250 ((void) ssl);
2251#endif
2252
2253 /* The PRNG is used for dynamic IV generation that's used
2254 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2255#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2256 ( defined(MBEDTLS_AES_C) || \
2257 defined(MBEDTLS_ARIA_C) || \
2258 defined(MBEDTLS_CAMELLIA_C) ) && \
2259 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2260 ((void) f_rng);
2261 ((void) p_rng);
2262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002266 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002267 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2270 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002271 if( rec == NULL
2272 || rec->buf == NULL
2273 || rec->buf_len < rec->data_offset
2274 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002275#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002276 || rec->cid_len != 0
2277#endif
2278 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002279 {
2280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002282 }
2283
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002284 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002285 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002287 data, rec->data_len );
2288
2289 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2290
2291 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2292 {
2293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2294 (unsigned) rec->data_len,
2295 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2297 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002298
Hanno Beckera0e20d02019-05-15 14:03:01 +01002299#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002300 /*
2301 * Add CID information
2302 */
2303 rec->cid_len = transform->out_cid_len;
2304 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2305 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002306
2307 if( rec->cid_len != 0 )
2308 {
2309 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002310 * Wrap plaintext into DTLSInnerPlaintext structure.
2311 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002312 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002313 * Note that this changes `rec->data_len`, and hence
2314 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002315 */
2316 if( ssl_cid_build_inner_plaintext( data,
2317 &rec->data_len,
2318 post_avail,
2319 rec->type ) != 0 )
2320 {
2321 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2322 }
2323
2324 rec->type = MBEDTLS_SSL_MSG_CID;
2325 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002326#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002327
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002328 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2329
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002331 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 */
Hanno Becker52344c22018-01-03 15:24:20 +00002333#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 if( mode == MBEDTLS_MODE_STREAM ||
2335 ( mode == MBEDTLS_MODE_CBC
2336#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002337 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002338#endif
2339 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002340 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002341 if( post_avail < transform->maclen )
2342 {
2343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2344 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2345 }
2346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002348 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002349 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002350 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002351 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2352 data, rec->data_len, rec->ctr, rec->type, mac );
2353 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002354 }
2355 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002356#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2358 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002359 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002360 {
Hanno Becker992b6872017-11-09 18:57:39 +00002361 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2362
Hanno Beckercab87e62019-04-29 13:52:53 +01002363 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002364
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002365 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002366 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002367 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2368 data, rec->data_len );
2369 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2370 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2371
2372 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002373 }
2374 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002375#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2378 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002379 }
2380
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002381 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2382 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002383
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002384 rec->data_len += transform->maclen;
2385 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002386 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002387 }
Hanno Becker52344c22018-01-03 15:24:20 +00002388#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002390 /*
2391 * Encrypt
2392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2394 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002396 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002397 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002399 "including %d bytes of padding",
2400 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002402 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2403 transform->iv_enc, transform->ivlen,
2404 data, rec->data_len,
2405 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002408 return( ret );
2409 }
2410
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002411 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 }
Paul Bakker68884e32013-01-07 18:20:04 +01002417 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002419
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002420#if defined(MBEDTLS_GCM_C) || \
2421 defined(MBEDTLS_CCM_C) || \
2422 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002424 mode == MBEDTLS_MODE_CCM ||
2425 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002426 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002427 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002428 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002429 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002430
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002431 /* Check that there's space for both the authentication tag
2432 * and the explicit IV before and after the record content. */
2433 if( post_avail < transform->taglen ||
2434 rec->data_offset < explicit_iv_len )
2435 {
2436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2437 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2438 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002439
Paul Bakker68884e32013-01-07 18:20:04 +01002440 /*
2441 * Generate IV
2442 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002443 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2444 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002445 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002446 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002447 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2448 explicit_iv_len );
2449 /* Prefix record content with explicit IV. */
2450 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002451 }
2452 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2453 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002454 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002455 unsigned char i;
2456
2457 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2458
2459 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002460 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002461 }
2462 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002463 {
2464 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2466 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002467 }
2468
Hanno Beckercab87e62019-04-29 13:52:53 +01002469 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002470
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002471 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2472 iv, transform->ivlen );
2473 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002474 data - explicit_iv_len, explicit_iv_len );
2475 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002476 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002478 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002479 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002480
Paul Bakker68884e32013-01-07 18:20:04 +01002481 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002482 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002483 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002484
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002485 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002486 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002487 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002488 data, rec->data_len, /* source */
2489 data, &rec->data_len, /* destination */
2490 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002493 return( ret );
2494 }
2495
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002496 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2497 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002498
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002499 rec->data_len += transform->taglen + explicit_iv_len;
2500 rec->data_offset -= explicit_iv_len;
2501 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002502 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002503 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2506#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002507 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002510 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002511 size_t padlen, i;
2512 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002513
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002514 /* Currently we're always using minimal padding
2515 * (up to 255 bytes would be allowed). */
2516 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2517 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 padlen = 0;
2519
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002520 /* Check there's enough space in the buffer for the padding. */
2521 if( post_avail < padlen + 1 )
2522 {
2523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2524 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2525 }
2526
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002528 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002530 rec->data_len += padlen + 1;
2531 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002534 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002535 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2536 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002537 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002538 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002539 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002540 if( f_rng == NULL )
2541 {
2542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2544 }
2545
2546 if( rec->data_offset < transform->ivlen )
2547 {
2548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2549 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2550 }
2551
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002552 /*
2553 * Generate IV
2554 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002555 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002556 if( ret != 0 )
2557 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002558
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002559 memcpy( data - transform->ivlen, transform->iv_enc,
2560 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002561
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002562 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002566 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002567 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002568 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002570 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2571 transform->iv_enc,
2572 transform->ivlen,
2573 data, rec->data_len,
2574 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002577 return( ret );
2578 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002579
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002580 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2583 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002584 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002587 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002588 {
2589 /*
2590 * Save IV in SSL3 and TLS1
2591 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002592 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2593 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002595 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002596#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002597 {
2598 data -= transform->ivlen;
2599 rec->data_offset -= transform->ivlen;
2600 rec->data_len += transform->ivlen;
2601 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002604 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002605 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002606 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2607
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002608 /*
2609 * MAC(MAC_write_key, seq_num +
2610 * TLSCipherText.type +
2611 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002612 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002613 * IV + // except for TLS 1.0
2614 * ENC(content + padding + padding_length));
2615 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002616
2617 if( post_avail < transform->maclen)
2618 {
2619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2620 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2621 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002622
Hanno Beckercab87e62019-04-29 13:52:53 +01002623 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002626 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002627 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002628
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002629 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002630 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002631 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2632 data, rec->data_len );
2633 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2634 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002635
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002636 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002637
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002638 rec->data_len += transform->maclen;
2639 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002640 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002641 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002643 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002644 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002646 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2649 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002650 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002652 /* Make extra sure authentication was performed, exactly once */
2653 if( auth_done != 1 )
2654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2656 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002657 }
2658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
2661 return( 0 );
2662}
2663
Hanno Becker605949f2019-07-12 08:23:59 +01002664int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002665 mbedtls_ssl_transform *transform,
2666 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002667{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002668 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002670 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002671#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002672 size_t padlen = 0, correct = 1;
2673#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002674 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002675 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002676 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002677
Hanno Beckera18d1322018-01-03 14:27:32 +00002678#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002679 ((void) ssl);
2680#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002683 if( rec == NULL ||
2684 rec->buf == NULL ||
2685 rec->buf_len < rec->data_offset ||
2686 rec->buf_len - rec->data_offset < rec->data_len )
2687 {
2688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002690 }
2691
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002692 data = rec->buf + rec->data_offset;
2693 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694
Hanno Beckera0e20d02019-05-15 14:03:01 +01002695#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002696 /*
2697 * Match record's CID with incoming CID.
2698 */
Hanno Becker938489a2019-05-08 13:02:22 +01002699 if( rec->cid_len != transform->in_cid_len ||
2700 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2701 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002702 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002703 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002704#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2707 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002708 {
2709 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002710 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2711 transform->iv_dec,
2712 transform->ivlen,
2713 data, rec->data_len,
2714 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002717 return( ret );
2718 }
2719
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002720 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2723 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002724 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 }
Paul Bakker68884e32013-01-07 18:20:04 +01002726 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002728#if defined(MBEDTLS_GCM_C) || \
2729 defined(MBEDTLS_CCM_C) || \
2730 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002732 mode == MBEDTLS_MODE_CCM ||
2733 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002734 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002735 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002736 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002737
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002738 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002739 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002740 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002741
2742 /* Check that there's enough space for the explicit IV
2743 * (at the beginning of the record) and the MAC (at the
2744 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002745 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002748 "+ taglen (%d)", rec->data_len,
2749 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002751 }
Paul Bakker68884e32013-01-07 18:20:04 +01002752
Hanno Beckerd96a6522019-07-10 13:55:25 +01002753#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002754 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2755 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002756 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002757
Hanno Beckerd96a6522019-07-10 13:55:25 +01002758 /* Fixed */
2759 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2760 /* Explicit */
2761 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002762 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002763 else
2764#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2765#if defined(MBEDTLS_CHACHAPOLY_C)
2766 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002767 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002768 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002769 unsigned char i;
2770
2771 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2772
2773 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002774 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002775 }
2776 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002777#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002778 {
2779 /* Reminder if we ever add an AEAD mode with a different size */
2780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2782 }
2783
Hanno Beckerd96a6522019-07-10 13:55:25 +01002784 /* Group changes to data, data_len, and add_data, because
2785 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002786 data += explicit_iv_len;
2787 rec->data_offset += explicit_iv_len;
2788 rec->data_len -= explicit_iv_len + transform->taglen;
2789
Hanno Beckercab87e62019-04-29 13:52:53 +01002790 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002791 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002792 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002793
Hanno Beckerd96a6522019-07-10 13:55:25 +01002794 /* Because of the check above, we know that there are
2795 * explicit_iv_len Bytes preceeding data, and taglen
2796 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002797 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002798 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002799
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002800 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002801 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002802 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002803
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002804 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002805 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002806 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002807 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2808 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002809 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002810 data, rec->data_len,
2811 data, &olen,
2812 data + rec->data_len,
2813 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2818 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002819
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002820 return( ret );
2821 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002822 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002823
Hanno Beckerd96a6522019-07-10 13:55:25 +01002824 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002825 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002829 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002830 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002831 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2833#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002834 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002837 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002838
Paul Bakker5121ce52009-01-03 21:22:43 +00002839 /*
Paul Bakker45829992013-01-03 14:52:21 +01002840 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002843 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2844 {
2845 /* The ciphertext is prefixed with the CBC IV. */
2846 minlen += transform->ivlen;
2847 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002848#endif
Paul Bakker45829992013-01-03 14:52:21 +01002849
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002850 /* Size considerations:
2851 *
2852 * - The CBC cipher text must not be empty and hence
2853 * at least of size transform->ivlen.
2854 *
2855 * Together with the potential IV-prefix, this explains
2856 * the first of the two checks below.
2857 *
2858 * - The record must contain a MAC, either in plain or
2859 * encrypted, depending on whether Encrypt-then-MAC
2860 * is used or not.
2861 * - If it is, the message contains the IV-prefix,
2862 * the CBC ciphertext, and the MAC.
2863 * - If it is not, the padded plaintext, and hence
2864 * the CBC ciphertext, has at least length maclen + 1
2865 * because there is at least the padding length byte.
2866 *
2867 * As the CBC ciphertext is not empty, both cases give the
2868 * lower bound minlen + maclen + 1 on the record size, which
2869 * we test for in the second check below.
2870 */
2871 if( rec->data_len < minlen + transform->ivlen ||
2872 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002875 "+ 1 ) ( + expl IV )", rec->data_len,
2876 transform->ivlen,
2877 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002879 }
2880
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002881 /*
2882 * Authenticate before decrypt if enabled
2883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002885 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002886 {
Hanno Becker992b6872017-11-09 18:57:39 +00002887 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002890
Hanno Beckerd96a6522019-07-10 13:55:25 +01002891 /* Update data_len in tandem with add_data.
2892 *
2893 * The subtraction is safe because of the previous check
2894 * data_len >= minlen + maclen + 1.
2895 *
2896 * Afterwards, we know that data + data_len is followed by at
2897 * least maclen Bytes, which justifies the call to
2898 * mbedtls_ssl_safer_memcmp() below.
2899 *
2900 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002901 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002902 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002903
Hanno Beckerd96a6522019-07-10 13:55:25 +01002904 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002905 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2906 add_data_len );
2907 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2908 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002909 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2910 data, rec->data_len );
2911 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2912 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002913
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002914 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2915 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002916 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002917 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002918
Hanno Beckerd96a6522019-07-10 13:55:25 +01002919 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002920 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2921 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002925 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002926 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002927 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002929
2930 /*
2931 * Check length sanity
2932 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002933
2934 /* We know from above that data_len > minlen >= 0,
2935 * so the following check in particular implies that
2936 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002937 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002940 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002942 }
2943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002945 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002946 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002947 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002948 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002949 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002950 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002951 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002952
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002953 data += transform->ivlen;
2954 rec->data_offset += transform->ivlen;
2955 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002956 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002958
Hanno Beckerd96a6522019-07-10 13:55:25 +01002959 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2960
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002961 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2962 transform->iv_dec, transform->ivlen,
2963 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002966 return( ret );
2967 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002968
Hanno Beckerd96a6522019-07-10 13:55:25 +01002969 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002970 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002974 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002977 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002978 {
2979 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002980 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
2981 * records is equivalent to CBC decryption of the concatenation
2982 * of the records; in other words, IVs are maintained across
2983 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02002984 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002985 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2986 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002988#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002989
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002990 /* Safe since data_len >= minlen + maclen + 1, so after having
2991 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01002992 * data_len > 0 (because of data_len % ivlen == 0, it's actually
2993 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002994 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002995
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002996 if( auth_done == 1 )
2997 {
2998 correct *= ( rec->data_len >= padlen + 1 );
2999 padlen *= ( rec->data_len >= padlen + 1 );
3000 }
3001 else
Paul Bakker45829992013-01-03 14:52:21 +01003002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003004 if( rec->data_len < transform->maclen + padlen + 1 )
3005 {
3006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3007 rec->data_len,
3008 transform->maclen,
3009 padlen + 1 ) );
3010 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003011#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003012
3013 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3014 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003015 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003017 padlen++;
3018
3019 /* Regardless of the validity of the padding,
3020 * we have data_len >= padlen here. */
3021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003023 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003025 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027#if defined(MBEDTLS_SSL_DEBUG_ALL)
3028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003029 "should be no more than %d",
3030 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003031#endif
Paul Bakker45829992013-01-03 14:52:21 +01003032 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003033 }
3034 }
3035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3037#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3038 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003039 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003040 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003041 /* The padding check involves a series of up to 256
3042 * consecutive memory reads at the end of the record
3043 * plaintext buffer. In order to hide the length and
3044 * validity of the padding, always perform exactly
3045 * `min(256,plaintext_len)` reads (but take into account
3046 * only the last `padlen` bytes for the padding check). */
3047 size_t pad_count = 0;
3048 size_t real_count = 0;
3049 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003050
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003051 /* Index of first padding byte; it has been ensured above
3052 * that the subtraction is safe. */
3053 size_t const padding_idx = rec->data_len - padlen;
3054 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3055 size_t const start_idx = rec->data_len - num_checks;
3056 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003057
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003058 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003059 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003060 real_count |= ( idx >= padding_idx );
3061 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003062 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003063 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003066 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003068#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003069 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003070 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003071 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3073 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3076 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003077 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003078
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003079 /* If the padding was found to be invalid, padlen == 0
3080 * and the subtraction is safe. If the padding was found valid,
3081 * padlen hasn't been changed and the previous assertion
3082 * data_len >= padlen still holds. */
3083 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003085 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003087 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3090 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003091 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003093#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003095 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003096#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003097
3098 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003099 * Authenticate if not done yet.
3100 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 */
Hanno Becker52344c22018-01-03 15:24:20 +00003102#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003103 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003104 {
Hanno Becker992b6872017-11-09 18:57:39 +00003105 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003106
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003107 /* If the initial value of padlen was such that
3108 * data_len < maclen + padlen + 1, then padlen
3109 * got reset to 1, and the initial check
3110 * data_len >= minlen + maclen + 1
3111 * guarantees that at this point we still
3112 * have at least data_len >= maclen.
3113 *
3114 * If the initial value of padlen was such that
3115 * data_len >= maclen + padlen + 1, then we have
3116 * subtracted either padlen + 1 (if the padding was correct)
3117 * or 0 (if the padding was incorrect) since then,
3118 * hence data_len >= maclen in any case.
3119 */
3120 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003121 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003124 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003125 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003126 ssl_mac( &transform->md_ctx_dec,
3127 transform->mac_dec,
3128 data, rec->data_len,
3129 rec->ctr, rec->type,
3130 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003131 }
3132 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3134#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3135 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003136 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003137 {
3138 /*
3139 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003140 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003141 *
3142 * Known timing attacks:
3143 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3144 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003145 * To compensate for different timings for the MAC calculation
3146 * depending on how much padding was removed (which is determined
3147 * by padlen), process extra_run more blocks through the hash
3148 * function.
3149 *
3150 * The formula in the paper is
3151 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3152 * where L1 is the size of the header plus the decrypted message
3153 * plus CBC padding and L2 is the size of the header plus the
3154 * decrypted message. This is for an underlying hash function
3155 * with 64-byte blocks.
3156 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3157 * correctly. We round down instead of up, so -56 is the correct
3158 * value for our calculations instead of -55.
3159 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003160 * Repeat the formula rather than defining a block_size variable.
3161 * This avoids requiring division by a variable at runtime
3162 * (which would be marginally less efficient and would require
3163 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003164 */
3165 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003166 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003167
3168 /*
3169 * The next two sizes are the minimum and maximum values of
3170 * in_msglen over all padlen values.
3171 *
3172 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003173 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003174 *
3175 * Note that max_len + maclen is never more than the buffer
3176 * length, as we previously did in_msglen -= maclen too.
3177 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003178 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003179 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3180
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003181 memset( tmp, 0, sizeof( tmp ) );
3182
3183 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003184 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003185#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3186 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003187 case MBEDTLS_MD_MD5:
3188 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003189 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003190 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003191 extra_run =
3192 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3193 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003194 break;
3195#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003196#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003197 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003198 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003199 extra_run =
3200 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3201 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003202 break;
3203#endif
3204 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3207 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003208
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003209 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003210
Hanno Beckercab87e62019-04-29 13:52:53 +01003211 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3212 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003213 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3214 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003215 /* Make sure we access everything even when padlen > 0. This
3216 * makes the synchronisation requirements for just-in-time
3217 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003218 ssl_read_memory( data + rec->data_len, padlen );
3219 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003220
3221 /* Call mbedtls_md_process at least once due to cache attacks
3222 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003223 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003224 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003225
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003226 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003227
3228 /* Make sure we access all the memory that could contain the MAC,
3229 * before we check it in the next code block. This makes the
3230 * synchronisation requirements for just-in-time Prime+Probe
3231 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003232 ssl_read_memory( data + min_len,
3233 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003234 }
3235 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3237 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3240 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003241 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003242
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003243#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003244 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3245 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003246#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003247
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003248 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3249 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#if defined(MBEDTLS_SSL_DEBUG_ALL)
3252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003253#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003254 correct = 0;
3255 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003256 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003257 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003258
3259 /*
3260 * Finally check the correct flag
3261 */
3262 if( correct == 0 )
3263 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003264#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003265
3266 /* Make extra sure authentication was performed, exactly once */
3267 if( auth_done != 1 )
3268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003271 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003272
Hanno Beckera0e20d02019-05-15 14:03:01 +01003273#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003274 if( rec->cid_len != 0 )
3275 {
3276 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3277 &rec->type );
3278 if( ret != 0 )
3279 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3280 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003281#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003284
3285 return( 0 );
3286}
3287
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003288#undef MAC_NONE
3289#undef MAC_PLAINTEXT
3290#undef MAC_CIPHERTEXT
3291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003293/*
3294 * Compression/decompression functions
3295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003297{
3298 int ret;
3299 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003300 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003301 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003302 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003303
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
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003306 if( len_pre == 0 )
3307 return( 0 );
3308
Paul Bakker2770fbd2012-07-03 13:30:23 +00003309 memcpy( msg_pre, ssl->out_msg, len_pre );
3310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003312 ssl->out_msglen ) );
3313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003315 ssl->out_msg, ssl->out_msglen );
3316
Paul Bakker48916f92012-09-16 19:57:18 +00003317 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3318 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3319 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003320 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003321
Paul Bakker48916f92012-09-16 19:57:18 +00003322 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003323 if( ret != Z_OK )
3324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3326 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003327 }
3328
Angus Grattond8213d02016-05-25 20:56:48 +10003329 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003330 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003333 ssl->out_msglen ) );
3334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003336 ssl->out_msg, ssl->out_msglen );
3337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003339
3340 return( 0 );
3341}
3342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003344{
3345 int ret;
3346 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003347 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003348 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003349 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003352
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003353 if( len_pre == 0 )
3354 return( 0 );
3355
Paul Bakker2770fbd2012-07-03 13:30:23 +00003356 memcpy( msg_pre, ssl->in_msg, len_pre );
3357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003359 ssl->in_msglen ) );
3360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003362 ssl->in_msg, ssl->in_msglen );
3363
Paul Bakker48916f92012-09-16 19:57:18 +00003364 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3365 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3366 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003367 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003368 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003369
Paul Bakker48916f92012-09-16 19:57:18 +00003370 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003371 if( ret != Z_OK )
3372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3374 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003375 }
3376
Angus Grattond8213d02016-05-25 20:56:48 +10003377 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003378 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003381 ssl->in_msglen ) );
3382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003384 ssl->in_msg, ssl->in_msglen );
3385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003387
3388 return( 0 );
3389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3393static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395#if defined(MBEDTLS_SSL_PROTO_DTLS)
3396static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003397{
3398 /* If renegotiation is not enforced, retransmit until we would reach max
3399 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003400 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003401 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003402 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003403 unsigned char doublings = 1;
3404
3405 while( ratio != 0 )
3406 {
3407 ++doublings;
3408 ratio >>= 1;
3409 }
3410
3411 if( ++ssl->renego_records_seen > doublings )
3412 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003414 return( 0 );
3415 }
3416 }
3417
3418 return( ssl_write_hello_request( ssl ) );
3419}
3420#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003422
Paul Bakker5121ce52009-01-03 21:22:43 +00003423/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003424 * Fill the input message buffer by appending data to it.
3425 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003426 *
3427 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3428 * available (from this read and/or a previous one). Otherwise, an error code
3429 * is returned (possibly EOF or WANT_READ).
3430 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003431 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3432 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3433 * since we always read a whole datagram at once.
3434 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003435 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003436 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003439{
Paul Bakker23986e52011-04-24 08:57:21 +00003440 int ret;
3441 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003444
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003445 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003448 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003450 }
3451
Angus Grattond8213d02016-05-25 20:56:48 +10003452 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003456 }
3457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003459 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003460 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003461 uint32_t timeout;
3462
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003463 /* Just to be sure */
3464 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3465 {
3466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3467 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3468 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3469 }
3470
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003471 /*
3472 * The point is, we need to always read a full datagram at once, so we
3473 * sometimes read more then requested, and handle the additional data.
3474 * It could be the rest of the current record (while fetching the
3475 * header) and/or some other records in the same datagram.
3476 */
3477
3478 /*
3479 * Move to the next record in the already read datagram if applicable
3480 */
3481 if( ssl->next_record_offset != 0 )
3482 {
3483 if( ssl->in_left < ssl->next_record_offset )
3484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3486 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003487 }
3488
3489 ssl->in_left -= ssl->next_record_offset;
3490
3491 if( ssl->in_left != 0 )
3492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003494 ssl->next_record_offset ) );
3495 memmove( ssl->in_hdr,
3496 ssl->in_hdr + ssl->next_record_offset,
3497 ssl->in_left );
3498 }
3499
3500 ssl->next_record_offset = 0;
3501 }
3502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003504 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003505
3506 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003507 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003508 */
3509 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003512 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003513 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003514
3515 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003516 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003517 * are not at the beginning of a new record, the caller did something
3518 * wrong.
3519 */
3520 if( ssl->in_left != 0 )
3521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3523 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003524 }
3525
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003526 /*
3527 * Don't even try to read if time's out already.
3528 * This avoids by-passing the timer when repeatedly receiving messages
3529 * that will end up being dropped.
3530 */
3531 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003532 {
3533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003534 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003535 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003536 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003537 {
Angus Grattond8213d02016-05-25 20:56:48 +10003538 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003541 timeout = ssl->handshake->retransmit_timeout;
3542 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003543 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003546
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003547 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003548 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3549 timeout );
3550 else
3551 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003554
3555 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003557 }
3558
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003559 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003562 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003565 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003566 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003569 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003570 }
3571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003575 return( ret );
3576 }
3577
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003578 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003579 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003581 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003583 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003584 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003587 return( ret );
3588 }
3589
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003590 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003591 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003593 }
3594
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 if( ret < 0 )
3596 return( ret );
3597
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003598 ssl->in_left = ret;
3599 }
3600 else
3601#endif
3602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003604 ssl->in_left, nb_want ) );
3605
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003606 while( ssl->in_left < nb_want )
3607 {
3608 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003609
3610 if( ssl_check_timer( ssl ) != 0 )
3611 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3612 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003613 {
3614 if( ssl->f_recv_timeout != NULL )
3615 {
3616 ret = ssl->f_recv_timeout( ssl->p_bio,
3617 ssl->in_hdr + ssl->in_left, len,
3618 ssl->conf->read_timeout );
3619 }
3620 else
3621 {
3622 ret = ssl->f_recv( ssl->p_bio,
3623 ssl->in_hdr + ssl->in_left, len );
3624 }
3625 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003628 ssl->in_left, nb_want ) );
3629 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003630
3631 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003633
3634 if( ret < 0 )
3635 return( ret );
3636
mohammad160352aecb92018-03-28 23:41:40 -07003637 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003638 {
Darryl Green11999bb2018-03-13 15:22:58 +00003639 MBEDTLS_SSL_DEBUG_MSG( 1,
3640 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003641 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3643 }
3644
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003645 ssl->in_left += ret;
3646 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003647 }
3648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003650
3651 return( 0 );
3652}
3653
3654/*
3655 * Flush any data not yet written
3656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003658{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003659 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003660 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003663
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003664 if( ssl->f_send == NULL )
3665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003667 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003669 }
3670
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003671 /* Avoid incrementing counter if data is flushed */
3672 if( ssl->out_left == 0 )
3673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003675 return( 0 );
3676 }
3677
Paul Bakker5121ce52009-01-03 21:22:43 +00003678 while( ssl->out_left > 0 )
3679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003681 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003682
Hanno Becker2b1e3542018-08-06 11:19:13 +01003683 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003684 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003686 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
3688 if( ret <= 0 )
3689 return( ret );
3690
mohammad160352aecb92018-03-28 23:41:40 -07003691 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003692 {
Darryl Green11999bb2018-03-13 15:22:58 +00003693 MBEDTLS_SSL_DEBUG_MSG( 1,
3694 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003695 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3697 }
3698
Paul Bakker5121ce52009-01-03 21:22:43 +00003699 ssl->out_left -= ret;
3700 }
3701
Hanno Becker2b1e3542018-08-06 11:19:13 +01003702#if defined(MBEDTLS_SSL_PROTO_DTLS)
3703 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003704 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003705 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003706 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003707 else
3708#endif
3709 {
3710 ssl->out_hdr = ssl->out_buf + 8;
3711 }
3712 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
3716 return( 0 );
3717}
3718
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003719/*
3720 * Functions to handle the DTLS retransmission state machine
3721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003723/*
3724 * Append current handshake message to current outgoing flight
3725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003727{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3730 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3731 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003732
3733 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003734 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003735 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003738 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003739 }
3740
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003741 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003742 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003745 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003746 }
3747
3748 /* Copy current handshake message with headers */
3749 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3750 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003751 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003752 msg->next = NULL;
3753
3754 /* Append to the current flight */
3755 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003756 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003757 else
3758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003760 while( cur->next != NULL )
3761 cur = cur->next;
3762 cur->next = msg;
3763 }
3764
Hanno Becker3b235902018-08-06 09:54:53 +01003765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003766 return( 0 );
3767}
3768
3769/*
3770 * Free the current flight of handshake messages
3771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003773{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 mbedtls_ssl_flight_item *cur = flight;
3775 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003776
3777 while( cur != NULL )
3778 {
3779 next = cur->next;
3780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781 mbedtls_free( cur->p );
3782 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003783
3784 cur = next;
3785 }
3786}
3787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3789static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003790#endif
3791
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003792/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003793 * Swap transform_out and out_ctr with the alternative ones
3794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003796{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003798 unsigned char tmp_out_ctr[8];
3799
3800 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003803 return;
3804 }
3805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003807
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003808 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003809 tmp_transform = ssl->transform_out;
3810 ssl->transform_out = ssl->handshake->alt_transform_out;
3811 ssl->handshake->alt_transform_out = tmp_transform;
3812
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003813 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003814 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3815 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003816 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003817
3818 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003819 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3822 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3827 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003828 }
3829 }
3830#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003831}
3832
3833/*
3834 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003835 */
3836int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3837{
3838 int ret = 0;
3839
3840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3841
3842 ret = mbedtls_ssl_flight_transmit( ssl );
3843
3844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3845
3846 return( ret );
3847}
3848
3849/*
3850 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003851 *
3852 * Need to remember the current message in case flush_output returns
3853 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003854 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003855 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003856int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003857{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003858 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003862 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003864
3865 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003866 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003867 ssl_swap_epochs( ssl );
3868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003870 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003871
3872 while( ssl->handshake->cur_msg != NULL )
3873 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003874 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003875 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003876
Hanno Beckere1dcb032018-08-17 16:47:58 +01003877 int const is_finished =
3878 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3879 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3880
Hanno Becker04da1892018-08-14 13:22:10 +01003881 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3882 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3883
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003884 /* Swap epochs before sending Finished: we can't do it after
3885 * sending ChangeCipherSpec, in case write returns WANT_READ.
3886 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003887 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003888 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003890 ssl_swap_epochs( ssl );
3891 }
3892
Hanno Becker67bc7c32018-08-06 11:33:50 +01003893 ret = ssl_get_remaining_payload_in_datagram( ssl );
3894 if( ret < 0 )
3895 return( ret );
3896 max_frag_len = (size_t) ret;
3897
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003898 /* CCS is copied as is, while HS messages may need fragmentation */
3899 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3900 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003901 if( max_frag_len == 0 )
3902 {
3903 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3904 return( ret );
3905
3906 continue;
3907 }
3908
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003909 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003910 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003911 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003912
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003913 /* Update position inside current message */
3914 ssl->handshake->cur_msg_p += cur->len;
3915 }
3916 else
3917 {
3918 const unsigned char * const p = ssl->handshake->cur_msg_p;
3919 const size_t hs_len = cur->len - 12;
3920 const size_t frag_off = p - ( cur->p + 12 );
3921 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003922 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003923
Hanno Beckere1dcb032018-08-17 16:47:58 +01003924 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003925 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003926 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003927 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003928
Hanno Becker67bc7c32018-08-06 11:33:50 +01003929 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3930 return( ret );
3931
3932 continue;
3933 }
3934 max_hs_frag_len = max_frag_len - 12;
3935
3936 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3937 max_hs_frag_len : rem_len;
3938
3939 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003940 {
3941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003942 (unsigned) cur_hs_frag_len,
3943 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003944 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003945
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003946 /* Messages are stored with handshake headers as if not fragmented,
3947 * copy beginning of headers then fill fragmentation fields.
3948 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3949 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003950
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003951 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3952 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3953 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3954
Hanno Becker67bc7c32018-08-06 11:33:50 +01003955 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3956 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3957 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003958
3959 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3960
Hanno Becker3f7b9732018-08-28 09:53:25 +01003961 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003962 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3963 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003964 ssl->out_msgtype = cur->type;
3965
3966 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003967 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003968 }
3969
3970 /* If done with the current message move to the next one if any */
3971 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3972 {
3973 if( cur->next != NULL )
3974 {
3975 ssl->handshake->cur_msg = cur->next;
3976 ssl->handshake->cur_msg_p = cur->next->p + 12;
3977 }
3978 else
3979 {
3980 ssl->handshake->cur_msg = NULL;
3981 ssl->handshake->cur_msg_p = NULL;
3982 }
3983 }
3984
3985 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003986 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003989 return( ret );
3990 }
3991 }
3992
Hanno Becker67bc7c32018-08-06 11:33:50 +01003993 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3994 return( ret );
3995
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003996 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3998 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003999 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004002 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4003 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004004
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004006
4007 return( 0 );
4008}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004009
4010/*
4011 * To be called when the last message of an incoming flight is received.
4012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004014{
4015 /* We won't need to resend that one any more */
4016 ssl_flight_free( ssl->handshake->flight );
4017 ssl->handshake->flight = NULL;
4018 ssl->handshake->cur_msg = NULL;
4019
4020 /* The next incoming flight will start with this msg_seq */
4021 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4022
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004023 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004024 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004025
Hanno Becker0271f962018-08-16 13:23:47 +01004026 /* Clear future message buffering structure. */
4027 ssl_buffering_free( ssl );
4028
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004029 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004030 ssl_set_timer( ssl, 0 );
4031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004032 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4033 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004036 }
4037 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004039}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004040
4041/*
4042 * To be called when the last message of an outgoing flight is send.
4043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004045{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004046 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004047 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004049 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4050 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004053 }
4054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004056}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004058
Paul Bakker5121ce52009-01-03 21:22:43 +00004059/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004060 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004061 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004062
4063/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004064 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004065 *
4066 * - fill in handshake headers
4067 * - update handshake checksum
4068 * - DTLS: save message for resending
4069 * - then pass to the record layer
4070 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004071 * DTLS: except for HelloRequest, messages are only queued, and will only be
4072 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004073 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004074 * Inputs:
4075 * - ssl->out_msglen: 4 + actual handshake message len
4076 * (4 is the size of handshake headers for TLS)
4077 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4078 * - ssl->out_msg + 4: the handshake message body
4079 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004080 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004081 * - ssl->out_msglen: the length of the record contents
4082 * (including handshake headers but excluding record headers)
4083 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004084 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004085int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004086{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004087 int ret;
4088 const size_t hs_len = ssl->out_msglen - 4;
4089 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004090
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4092
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004093 /*
4094 * Sanity checks
4095 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004096 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004097 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4098 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004099 /* In SSLv3, the client might send a NoCertificate alert. */
4100#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4101 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4102 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4103 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4104#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4105 {
4106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4107 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4108 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004109 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004110
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004111 /* Whenever we send anything different from a
4112 * HelloRequest we should be in a handshake - double check. */
4113 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4114 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004115 ssl->handshake == NULL )
4116 {
4117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4118 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4119 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004122 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004123 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004125 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4127 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004128 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004129#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004130
Hanno Beckerb50a2532018-08-06 11:52:54 +01004131 /* Double-check that we did not exceed the bounds
4132 * of the outgoing record buffer.
4133 * This should never fail as the various message
4134 * writing functions must obey the bounds of the
4135 * outgoing record buffer, but better be safe.
4136 *
4137 * Note: We deliberately do not check for the MTU or MFL here.
4138 */
4139 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4140 {
4141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4142 "size %u, maximum %u",
4143 (unsigned) ssl->out_msglen,
4144 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4145 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4146 }
4147
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004148 /*
4149 * Fill handshake headers
4150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004151 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004152 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004153 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4154 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4155 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004156
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004157 /*
4158 * DTLS has additional fields in the Handshake layer,
4159 * between the length field and the actual payload:
4160 * uint16 message_seq;
4161 * uint24 fragment_offset;
4162 * uint24 fragment_length;
4163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004165 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004166 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004167 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004168 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004169 {
4170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4171 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004172 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004173 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004174 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4175 }
4176
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004177 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004178 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004179
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004180 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004181 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004182 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004183 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4184 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4185 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004186 }
4187 else
4188 {
4189 ssl->out_msg[4] = 0;
4190 ssl->out_msg[5] = 0;
4191 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004192
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004193 /* Handshake hashes are computed without fragmentation,
4194 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004195 memset( ssl->out_msg + 6, 0x00, 3 );
4196 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004197 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004199
Hanno Becker0207e532018-08-28 10:28:28 +01004200 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004201 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4202 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004203 }
4204
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004205 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004207 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004208 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4209 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004210 {
4211 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004214 return( ret );
4215 }
4216 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004217 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004218#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004219 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004220 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004221 {
4222 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4223 return( ret );
4224 }
4225 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004226
4227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4228
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004229 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004230}
4231
4232/*
4233 * Record layer functions
4234 */
4235
4236/*
4237 * Write current record.
4238 *
4239 * Uses:
4240 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4241 * - ssl->out_msglen: length of the record content (excl headers)
4242 * - ssl->out_msg: record content
4243 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004244int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004245{
4246 int ret, done = 0;
4247 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004248 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004249
4250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004253 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004255 {
4256 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004259 return( ret );
4260 }
4261
4262 len = ssl->out_msglen;
4263 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4267 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271 ret = mbedtls_ssl_hw_record_write( ssl );
4272 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4275 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004276 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004277
4278 if( ret == 0 )
4279 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004280 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004282 if( !done )
4283 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004284 unsigned i;
4285 size_t protected_record_size;
4286
Hanno Becker6430faf2019-05-08 11:57:13 +01004287 /* Skip writing the record content type to after the encryption,
4288 * as it may change when using the CID extension. */
4289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004290 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004291 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004292
Hanno Becker19859472018-08-06 09:40:20 +01004293 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004294 ssl->out_len[0] = (unsigned char)( len >> 8 );
4295 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004296
Paul Bakker48916f92012-09-16 19:57:18 +00004297 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004298 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004299 mbedtls_record rec;
4300
4301 rec.buf = ssl->out_iv;
4302 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4303 ( ssl->out_iv - ssl->out_buf );
4304 rec.data_len = ssl->out_msglen;
4305 rec.data_offset = ssl->out_msg - rec.buf;
4306
4307 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4308 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4309 ssl->conf->transport, rec.ver );
4310 rec.type = ssl->out_msgtype;
4311
Hanno Beckera0e20d02019-05-15 14:03:01 +01004312#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004313 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004314 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004315#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004316
Hanno Beckera18d1322018-01-03 14:27:32 +00004317 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004318 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004321 return( ret );
4322 }
4323
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004324 if( rec.data_offset != 0 )
4325 {
4326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4328 }
4329
Hanno Becker6430faf2019-05-08 11:57:13 +01004330 /* Update the record content type and CID. */
4331 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004332#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004333 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004334#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004335 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004336 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4337 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004338 }
4339
Hanno Becker5903de42019-05-03 14:46:38 +01004340 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004341
4342#if defined(MBEDTLS_SSL_PROTO_DTLS)
4343 /* In case of DTLS, double-check that we don't exceed
4344 * the remaining space in the datagram. */
4345 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4346 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004347 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004348 if( ret < 0 )
4349 return( ret );
4350
4351 if( protected_record_size > (size_t) ret )
4352 {
4353 /* Should never happen */
4354 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4355 }
4356 }
4357#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004358
Hanno Becker6430faf2019-05-08 11:57:13 +01004359 /* Now write the potentially updated record content type. */
4360 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004363 "version = [%d:%d], msglen = %d",
4364 ssl->out_hdr[0], ssl->out_hdr[1],
4365 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004368 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004369
4370 ssl->out_left += protected_record_size;
4371 ssl->out_hdr += protected_record_size;
4372 ssl_update_out_pointers( ssl, ssl->transform_out );
4373
Hanno Becker04484622018-08-06 09:49:38 +01004374 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4375 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4376 break;
4377
4378 /* The loop goes to its end iff the counter is wrapping */
4379 if( i == ssl_ep_len( ssl ) )
4380 {
4381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4382 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4383 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004384 }
4385
Hanno Becker67bc7c32018-08-06 11:33:50 +01004386#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004387 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4388 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004389 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004390 size_t remaining;
4391 ret = ssl_get_remaining_payload_in_datagram( ssl );
4392 if( ret < 0 )
4393 {
4394 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4395 ret );
4396 return( ret );
4397 }
4398
4399 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004400 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004401 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004402 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004403 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004404 else
4405 {
Hanno Becker513815a2018-08-20 11:56:09 +01004406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004407 }
4408 }
4409#endif /* MBEDTLS_SSL_PROTO_DTLS */
4410
4411 if( ( flush == SSL_FORCE_FLUSH ) &&
4412 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004415 return( ret );
4416 }
4417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004419
4420 return( 0 );
4421}
4422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004424
4425static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4426{
4427 if( ssl->in_msglen < ssl->in_hslen ||
4428 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4429 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4430 {
4431 return( 1 );
4432 }
4433 return( 0 );
4434}
Hanno Becker44650b72018-08-16 12:51:11 +01004435
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004436static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004437{
4438 return( ( ssl->in_msg[9] << 16 ) |
4439 ( ssl->in_msg[10] << 8 ) |
4440 ssl->in_msg[11] );
4441}
4442
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004443static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004444{
4445 return( ( ssl->in_msg[6] << 16 ) |
4446 ( ssl->in_msg[7] << 8 ) |
4447 ssl->in_msg[8] );
4448}
4449
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004450static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004451{
4452 uint32_t msg_len, frag_off, frag_len;
4453
4454 msg_len = ssl_get_hs_total_len( ssl );
4455 frag_off = ssl_get_hs_frag_off( ssl );
4456 frag_len = ssl_get_hs_frag_len( ssl );
4457
4458 if( frag_off > msg_len )
4459 return( -1 );
4460
4461 if( frag_len > msg_len - frag_off )
4462 return( -1 );
4463
4464 if( frag_len + 12 > ssl->in_msglen )
4465 return( -1 );
4466
4467 return( 0 );
4468}
4469
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004470/*
4471 * Mark bits in bitmask (used for DTLS HS reassembly)
4472 */
4473static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4474{
4475 unsigned int start_bits, end_bits;
4476
4477 start_bits = 8 - ( offset % 8 );
4478 if( start_bits != 8 )
4479 {
4480 size_t first_byte_idx = offset / 8;
4481
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004482 /* Special case */
4483 if( len <= start_bits )
4484 {
4485 for( ; len != 0; len-- )
4486 mask[first_byte_idx] |= 1 << ( start_bits - len );
4487
4488 /* Avoid potential issues with offset or len becoming invalid */
4489 return;
4490 }
4491
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004492 offset += start_bits; /* Now offset % 8 == 0 */
4493 len -= start_bits;
4494
4495 for( ; start_bits != 0; start_bits-- )
4496 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4497 }
4498
4499 end_bits = len % 8;
4500 if( end_bits != 0 )
4501 {
4502 size_t last_byte_idx = ( offset + len ) / 8;
4503
4504 len -= end_bits; /* Now len % 8 == 0 */
4505
4506 for( ; end_bits != 0; end_bits-- )
4507 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4508 }
4509
4510 memset( mask + offset / 8, 0xFF, len / 8 );
4511}
4512
4513/*
4514 * Check that bitmask is full
4515 */
4516static int ssl_bitmask_check( unsigned char *mask, size_t len )
4517{
4518 size_t i;
4519
4520 for( i = 0; i < len / 8; i++ )
4521 if( mask[i] != 0xFF )
4522 return( -1 );
4523
4524 for( i = 0; i < len % 8; i++ )
4525 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4526 return( -1 );
4527
4528 return( 0 );
4529}
4530
Hanno Becker56e205e2018-08-16 09:06:12 +01004531/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004532static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004533 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004534{
Hanno Becker56e205e2018-08-16 09:06:12 +01004535 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004536
Hanno Becker56e205e2018-08-16 09:06:12 +01004537 alloc_len = 12; /* Handshake header */
4538 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004539
Hanno Beckerd07df862018-08-16 09:14:58 +01004540 if( add_bitmap )
4541 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004542
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004543 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004544}
Hanno Becker56e205e2018-08-16 09:06:12 +01004545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004547
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004548static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004549{
4550 return( ( ssl->in_msg[1] << 16 ) |
4551 ( ssl->in_msg[2] << 8 ) |
4552 ssl->in_msg[3] );
4553}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004554
Simon Butcher99000142016-10-13 17:21:01 +01004555int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004556{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004560 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004562 }
4563
Hanno Becker12555c62018-08-16 12:47:53 +01004564 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004567 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004568 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004571 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004572 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004573 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004574 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004575
Hanno Becker44650b72018-08-16 12:51:11 +01004576 if( ssl_check_hs_header( ssl ) != 0 )
4577 {
4578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4579 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4580 }
4581
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004582 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004583 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4584 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4585 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4586 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004587 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004588 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4589 {
4590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4591 recv_msg_seq,
4592 ssl->handshake->in_msg_seq ) );
4593 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4594 }
4595
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004596 /* Retransmit only on last message from previous flight, to avoid
4597 * too many retransmissions.
4598 * Besides, No sane server ever retransmits HelloVerifyRequest */
4599 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004603 "message_seq = %d, start_of_flight = %d",
4604 recv_msg_seq,
4605 ssl->handshake->in_flight_start_seq ) );
4606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004607 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004610 return( ret );
4611 }
4612 }
4613 else
4614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004616 "message_seq = %d, expected = %d",
4617 recv_msg_seq,
4618 ssl->handshake->in_msg_seq ) );
4619 }
4620
Hanno Becker90333da2017-10-10 11:27:13 +01004621 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004622 }
4623 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004624
Hanno Becker6d97ef52018-08-16 13:09:04 +01004625 /* Message reassembly is handled alongside buffering of future
4626 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004627 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004628 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004629 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004632 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004633 }
4634 }
4635 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004636#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004637 /* With TLS we don't handle fragmentation (for now) */
4638 if( ssl->in_msglen < ssl->in_hslen )
4639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4641 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004642 }
4643
Simon Butcher99000142016-10-13 17:21:01 +01004644 return( 0 );
4645}
4646
4647void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4648{
Hanno Becker0271f962018-08-16 13:23:47 +01004649 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004650
Hanno Becker0271f962018-08-16 13:23:47 +01004651 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004652 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004653 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004654 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004655
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004656 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004657#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004658 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004659 ssl->handshake != NULL )
4660 {
Hanno Becker0271f962018-08-16 13:23:47 +01004661 unsigned offset;
4662 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004663
Hanno Becker0271f962018-08-16 13:23:47 +01004664 /* Increment handshake sequence number */
4665 hs->in_msg_seq++;
4666
4667 /*
4668 * Clear up handshake buffering and reassembly structure.
4669 */
4670
4671 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004672 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004673
4674 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004675 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4676 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004677 offset++, hs_buf++ )
4678 {
4679 *hs_buf = *(hs_buf + 1);
4680 }
4681
4682 /* Create a fresh last entry */
4683 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004684 }
4685#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004686}
4687
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004688/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004689 * DTLS anti-replay: RFC 6347 4.1.2.6
4690 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004691 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4692 * Bit n is set iff record number in_window_top - n has been seen.
4693 *
4694 * Usually, in_window_top is the last record number seen and the lsb of
4695 * in_window is set. The only exception is the initial state (record number 0
4696 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004698#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4699static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004700{
4701 ssl->in_window_top = 0;
4702 ssl->in_window = 0;
4703}
4704
4705static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4706{
4707 return( ( (uint64_t) buf[0] << 40 ) |
4708 ( (uint64_t) buf[1] << 32 ) |
4709 ( (uint64_t) buf[2] << 24 ) |
4710 ( (uint64_t) buf[3] << 16 ) |
4711 ( (uint64_t) buf[4] << 8 ) |
4712 ( (uint64_t) buf[5] ) );
4713}
4714
4715/*
4716 * Return 0 if sequence number is acceptable, -1 otherwise
4717 */
Hanno Becker0183d692019-07-12 08:50:37 +01004718int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004719{
4720 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4721 uint64_t bit;
4722
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004723 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004724 return( 0 );
4725
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004726 if( rec_seqnum > ssl->in_window_top )
4727 return( 0 );
4728
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004729 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004730
4731 if( bit >= 64 )
4732 return( -1 );
4733
4734 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4735 return( -1 );
4736
4737 return( 0 );
4738}
4739
4740/*
4741 * Update replay window on new validated record
4742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004743void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004744{
4745 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4746
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004747 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004748 return;
4749
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004750 if( rec_seqnum > ssl->in_window_top )
4751 {
4752 /* Update window_top and the contents of the window */
4753 uint64_t shift = rec_seqnum - ssl->in_window_top;
4754
4755 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004756 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004757 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004758 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004759 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004760 ssl->in_window |= 1;
4761 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004762
4763 ssl->in_window_top = rec_seqnum;
4764 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004765 else
4766 {
4767 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004768 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004769
4770 if( bit < 64 ) /* Always true, but be extra sure */
4771 ssl->in_window |= (uint64_t) 1 << bit;
4772 }
4773}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004775
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004776#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004777/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004778static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4779
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004780/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004781 * Without any SSL context, check if a datagram looks like a ClientHello with
4782 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004783 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004784 *
4785 * - if cookie is valid, return 0
4786 * - if ClientHello looks superficially valid but cookie is not,
4787 * fill obuf and set olen, then
4788 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4789 * - otherwise return a specific error code
4790 */
4791static int ssl_check_dtls_clihlo_cookie(
4792 mbedtls_ssl_cookie_write_t *f_cookie_write,
4793 mbedtls_ssl_cookie_check_t *f_cookie_check,
4794 void *p_cookie,
4795 const unsigned char *cli_id, size_t cli_id_len,
4796 const unsigned char *in, size_t in_len,
4797 unsigned char *obuf, size_t buf_len, size_t *olen )
4798{
4799 size_t sid_len, cookie_len;
4800 unsigned char *p;
4801
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004802 /*
4803 * Structure of ClientHello with record and handshake headers,
4804 * and expected values. We don't need to check a lot, more checks will be
4805 * done when actually parsing the ClientHello - skipping those checks
4806 * avoids code duplication and does not make cookie forging any easier.
4807 *
4808 * 0-0 ContentType type; copied, must be handshake
4809 * 1-2 ProtocolVersion version; copied
4810 * 3-4 uint16 epoch; copied, must be 0
4811 * 5-10 uint48 sequence_number; copied
4812 * 11-12 uint16 length; (ignored)
4813 *
4814 * 13-13 HandshakeType msg_type; (ignored)
4815 * 14-16 uint24 length; (ignored)
4816 * 17-18 uint16 message_seq; copied
4817 * 19-21 uint24 fragment_offset; copied, must be 0
4818 * 22-24 uint24 fragment_length; (ignored)
4819 *
4820 * 25-26 ProtocolVersion client_version; (ignored)
4821 * 27-58 Random random; (ignored)
4822 * 59-xx SessionID session_id; 1 byte len + sid_len content
4823 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4824 * ...
4825 *
4826 * Minimum length is 61 bytes.
4827 */
4828 if( in_len < 61 ||
4829 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4830 in[3] != 0 || in[4] != 0 ||
4831 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4832 {
4833 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4834 }
4835
4836 sid_len = in[59];
4837 if( sid_len > in_len - 61 )
4838 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4839
4840 cookie_len = in[60 + sid_len];
4841 if( cookie_len > in_len - 60 )
4842 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4843
4844 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4845 cli_id, cli_id_len ) == 0 )
4846 {
4847 /* Valid cookie */
4848 return( 0 );
4849 }
4850
4851 /*
4852 * If we get here, we've got an invalid cookie, let's prepare HVR.
4853 *
4854 * 0-0 ContentType type; copied
4855 * 1-2 ProtocolVersion version; copied
4856 * 3-4 uint16 epoch; copied
4857 * 5-10 uint48 sequence_number; copied
4858 * 11-12 uint16 length; olen - 13
4859 *
4860 * 13-13 HandshakeType msg_type; hello_verify_request
4861 * 14-16 uint24 length; olen - 25
4862 * 17-18 uint16 message_seq; copied
4863 * 19-21 uint24 fragment_offset; copied
4864 * 22-24 uint24 fragment_length; olen - 25
4865 *
4866 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4867 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4868 *
4869 * Minimum length is 28.
4870 */
4871 if( buf_len < 28 )
4872 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4873
4874 /* Copy most fields and adapt others */
4875 memcpy( obuf, in, 25 );
4876 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4877 obuf[25] = 0xfe;
4878 obuf[26] = 0xff;
4879
4880 /* Generate and write actual cookie */
4881 p = obuf + 28;
4882 if( f_cookie_write( p_cookie,
4883 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4884 {
4885 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4886 }
4887
4888 *olen = p - obuf;
4889
4890 /* Go back and fill length fields */
4891 obuf[27] = (unsigned char)( *olen - 28 );
4892
4893 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4894 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4895 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4896
4897 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4898 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4899
4900 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4901}
4902
4903/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004904 * Handle possible client reconnect with the same UDP quadruplet
4905 * (RFC 6347 Section 4.2.8).
4906 *
4907 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4908 * that looks like a ClientHello.
4909 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004910 * - if the input looks like a ClientHello without cookies,
4911 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004912 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004913 * - if the input looks like a ClientHello with a valid cookie,
4914 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004915 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004916 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004917 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004918 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004919 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4920 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004921 */
4922static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4923{
4924 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004925 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004926
Hanno Becker2fddd372019-07-10 14:37:41 +01004927 if( ssl->conf->f_cookie_write == NULL ||
4928 ssl->conf->f_cookie_check == NULL )
4929 {
4930 /* If we can't use cookies to verify reachability of the peer,
4931 * drop the record. */
4932 return( 0 );
4933 }
4934
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004935 ret = ssl_check_dtls_clihlo_cookie(
4936 ssl->conf->f_cookie_write,
4937 ssl->conf->f_cookie_check,
4938 ssl->conf->p_cookie,
4939 ssl->cli_id, ssl->cli_id_len,
4940 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004941 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004942
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004943 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4944
4945 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004946 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004947 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004948 * If the error is permanent we'll catch it later,
4949 * if it's not, then hopefully it'll work next time. */
4950 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004951 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004952 }
4953
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004954 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004955 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004956 /* Got a valid cookie, partially reset context */
4957 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4958 {
4959 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4960 return( ret );
4961 }
4962
4963 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004964 }
4965
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004966 return( ret );
4967}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004968#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004969
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004970static int ssl_check_record_type( uint8_t record_type )
4971{
4972 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4973 record_type != MBEDTLS_SSL_MSG_ALERT &&
4974 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4975 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4976 {
4977 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4978 }
4979
4980 return( 0 );
4981}
4982
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004983/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004984 * ContentType type;
4985 * ProtocolVersion version;
4986 * uint16 epoch; // DTLS only
4987 * uint48 sequence_number; // DTLS only
4988 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004989 *
4990 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004991 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004992 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4993 *
4994 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004995 * 1. proceed with the record if this function returns 0
4996 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4997 * 3. return CLIENT_RECONNECT if this function return that value
4998 * 4. drop the whole datagram if this function returns anything else.
4999 * Point 2 is needed when the peer is resending, and we have already received
5000 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005001 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005002static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005003 unsigned char *buf,
5004 size_t len,
5005 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005006{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005007 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005008
Hanno Beckere5e7e782019-07-11 12:29:35 +01005009 size_t const rec_hdr_type_offset = 0;
5010 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005011
Hanno Beckere5e7e782019-07-11 12:29:35 +01005012 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5013 rec_hdr_type_len;
5014 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005015
Hanno Beckere5e7e782019-07-11 12:29:35 +01005016 size_t const rec_hdr_ctr_len = 8;
5017#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005018 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005019 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5020 rec_hdr_version_len;
5021
Hanno Beckera0e20d02019-05-15 14:03:01 +01005022#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005023 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5024 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005025 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005026#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5027#endif /* MBEDTLS_SSL_PROTO_DTLS */
5028
5029 size_t rec_hdr_len_offset; /* To be determined */
5030 size_t const rec_hdr_len_len = 2;
5031
5032 /*
5033 * Check minimum lengths for record header.
5034 */
5035
5036#if defined(MBEDTLS_SSL_PROTO_DTLS)
5037 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5038 {
5039 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5040 }
5041 else
5042#endif /* MBEDTLS_SSL_PROTO_DTLS */
5043 {
5044 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5045 }
5046
5047 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5048 {
5049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5050 (unsigned) len,
5051 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5052 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5053 }
5054
5055 /*
5056 * Parse and validate record content type
5057 */
5058
5059 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005060
5061 /* Check record content type */
5062#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5063 rec->cid_len = 0;
5064
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005065 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005066 ssl->conf->cid_len != 0 &&
5067 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005068 {
5069 /* Shift pointers to account for record header including CID
5070 * struct {
5071 * ContentType special_type = tls12_cid;
5072 * ProtocolVersion version;
5073 * uint16 epoch;
5074 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005075 * opaque cid[cid_length]; // Additional field compared to
5076 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005077 * uint16 length;
5078 * opaque enc_content[DTLSCiphertext.length];
5079 * } DTLSCiphertext;
5080 */
5081
5082 /* So far, we only support static CID lengths
5083 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005084 rec_hdr_cid_len = ssl->conf->cid_len;
5085 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005086
Hanno Beckere5e7e782019-07-11 12:29:35 +01005087 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005088 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5090 (unsigned) len,
5091 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005092 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005093 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005094
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005095 /* configured CID len is guaranteed at most 255, see
5096 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5097 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005098 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005099 }
5100 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005101#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005102 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005103 if( ssl_check_record_type( rec->type ) )
5104 {
Hanno Becker54229812019-07-12 14:40:00 +01005105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5106 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005107 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5108 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005109 }
5110
Hanno Beckere5e7e782019-07-11 12:29:35 +01005111 /*
5112 * Parse and validate record version
5113 */
5114
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005115 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5116 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005117 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5118 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005119 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005120
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005121 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5124 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005125 }
5126
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005127 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5130 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005131 }
5132
Hanno Beckere5e7e782019-07-11 12:29:35 +01005133 /*
5134 * Parse/Copy record sequence number.
5135 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005136
Hanno Beckere5e7e782019-07-11 12:29:35 +01005137#if defined(MBEDTLS_SSL_PROTO_DTLS)
5138 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005139 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005140 /* Copy explicit record sequence number from input buffer. */
5141 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5142 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005143 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005144 else
5145#endif /* MBEDTLS_SSL_PROTO_DTLS */
5146 {
5147 /* Copy implicit record sequence number from SSL context structure. */
5148 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5149 }
Paul Bakker40e46942009-01-03 21:51:57 +00005150
Hanno Beckere5e7e782019-07-11 12:29:35 +01005151 /*
5152 * Parse record length.
5153 */
5154
Hanno Beckere5e7e782019-07-11 12:29:35 +01005155 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005156 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5157 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005158 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005159
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005161 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005162 rec->type,
5163 major_ver, minor_ver, rec->data_len ) );
5164
5165 rec->buf = buf;
5166 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005167
Hanno Beckerd417cc92019-07-26 08:20:27 +01005168 if( rec->data_len == 0 )
5169 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005170
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005171 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005172 * DTLS-related tests.
5173 * Check epoch before checking length constraint because
5174 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5175 * message gets duplicated before the corresponding Finished message,
5176 * the second ChangeCipherSpec should be discarded because it belongs
5177 * to an old epoch, but not because its length is shorter than
5178 * the minimum record length for packets using the new record transform.
5179 * Note that these two kinds of failures are handled differently,
5180 * as an unexpected record is silently skipped but an invalid
5181 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005182 */
5183#if defined(MBEDTLS_SSL_PROTO_DTLS)
5184 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5185 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005186 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005187
Hanno Becker955a5c92019-07-10 17:12:07 +01005188 /* Check that the datagram is large enough to contain a record
5189 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005190 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005191 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5193 (unsigned) len,
5194 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005195 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5196 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005197
Hanno Becker37cfe732019-07-10 17:20:01 +01005198 /* Records from other, non-matching epochs are silently discarded.
5199 * (The case of same-port Client reconnects must be considered in
5200 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005201 if( rec_epoch != ssl->in_epoch )
5202 {
5203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5204 "expected %d, received %d",
5205 ssl->in_epoch, rec_epoch ) );
5206
Hanno Becker552f7472019-07-19 10:59:12 +01005207 /* Records from the next epoch are considered for buffering
5208 * (concretely: early Finished messages). */
5209 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005210 {
Hanno Becker552f7472019-07-19 10:59:12 +01005211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5212 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005213 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005214
Hanno Becker2fddd372019-07-10 14:37:41 +01005215 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005216 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005217#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005218 /* For records from the correct epoch, check whether their
5219 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005220 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005221 {
5222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5223 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5224 }
5225#endif
5226 }
5227#endif /* MBEDTLS_SSL_PROTO_DTLS */
5228
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005229 return( 0 );
5230}
Paul Bakker5121ce52009-01-03 21:22:43 +00005231
Paul Bakker5121ce52009-01-03 21:22:43 +00005232
Hanno Becker2fddd372019-07-10 14:37:41 +01005233#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5234static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5235{
5236 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5237
5238 /*
5239 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5240 * access the first byte of record content (handshake type), as we
5241 * have an active transform (possibly iv_len != 0), so use the
5242 * fact that the record header len is 13 instead.
5243 */
5244 if( rec_epoch == 0 &&
5245 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5246 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5247 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5248 ssl->in_left > 13 &&
5249 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5250 {
5251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5252 "from the same port" ) );
5253 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005254 }
5255
5256 return( 0 );
5257}
Hanno Becker2fddd372019-07-10 14:37:41 +01005258#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005259
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005260/*
5261 * If applicable, decrypt (and decompress) record content
5262 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005263static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5264 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005265{
5266 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005268 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005269 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5272 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276 ret = mbedtls_ssl_hw_record_read( ssl );
5277 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005279 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5280 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005281 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005282
5283 if( ret == 0 )
5284 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005285 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005286#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005287 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005288 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005289 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005290
Hanno Beckera18d1322018-01-03 14:27:32 +00005291 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005292 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005294 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005295
Hanno Beckera0e20d02019-05-15 14:03:01 +01005296#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005297 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5298 ssl->conf->ignore_unexpected_cid
5299 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5300 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005301 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005302 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005303 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005304#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005305
Paul Bakker5121ce52009-01-03 21:22:43 +00005306 return( ret );
5307 }
5308
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005309 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005310 {
5311 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005312 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005313 }
5314
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005315 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005316 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005317
Hanno Beckera0e20d02019-05-15 14:03:01 +01005318#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005319 /* We have already checked the record content type
5320 * in ssl_parse_record_header(), failing or silently
5321 * dropping the record in the case of an unknown type.
5322 *
5323 * Since with the use of CIDs, the record content type
5324 * might change during decryption, re-check the record
5325 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005326 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005327 {
5328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5329 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5330 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005331#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005332
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005333 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005334 {
5335#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5336 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005337 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005338 {
5339 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5341 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5342 }
5343#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5344
5345 ssl->nb_zero++;
5346
5347 /*
5348 * Three or more empty messages may be a DoS attack
5349 * (excessive CPU consumption).
5350 */
5351 if( ssl->nb_zero > 3 )
5352 {
5353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005354 "messages, possible DoS attack" ) );
5355 /* Treat the records as if they were not properly authenticated,
5356 * thereby failing the connection if we see more than allowed
5357 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005358 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5359 }
5360 }
5361 else
5362 ssl->nb_zero = 0;
5363
5364#if defined(MBEDTLS_SSL_PROTO_DTLS)
5365 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5366 {
5367 ; /* in_ctr read from peer, not maintained internally */
5368 }
5369 else
5370#endif
5371 {
5372 unsigned i;
5373 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5374 if( ++ssl->in_ctr[i - 1] != 0 )
5375 break;
5376
5377 /* The loop goes to its end iff the counter is wrapping */
5378 if( i == ssl_ep_len( ssl ) )
5379 {
5380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5381 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5382 }
5383 }
5384
Paul Bakker5121ce52009-01-03 21:22:43 +00005385 }
5386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005388 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005390 {
5391 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005394 return( ret );
5395 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005396 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005400 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005403 }
5404#endif
5405
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005406 /* Check actual (decrypted) record content length against
5407 * configured maximum. */
5408 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5409 {
5410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5411 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5412 }
5413
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005414 return( 0 );
5415}
5416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005418
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005419/*
5420 * Read a record.
5421 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005422 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5423 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5424 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005425 */
Hanno Becker1097b342018-08-15 14:09:41 +01005426
5427/* Helper functions for mbedtls_ssl_read_record(). */
5428static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005429static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5430static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005431
Hanno Becker327c93b2018-08-15 13:56:18 +01005432int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005433 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005434{
5435 int ret;
5436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005438
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005439 if( ssl->keep_current_message == 0 )
5440 {
5441 do {
Simon Butcher99000142016-10-13 17:21:01 +01005442
Hanno Becker26994592018-08-15 14:14:59 +01005443 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005444 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005445 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005446
Hanno Beckere74d5562018-08-15 14:26:08 +01005447 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005448 {
Hanno Becker40f50842018-08-15 14:48:01 +01005449#if defined(MBEDTLS_SSL_PROTO_DTLS)
5450 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005451
Hanno Becker40f50842018-08-15 14:48:01 +01005452 /* We only check for buffered messages if the
5453 * current datagram is fully consumed. */
5454 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005455 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005456 {
Hanno Becker40f50842018-08-15 14:48:01 +01005457 if( ssl_load_buffered_message( ssl ) == 0 )
5458 have_buffered = 1;
5459 }
5460
5461 if( have_buffered == 0 )
5462#endif /* MBEDTLS_SSL_PROTO_DTLS */
5463 {
5464 ret = ssl_get_next_record( ssl );
5465 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5466 continue;
5467
5468 if( ret != 0 )
5469 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005470 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005471 return( ret );
5472 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005473 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005474 }
5475
5476 ret = mbedtls_ssl_handle_message_type( ssl );
5477
Hanno Becker40f50842018-08-15 14:48:01 +01005478#if defined(MBEDTLS_SSL_PROTO_DTLS)
5479 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5480 {
5481 /* Buffer future message */
5482 ret = ssl_buffer_message( ssl );
5483 if( ret != 0 )
5484 return( ret );
5485
5486 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5487 }
5488#endif /* MBEDTLS_SSL_PROTO_DTLS */
5489
Hanno Becker90333da2017-10-10 11:27:13 +01005490 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5491 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005492
5493 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005494 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005495 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005496 return( ret );
5497 }
5498
Hanno Becker327c93b2018-08-15 13:56:18 +01005499 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005500 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005501 {
5502 mbedtls_ssl_update_handshake_status( ssl );
5503 }
Simon Butcher99000142016-10-13 17:21:01 +01005504 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005505 else
Simon Butcher99000142016-10-13 17:21:01 +01005506 {
Hanno Becker02f59072018-08-15 14:00:24 +01005507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005508 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005509 }
5510
5511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5512
5513 return( 0 );
5514}
5515
Hanno Becker40f50842018-08-15 14:48:01 +01005516#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005517static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005518{
Hanno Becker40f50842018-08-15 14:48:01 +01005519 if( ssl->in_left > ssl->next_record_offset )
5520 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005521
Hanno Becker40f50842018-08-15 14:48:01 +01005522 return( 0 );
5523}
5524
5525static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5526{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005527 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005528 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005529 int ret = 0;
5530
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005531 if( hs == NULL )
5532 return( -1 );
5533
Hanno Beckere00ae372018-08-20 09:39:42 +01005534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5535
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005536 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5537 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5538 {
5539 /* Check if we have seen a ChangeCipherSpec before.
5540 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005541 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005542 {
5543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5544 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005545 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005546 }
5547
Hanno Becker39b8bc92018-08-28 17:17:13 +01005548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005549 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5550 ssl->in_msglen = 1;
5551 ssl->in_msg[0] = 1;
5552
5553 /* As long as they are equal, the exact value doesn't matter. */
5554 ssl->in_left = 0;
5555 ssl->next_record_offset = 0;
5556
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005557 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005558 goto exit;
5559 }
Hanno Becker37f95322018-08-16 13:55:32 +01005560
Hanno Beckerb8f50142018-08-28 10:01:34 +01005561#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005562 /* Debug only */
5563 {
5564 unsigned offset;
5565 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5566 {
5567 hs_buf = &hs->buffering.hs[offset];
5568 if( hs_buf->is_valid == 1 )
5569 {
5570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5571 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005572 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005573 }
5574 }
5575 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005576#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005577
5578 /* Check if we have buffered and/or fully reassembled the
5579 * next handshake message. */
5580 hs_buf = &hs->buffering.hs[0];
5581 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5582 {
5583 /* Synthesize a record containing the buffered HS message. */
5584 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5585 ( hs_buf->data[2] << 8 ) |
5586 hs_buf->data[3];
5587
5588 /* Double-check that we haven't accidentally buffered
5589 * a message that doesn't fit into the input buffer. */
5590 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5591 {
5592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5593 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5594 }
5595
5596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5597 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5598 hs_buf->data, msg_len + 12 );
5599
5600 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5601 ssl->in_hslen = msg_len + 12;
5602 ssl->in_msglen = msg_len + 12;
5603 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5604
5605 ret = 0;
5606 goto exit;
5607 }
5608 else
5609 {
5610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5611 hs->in_msg_seq ) );
5612 }
5613
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005614 ret = -1;
5615
5616exit:
5617
5618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5619 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005620}
5621
Hanno Beckera02b0b42018-08-21 17:20:27 +01005622static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5623 size_t desired )
5624{
5625 int offset;
5626 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5628 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005629
Hanno Becker01315ea2018-08-21 17:22:17 +01005630 /* Get rid of future records epoch first, if such exist. */
5631 ssl_free_buffered_record( ssl );
5632
5633 /* Check if we have enough space available now. */
5634 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5635 hs->buffering.total_bytes_buffered ) )
5636 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005638 return( 0 );
5639 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005640
Hanno Becker4f432ad2018-08-28 10:02:32 +01005641 /* We don't have enough space to buffer the next expected handshake
5642 * message. Remove buffers used for future messages to gain space,
5643 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005644 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5645 offset >= 0; offset-- )
5646 {
5647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5648 offset ) );
5649
Hanno Beckerb309b922018-08-23 13:18:05 +01005650 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005651
5652 /* Check if we have enough space available now. */
5653 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5654 hs->buffering.total_bytes_buffered ) )
5655 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005657 return( 0 );
5658 }
5659 }
5660
5661 return( -1 );
5662}
5663
Hanno Becker40f50842018-08-15 14:48:01 +01005664static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5665{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005666 int ret = 0;
5667 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5668
5669 if( hs == NULL )
5670 return( 0 );
5671
5672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5673
5674 switch( ssl->in_msgtype )
5675 {
5676 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005678
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005679 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005680 break;
5681
5682 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005683 {
5684 unsigned recv_msg_seq_offset;
5685 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5686 mbedtls_ssl_hs_buffer *hs_buf;
5687 size_t msg_len = ssl->in_hslen - 12;
5688
5689 /* We should never receive an old handshake
5690 * message - double-check nonetheless. */
5691 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5692 {
5693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5695 }
5696
5697 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5698 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5699 {
5700 /* Silently ignore -- message too far in the future */
5701 MBEDTLS_SSL_DEBUG_MSG( 2,
5702 ( "Ignore future HS message with sequence number %u, "
5703 "buffering window %u - %u",
5704 recv_msg_seq, ssl->handshake->in_msg_seq,
5705 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5706
5707 goto exit;
5708 }
5709
5710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5711 recv_msg_seq, recv_msg_seq_offset ) );
5712
5713 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5714
5715 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005716 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005717 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005718 size_t reassembly_buf_sz;
5719
Hanno Becker37f95322018-08-16 13:55:32 +01005720 hs_buf->is_fragmented =
5721 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5722
5723 /* We copy the message back into the input buffer
5724 * after reassembly, so check that it's not too large.
5725 * This is an implementation-specific limitation
5726 * and not one from the standard, hence it is not
5727 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005728 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005729 {
5730 /* Ignore message */
5731 goto exit;
5732 }
5733
Hanno Beckere0b150f2018-08-21 15:51:03 +01005734 /* Check if we have enough space to buffer the message. */
5735 if( hs->buffering.total_bytes_buffered >
5736 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5737 {
5738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5739 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5740 }
5741
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005742 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5743 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005744
5745 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5746 hs->buffering.total_bytes_buffered ) )
5747 {
5748 if( recv_msg_seq_offset > 0 )
5749 {
5750 /* If we can't buffer a future message because
5751 * of space limitations -- ignore. */
5752 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",
5753 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5754 (unsigned) hs->buffering.total_bytes_buffered ) );
5755 goto exit;
5756 }
Hanno Beckere1801392018-08-21 16:51:05 +01005757 else
5758 {
5759 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",
5760 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5761 (unsigned) hs->buffering.total_bytes_buffered ) );
5762 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005763
Hanno Beckera02b0b42018-08-21 17:20:27 +01005764 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005765 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005766 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",
5767 (unsigned) msg_len,
5768 (unsigned) reassembly_buf_sz,
5769 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005770 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005771 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5772 goto exit;
5773 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005774 }
5775
5776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5777 msg_len ) );
5778
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005779 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5780 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005781 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005782 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005783 goto exit;
5784 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005785 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005786
5787 /* Prepare final header: copy msg_type, length and message_seq,
5788 * then add standardised fragment_offset and fragment_length */
5789 memcpy( hs_buf->data, ssl->in_msg, 6 );
5790 memset( hs_buf->data + 6, 0, 3 );
5791 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5792
5793 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005794
5795 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005796 }
5797 else
5798 {
5799 /* Make sure msg_type and length are consistent */
5800 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5801 {
5802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5803 /* Ignore */
5804 goto exit;
5805 }
5806 }
5807
Hanno Becker4422bbb2018-08-20 09:40:19 +01005808 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005809 {
5810 size_t frag_len, frag_off;
5811 unsigned char * const msg = hs_buf->data + 12;
5812
5813 /*
5814 * Check and copy current fragment
5815 */
5816
5817 /* Validation of header fields already done in
5818 * mbedtls_ssl_prepare_handshake_record(). */
5819 frag_off = ssl_get_hs_frag_off( ssl );
5820 frag_len = ssl_get_hs_frag_len( ssl );
5821
5822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5823 frag_off, frag_len ) );
5824 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5825
5826 if( hs_buf->is_fragmented )
5827 {
5828 unsigned char * const bitmask = msg + msg_len;
5829 ssl_bitmask_set( bitmask, frag_off, frag_len );
5830 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5831 msg_len ) == 0 );
5832 }
5833 else
5834 {
5835 hs_buf->is_complete = 1;
5836 }
5837
5838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5839 hs_buf->is_complete ? "" : "not yet " ) );
5840 }
5841
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005842 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005843 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005844
5845 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005846 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005847 break;
5848 }
5849
5850exit:
5851
5852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5853 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005854}
5855#endif /* MBEDTLS_SSL_PROTO_DTLS */
5856
Hanno Becker1097b342018-08-15 14:09:41 +01005857static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005858{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005859 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005860 * Consume last content-layer message and potentially
5861 * update in_msglen which keeps track of the contents'
5862 * consumption state.
5863 *
5864 * (1) Handshake messages:
5865 * Remove last handshake message, move content
5866 * and adapt in_msglen.
5867 *
5868 * (2) Alert messages:
5869 * Consume whole record content, in_msglen = 0.
5870 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005871 * (3) Change cipher spec:
5872 * Consume whole record content, in_msglen = 0.
5873 *
5874 * (4) Application data:
5875 * Don't do anything - the record layer provides
5876 * the application data as a stream transport
5877 * and consumes through mbedtls_ssl_read only.
5878 *
5879 */
5880
5881 /* Case (1): Handshake messages */
5882 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005883 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005884 /* Hard assertion to be sure that no application data
5885 * is in flight, as corrupting ssl->in_msglen during
5886 * ssl->in_offt != NULL is fatal. */
5887 if( ssl->in_offt != NULL )
5888 {
5889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5890 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5891 }
5892
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005893 /*
5894 * Get next Handshake message in the current record
5895 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005896
Hanno Becker4a810fb2017-05-24 16:27:30 +01005897 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005898 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005899 * current handshake content: If DTLS handshake
5900 * fragmentation is used, that's the fragment
5901 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005902 * size here is faulty and should be changed at
5903 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005904 * (2) While it doesn't seem to cause problems, one
5905 * has to be very careful not to assume that in_hslen
5906 * is always <= in_msglen in a sensible communication.
5907 * Again, it's wrong for DTLS handshake fragmentation.
5908 * The following check is therefore mandatory, and
5909 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005910 * Additionally, ssl->in_hslen might be arbitrarily out of
5911 * bounds after handling a DTLS message with an unexpected
5912 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005913 */
5914 if( ssl->in_hslen < ssl->in_msglen )
5915 {
5916 ssl->in_msglen -= ssl->in_hslen;
5917 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5918 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005919
Hanno Becker4a810fb2017-05-24 16:27:30 +01005920 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5921 ssl->in_msg, ssl->in_msglen );
5922 }
5923 else
5924 {
5925 ssl->in_msglen = 0;
5926 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005927
Hanno Becker4a810fb2017-05-24 16:27:30 +01005928 ssl->in_hslen = 0;
5929 }
5930 /* Case (4): Application data */
5931 else if( ssl->in_offt != NULL )
5932 {
5933 return( 0 );
5934 }
5935 /* Everything else (CCS & Alerts) */
5936 else
5937 {
5938 ssl->in_msglen = 0;
5939 }
5940
Hanno Becker1097b342018-08-15 14:09:41 +01005941 return( 0 );
5942}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005943
Hanno Beckere74d5562018-08-15 14:26:08 +01005944static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5945{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005946 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005947 return( 1 );
5948
5949 return( 0 );
5950}
5951
Hanno Becker5f066e72018-08-16 14:56:31 +01005952#if defined(MBEDTLS_SSL_PROTO_DTLS)
5953
5954static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5955{
5956 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5957 if( hs == NULL )
5958 return;
5959
Hanno Becker01315ea2018-08-21 17:22:17 +01005960 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005961 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005962 hs->buffering.total_bytes_buffered -=
5963 hs->buffering.future_record.len;
5964
5965 mbedtls_free( hs->buffering.future_record.data );
5966 hs->buffering.future_record.data = NULL;
5967 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005968}
5969
5970static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5971{
5972 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5973 unsigned char * rec;
5974 size_t rec_len;
5975 unsigned rec_epoch;
5976
5977 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5978 return( 0 );
5979
5980 if( hs == NULL )
5981 return( 0 );
5982
Hanno Becker5f066e72018-08-16 14:56:31 +01005983 rec = hs->buffering.future_record.data;
5984 rec_len = hs->buffering.future_record.len;
5985 rec_epoch = hs->buffering.future_record.epoch;
5986
5987 if( rec == NULL )
5988 return( 0 );
5989
Hanno Becker4cb782d2018-08-20 11:19:05 +01005990 /* Only consider loading future records if the
5991 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005992 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005993 return( 0 );
5994
Hanno Becker5f066e72018-08-16 14:56:31 +01005995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5996
5997 if( rec_epoch != ssl->in_epoch )
5998 {
5999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6000 goto exit;
6001 }
6002
6003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6004
6005 /* Double-check that the record is not too large */
6006 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6007 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6008 {
6009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6010 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6011 }
6012
6013 memcpy( ssl->in_hdr, rec, rec_len );
6014 ssl->in_left = rec_len;
6015 ssl->next_record_offset = 0;
6016
6017 ssl_free_buffered_record( ssl );
6018
6019exit:
6020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6021 return( 0 );
6022}
6023
Hanno Becker519f15d2019-07-11 12:43:20 +01006024static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6025 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006026{
6027 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006028
6029 /* Don't buffer future records outside handshakes. */
6030 if( hs == NULL )
6031 return( 0 );
6032
6033 /* Only buffer handshake records (we are only interested
6034 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006035 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006036 return( 0 );
6037
6038 /* Don't buffer more than one future epoch record. */
6039 if( hs->buffering.future_record.data != NULL )
6040 return( 0 );
6041
Hanno Becker01315ea2018-08-21 17:22:17 +01006042 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006043 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006044 hs->buffering.total_bytes_buffered ) )
6045 {
6046 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 +01006047 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006048 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006049 return( 0 );
6050 }
6051
Hanno Becker5f066e72018-08-16 14:56:31 +01006052 /* Buffer record */
6053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6054 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006055 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006056
6057 /* ssl_parse_record_header() only considers records
6058 * of the next epoch as candidates for buffering. */
6059 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006060 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006061
6062 hs->buffering.future_record.data =
6063 mbedtls_calloc( 1, hs->buffering.future_record.len );
6064 if( hs->buffering.future_record.data == NULL )
6065 {
6066 /* If we run out of RAM trying to buffer a
6067 * record from the next epoch, just ignore. */
6068 return( 0 );
6069 }
6070
Hanno Becker519f15d2019-07-11 12:43:20 +01006071 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006072
Hanno Becker519f15d2019-07-11 12:43:20 +01006073 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006074 return( 0 );
6075}
6076
6077#endif /* MBEDTLS_SSL_PROTO_DTLS */
6078
Hanno Beckere74d5562018-08-15 14:26:08 +01006079static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006080{
6081 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006082 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006083
Hanno Becker5f066e72018-08-16 14:56:31 +01006084#if defined(MBEDTLS_SSL_PROTO_DTLS)
6085 /* We might have buffered a future record; if so,
6086 * and if the epoch matches now, load it.
6087 * On success, this call will set ssl->in_left to
6088 * the length of the buffered record, so that
6089 * the calls to ssl_fetch_input() below will
6090 * essentially be no-ops. */
6091 ret = ssl_load_buffered_record( ssl );
6092 if( ret != 0 )
6093 return( ret );
6094#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006095
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006096 /* Ensure that we have enough space available for the default form
6097 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6098 * with no space for CIDs counted in). */
6099 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6100 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006103 return( ret );
6104 }
6105
Hanno Beckere5e7e782019-07-11 12:29:35 +01006106 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6107 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006110 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006111 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006112 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6113 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006114 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006115 if( ret != 0 )
6116 return( ret );
6117
6118 /* Fall through to handling of unexpected records */
6119 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6120 }
6121
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006122 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6123 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006124#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006125 /* Reset in pointers to default state for TLS/DTLS records,
6126 * assuming no CID and no offset between record content and
6127 * record plaintext. */
6128 ssl_update_in_pointers( ssl );
6129
Hanno Becker7ae20e02019-07-12 08:33:49 +01006130 /* Setup internal message pointers from record structure. */
6131 ssl->in_msgtype = rec.type;
6132#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6133 ssl->in_len = ssl->in_cid + rec.cid_len;
6134#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6135 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6136 ssl->in_msglen = rec.data_len;
6137
Hanno Becker2fddd372019-07-10 14:37:41 +01006138 ret = ssl_check_client_reconnect( ssl );
6139 if( ret != 0 )
6140 return( ret );
6141#endif
6142
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006143 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006144 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006145
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6147 "(header)" ) );
6148 }
6149 else
6150 {
6151 /* Skip invalid record and the rest of the datagram */
6152 ssl->next_record_offset = 0;
6153 ssl->in_left = 0;
6154
6155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6156 "(header)" ) );
6157 }
6158
6159 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006160 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006161 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006162 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006163#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006164 {
6165 return( ret );
6166 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006167 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006170 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006171 {
Hanno Beckera8814792019-07-10 15:01:45 +01006172 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006173 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006174 if( ssl->next_record_offset < ssl->in_left )
6175 {
6176 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6177 }
6178 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006179 else
6180#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006181 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006182 /*
6183 * Fetch record contents from underlying transport.
6184 */
Hanno Beckera3175662019-07-11 12:50:29 +01006185 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006186 if( ret != 0 )
6187 {
6188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6189 return( ret );
6190 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006191
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006192 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006193 }
6194
6195 /*
6196 * Decrypt record contents.
6197 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006198
Hanno Beckerfdf66042019-07-11 13:07:45 +01006199 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006202 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006203 {
6204 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006205 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006206 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006207 /* Except when waiting for Finished as a bad mac here
6208 * probably means something went wrong in the handshake
6209 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6210 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6211 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6212 {
6213#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6214 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6215 {
6216 mbedtls_ssl_send_alert_message( ssl,
6217 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6218 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6219 }
6220#endif
6221 return( ret );
6222 }
6223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006225 if( ssl->conf->badmac_limit != 0 &&
6226 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6229 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006230 }
6231#endif
6232
Hanno Becker4a810fb2017-05-24 16:27:30 +01006233 /* As above, invalid records cause
6234 * dismissal of the whole datagram. */
6235
6236 ssl->next_record_offset = 0;
6237 ssl->in_left = 0;
6238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006240 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006241 }
6242
6243 return( ret );
6244 }
6245 else
6246#endif
6247 {
6248 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6250 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252 mbedtls_ssl_send_alert_message( ssl,
6253 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6254 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006255 }
6256#endif
6257 return( ret );
6258 }
6259 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006260
Hanno Becker44d89b22019-07-12 09:40:44 +01006261
6262 /* Reset in pointers to default state for TLS/DTLS records,
6263 * assuming no CID and no offset between record content and
6264 * record plaintext. */
6265 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006266#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6267 ssl->in_len = ssl->in_cid + rec.cid_len;
6268#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6269 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006270
Hanno Becker8685c822019-07-12 09:37:30 +01006271 /* The record content type may change during decryption,
6272 * so re-read it. */
6273 ssl->in_msgtype = rec.type;
6274 /* Also update the input buffer, because unfortunately
6275 * the server-side ssl_parse_client_hello() reparses the
6276 * record header when receiving a ClientHello initiating
6277 * a renegotiation. */
6278 ssl->in_hdr[0] = rec.type;
6279 ssl->in_msg = rec.buf + rec.data_offset;
6280 ssl->in_msglen = rec.data_len;
6281 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6282 ssl->in_len[1] = (unsigned char)( rec.data_len );
6283
Simon Butcher99000142016-10-13 17:21:01 +01006284 return( 0 );
6285}
6286
6287int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6288{
6289 int ret;
6290
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006291 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006292 * Handle particular types of records
6293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006295 {
Simon Butcher99000142016-10-13 17:21:01 +01006296 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6297 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006298 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006300 }
6301
Hanno Beckere678eaa2018-08-21 14:57:46 +01006302 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006303 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006304 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006305 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6307 ssl->in_msglen ) );
6308 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006309 }
6310
Hanno Beckere678eaa2018-08-21 14:57:46 +01006311 if( ssl->in_msg[0] != 1 )
6312 {
6313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6314 ssl->in_msg[0] ) );
6315 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6316 }
6317
6318#if defined(MBEDTLS_SSL_PROTO_DTLS)
6319 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6320 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6321 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6322 {
6323 if( ssl->handshake == NULL )
6324 {
6325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6326 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6327 }
6328
6329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6330 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6331 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006332#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006333 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006335 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006336 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006337 if( ssl->in_msglen != 2 )
6338 {
6339 /* Note: Standard allows for more than one 2 byte alert
6340 to be packed in a single message, but Mbed TLS doesn't
6341 currently support this. */
6342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6343 ssl->in_msglen ) );
6344 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6345 }
6346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006348 ssl->in_msg[0], ssl->in_msg[1] ) );
6349
6350 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006351 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006356 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006358 }
6359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6361 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6364 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006365 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006366
6367#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6368 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6369 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6370 {
Hanno Becker90333da2017-10-10 11:27:13 +01006371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006372 /* Will be handled when trying to parse ServerHello */
6373 return( 0 );
6374 }
6375#endif
6376
6377#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6378 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6379 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6380 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6381 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6382 {
6383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6384 /* Will be handled in mbedtls_ssl_parse_certificate() */
6385 return( 0 );
6386 }
6387#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6388
6389 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006390 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006391 }
6392
Hanno Beckerc76c6192017-06-06 10:03:17 +01006393#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006394 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006395 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006396 /* Drop unexpected ApplicationData records,
6397 * except at the beginning of renegotiations */
6398 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6399 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6400#if defined(MBEDTLS_SSL_RENEGOTIATION)
6401 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6402 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006403#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006404 )
6405 {
6406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6407 return( MBEDTLS_ERR_SSL_NON_FATAL );
6408 }
6409
6410 if( ssl->handshake != NULL &&
6411 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6412 {
6413 ssl_handshake_wrapup_free_hs_transform( ssl );
6414 }
6415 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006416#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006417
Paul Bakker5121ce52009-01-03 21:22:43 +00006418 return( 0 );
6419}
6420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006421int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006422{
6423 int ret;
6424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6426 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6427 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006428 {
6429 return( ret );
6430 }
6431
6432 return( 0 );
6433}
6434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006436 unsigned char level,
6437 unsigned char message )
6438{
6439 int ret;
6440
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006441 if( ssl == NULL || ssl->conf == NULL )
6442 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006448 ssl->out_msglen = 2;
6449 ssl->out_msg[0] = level;
6450 ssl->out_msg[1] = message;
6451
Hanno Becker67bc7c32018-08-06 11:33:50 +01006452 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006455 return( ret );
6456 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006458
6459 return( 0 );
6460}
6461
Hanno Beckerb9d44792019-02-08 07:19:04 +00006462#if defined(MBEDTLS_X509_CRT_PARSE_C)
6463static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6464{
6465#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6466 if( session->peer_cert != NULL )
6467 {
6468 mbedtls_x509_crt_free( session->peer_cert );
6469 mbedtls_free( session->peer_cert );
6470 session->peer_cert = NULL;
6471 }
6472#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6473 if( session->peer_cert_digest != NULL )
6474 {
6475 /* Zeroization is not necessary. */
6476 mbedtls_free( session->peer_cert_digest );
6477 session->peer_cert_digest = NULL;
6478 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6479 session->peer_cert_digest_len = 0;
6480 }
6481#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6482}
6483#endif /* MBEDTLS_X509_CRT_PARSE_C */
6484
Paul Bakker5121ce52009-01-03 21:22:43 +00006485/*
6486 * Handshake functions
6487 */
Hanno Becker21489932019-02-05 13:20:55 +00006488#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006489/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006491{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006492 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6493 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006496
Hanno Becker7177a882019-02-05 13:36:46 +00006497 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006500 ssl->state++;
6501 return( 0 );
6502 }
6503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006506}
6507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006509{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006510 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6511 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006514
Hanno Becker7177a882019-02-05 13:36:46 +00006515 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006518 ssl->state++;
6519 return( 0 );
6520 }
6521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6523 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006524}
Gilles Peskinef9828522017-05-03 12:28:43 +02006525
Hanno Becker21489932019-02-05 13:20:55 +00006526#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006527/* Some certificate support -> implement write and parse */
6528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006530{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006532 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006534 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6535 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006538
Hanno Becker7177a882019-02-05 13:36:46 +00006539 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006542 ssl->state++;
6543 return( 0 );
6544 }
6545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006547 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006548 {
6549 if( ssl->client_auth == 0 )
6550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006552 ssl->state++;
6553 return( 0 );
6554 }
6555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006557 /*
6558 * If using SSLv3 and got no cert, send an Alert message
6559 * (otherwise an empty Certificate message will be sent).
6560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6562 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006563 {
6564 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6566 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6567 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006570 goto write_msg;
6571 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006573 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574#endif /* MBEDTLS_SSL_CLI_C */
6575#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006576 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006578 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6581 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006582 }
6583 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006584#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006587
6588 /*
6589 * 0 . 0 handshake type
6590 * 1 . 3 handshake length
6591 * 4 . 6 length of all certs
6592 * 7 . 9 length of cert. 1
6593 * 10 . n-1 peer certificate
6594 * n . n+2 length of cert. 2
6595 * n+3 . ... upper level cert, etc.
6596 */
6597 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006599
Paul Bakker29087132010-03-21 21:03:34 +00006600 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006601 {
6602 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006603 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006606 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006608 }
6609
6610 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6611 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6612 ssl->out_msg[i + 2] = (unsigned char)( n );
6613
6614 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6615 i += n; crt = crt->next;
6616 }
6617
6618 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6619 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6620 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6621
6622 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6624 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006625
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006626#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006627write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006628#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006629
6630 ssl->state++;
6631
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006632 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006633 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006634 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006635 return( ret );
6636 }
6637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006639
Paul Bakkered27a042013-04-18 22:46:23 +02006640 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006641}
6642
Hanno Becker84879e32019-01-31 07:44:03 +00006643#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006644
6645#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006646static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6647 unsigned char *crt_buf,
6648 size_t crt_buf_len )
6649{
6650 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6651
6652 if( peer_crt == NULL )
6653 return( -1 );
6654
6655 if( peer_crt->raw.len != crt_buf_len )
6656 return( -1 );
6657
Hanno Becker46f34d02019-02-08 14:00:04 +00006658 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006659}
Hanno Becker177475a2019-02-05 17:02:46 +00006660#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6661static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6662 unsigned char *crt_buf,
6663 size_t crt_buf_len )
6664{
6665 int ret;
6666 unsigned char const * const peer_cert_digest =
6667 ssl->session->peer_cert_digest;
6668 mbedtls_md_type_t const peer_cert_digest_type =
6669 ssl->session->peer_cert_digest_type;
6670 mbedtls_md_info_t const * const digest_info =
6671 mbedtls_md_info_from_type( peer_cert_digest_type );
6672 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6673 size_t digest_len;
6674
6675 if( peer_cert_digest == NULL || digest_info == NULL )
6676 return( -1 );
6677
6678 digest_len = mbedtls_md_get_size( digest_info );
6679 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6680 return( -1 );
6681
6682 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6683 if( ret != 0 )
6684 return( -1 );
6685
6686 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6687}
6688#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006689#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006690
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006691/*
6692 * Once the certificate message is read, parse it into a cert chain and
6693 * perform basic checks, but leave actual verification to the caller
6694 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006695static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6696 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006697{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006698 int ret;
6699#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6700 int crt_cnt=0;
6701#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006702 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006703 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006708 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6709 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006711 }
6712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6714 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006717 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6718 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006720 }
6721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006723
Paul Bakker5121ce52009-01-03 21:22:43 +00006724 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006726 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006727 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006728
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006729 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006733 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6734 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006736 }
6737
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006738 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6739 i += 3;
6740
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006741 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006742 while( i < ssl->in_hslen )
6743 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006744 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006745 if ( i + 3 > ssl->in_hslen ) {
6746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006747 mbedtls_ssl_send_alert_message( ssl,
6748 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6749 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006750 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6751 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006752 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6753 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006754 if( ssl->in_msg[i] != 0 )
6755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006757 mbedtls_ssl_send_alert_message( ssl,
6758 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6759 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006761 }
6762
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006763 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006764 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6765 | (unsigned int) ssl->in_msg[i + 2];
6766 i += 3;
6767
6768 if( n < 128 || i + n > ssl->in_hslen )
6769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006771 mbedtls_ssl_send_alert_message( ssl,
6772 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6773 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006775 }
6776
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006777 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006778#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6779 if( crt_cnt++ == 0 &&
6780 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6781 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006782 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006783 /* During client-side renegotiation, check that the server's
6784 * end-CRTs hasn't changed compared to the initial handshake,
6785 * mitigating the triple handshake attack. On success, reuse
6786 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006787 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6788 if( ssl_check_peer_crt_unchanged( ssl,
6789 &ssl->in_msg[i],
6790 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006791 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6793 mbedtls_ssl_send_alert_message( ssl,
6794 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6795 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6796 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006797 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006798
6799 /* Now we can safely free the original chain. */
6800 ssl_clear_peer_cert( ssl->session );
6801 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006802#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6803
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006804 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006805#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006806 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006807#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006808 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006809 * it in-place from the input buffer instead of making a copy. */
6810 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6811#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006812 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006813 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006814 case 0: /*ok*/
6815 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6816 /* Ignore certificate with an unknown algorithm: maybe a
6817 prior certificate was already trusted. */
6818 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006819
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006820 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6821 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6822 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006823
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006824 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6825 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6826 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006827
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006828 default:
6829 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6830 crt_parse_der_failed:
6831 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6832 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6833 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006834 }
6835
6836 i += n;
6837 }
6838
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006839 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006840 return( 0 );
6841}
6842
Hanno Becker4a55f632019-02-05 12:49:06 +00006843#if defined(MBEDTLS_SSL_SRV_C)
6844static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6845{
6846 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6847 return( -1 );
6848
6849#if defined(MBEDTLS_SSL_PROTO_SSL3)
6850 /*
6851 * Check if the client sent an empty certificate
6852 */
6853 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6854 {
6855 if( ssl->in_msglen == 2 &&
6856 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6857 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6858 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6859 {
6860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6861 return( 0 );
6862 }
6863
6864 return( -1 );
6865 }
6866#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6867
6868#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6869 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6870 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6871 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6872 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6873 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6874 {
6875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6876 return( 0 );
6877 }
6878
6879 return( -1 );
6880#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6881 MBEDTLS_SSL_PROTO_TLS1_2 */
6882}
6883#endif /* MBEDTLS_SSL_SRV_C */
6884
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006885/* Check if a certificate message is expected.
6886 * Return either
6887 * - SSL_CERTIFICATE_EXPECTED, or
6888 * - SSL_CERTIFICATE_SKIP
6889 * indicating whether a Certificate message is expected or not.
6890 */
6891#define SSL_CERTIFICATE_EXPECTED 0
6892#define SSL_CERTIFICATE_SKIP 1
6893static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6894 int authmode )
6895{
6896 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006897 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006898
6899 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6900 return( SSL_CERTIFICATE_SKIP );
6901
6902#if defined(MBEDTLS_SSL_SRV_C)
6903 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6904 {
6905 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6906 return( SSL_CERTIFICATE_SKIP );
6907
6908 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6909 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006910 ssl->session_negotiate->verify_result =
6911 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6912 return( SSL_CERTIFICATE_SKIP );
6913 }
6914 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006915#else
6916 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006917#endif /* MBEDTLS_SSL_SRV_C */
6918
6919 return( SSL_CERTIFICATE_EXPECTED );
6920}
6921
Hanno Becker68636192019-02-05 14:36:34 +00006922static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6923 int authmode,
6924 mbedtls_x509_crt *chain,
6925 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006926{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006927 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006928 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006929 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006930 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006931
Hanno Becker8927c832019-04-03 12:52:50 +01006932 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6933 void *p_vrfy;
6934
Hanno Becker68636192019-02-05 14:36:34 +00006935 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6936 return( 0 );
6937
Hanno Becker8927c832019-04-03 12:52:50 +01006938 if( ssl->f_vrfy != NULL )
6939 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006941 f_vrfy = ssl->f_vrfy;
6942 p_vrfy = ssl->p_vrfy;
6943 }
6944 else
6945 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006947 f_vrfy = ssl->conf->f_vrfy;
6948 p_vrfy = ssl->conf->p_vrfy;
6949 }
6950
Hanno Becker68636192019-02-05 14:36:34 +00006951 /*
6952 * Main check: verify certificate
6953 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006954#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6955 if( ssl->conf->f_ca_cb != NULL )
6956 {
6957 ((void) rs_ctx);
6958 have_ca_chain = 1;
6959
6960 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006961 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006962 chain,
6963 ssl->conf->f_ca_cb,
6964 ssl->conf->p_ca_cb,
6965 ssl->conf->cert_profile,
6966 ssl->hostname,
6967 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006968 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006969 }
6970 else
6971#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6972 {
6973 mbedtls_x509_crt *ca_chain;
6974 mbedtls_x509_crl *ca_crl;
6975
6976#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6977 if( ssl->handshake->sni_ca_chain != NULL )
6978 {
6979 ca_chain = ssl->handshake->sni_ca_chain;
6980 ca_crl = ssl->handshake->sni_ca_crl;
6981 }
6982 else
6983#endif
6984 {
6985 ca_chain = ssl->conf->ca_chain;
6986 ca_crl = ssl->conf->ca_crl;
6987 }
6988
6989 if( ca_chain != NULL )
6990 have_ca_chain = 1;
6991
6992 ret = mbedtls_x509_crt_verify_restartable(
6993 chain,
6994 ca_chain, ca_crl,
6995 ssl->conf->cert_profile,
6996 ssl->hostname,
6997 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006998 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006999 }
Hanno Becker68636192019-02-05 14:36:34 +00007000
7001 if( ret != 0 )
7002 {
7003 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7004 }
7005
7006#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7007 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7008 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7009#endif
7010
7011 /*
7012 * Secondary checks: always done, but change 'ret' only if it was 0
7013 */
7014
7015#if defined(MBEDTLS_ECP_C)
7016 {
7017 const mbedtls_pk_context *pk = &chain->pk;
7018
7019 /* If certificate uses an EC key, make sure the curve is OK */
7020 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7021 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7022 {
7023 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7024
7025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7026 if( ret == 0 )
7027 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7028 }
7029 }
7030#endif /* MBEDTLS_ECP_C */
7031
7032 if( mbedtls_ssl_check_cert_usage( chain,
7033 ciphersuite_info,
7034 ! ssl->conf->endpoint,
7035 &ssl->session_negotiate->verify_result ) != 0 )
7036 {
7037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7038 if( ret == 0 )
7039 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7040 }
7041
7042 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7043 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7044 * with details encoded in the verification flags. All other kinds
7045 * of error codes, including those from the user provided f_vrfy
7046 * functions, are treated as fatal and lead to a failure of
7047 * ssl_parse_certificate even if verification was optional. */
7048 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7049 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7050 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7051 {
7052 ret = 0;
7053 }
7054
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007055 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007056 {
7057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7058 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7059 }
7060
7061 if( ret != 0 )
7062 {
7063 uint8_t alert;
7064
7065 /* The certificate may have been rejected for several reasons.
7066 Pick one and send the corresponding alert. Which alert to send
7067 may be a subject of debate in some cases. */
7068 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7069 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7070 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7071 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7072 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7073 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7074 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7075 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7076 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7077 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7078 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7079 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7080 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7081 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7082 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7083 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7084 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7085 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7086 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7087 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7088 else
7089 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7090 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7091 alert );
7092 }
7093
7094#if defined(MBEDTLS_DEBUG_C)
7095 if( ssl->session_negotiate->verify_result != 0 )
7096 {
7097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7098 ssl->session_negotiate->verify_result ) );
7099 }
7100 else
7101 {
7102 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7103 }
7104#endif /* MBEDTLS_DEBUG_C */
7105
7106 return( ret );
7107}
7108
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007109#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7110static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7111 unsigned char *start, size_t len )
7112{
7113 int ret;
7114 /* Remember digest of the peer's end-CRT. */
7115 ssl->session_negotiate->peer_cert_digest =
7116 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7117 if( ssl->session_negotiate->peer_cert_digest == NULL )
7118 {
7119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7120 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7121 mbedtls_ssl_send_alert_message( ssl,
7122 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7123 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7124
7125 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7126 }
7127
7128 ret = mbedtls_md( mbedtls_md_info_from_type(
7129 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7130 start, len,
7131 ssl->session_negotiate->peer_cert_digest );
7132
7133 ssl->session_negotiate->peer_cert_digest_type =
7134 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7135 ssl->session_negotiate->peer_cert_digest_len =
7136 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7137
7138 return( ret );
7139}
7140
7141static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7142 unsigned char *start, size_t len )
7143{
7144 unsigned char *end = start + len;
7145 int ret;
7146
7147 /* Make a copy of the peer's raw public key. */
7148 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7149 ret = mbedtls_pk_parse_subpubkey( &start, end,
7150 &ssl->handshake->peer_pubkey );
7151 if( ret != 0 )
7152 {
7153 /* We should have parsed the public key before. */
7154 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7155 }
7156
7157 return( 0 );
7158}
7159#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7160
Hanno Becker68636192019-02-05 14:36:34 +00007161int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7162{
7163 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007164 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007165#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7166 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7167 ? ssl->handshake->sni_authmode
7168 : ssl->conf->authmode;
7169#else
7170 const int authmode = ssl->conf->authmode;
7171#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007172 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007173 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007174
7175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7176
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007177 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7178 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007179 {
7180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007181 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007182 }
7183
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007184#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7185 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007186 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007187 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007188 chain = ssl->handshake->ecrs_peer_cert;
7189 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007190 goto crt_verify;
7191 }
7192#endif
7193
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007194 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007195 {
7196 /* mbedtls_ssl_read_record may have sent an alert already. We
7197 let it decide whether to alert. */
7198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007199 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007200 }
7201
Hanno Becker4a55f632019-02-05 12:49:06 +00007202#if defined(MBEDTLS_SSL_SRV_C)
7203 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7204 {
7205 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007206
7207 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007208 ret = 0;
7209 else
7210 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007211
Hanno Becker6bdfab22019-02-05 13:11:17 +00007212 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007213 }
7214#endif /* MBEDTLS_SSL_SRV_C */
7215
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007216 /* Clear existing peer CRT structure in case we tried to
7217 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007218 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007219
7220 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7221 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007222 {
7223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7224 sizeof( mbedtls_x509_crt ) ) );
7225 mbedtls_ssl_send_alert_message( ssl,
7226 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7227 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007228
Hanno Becker3dad3112019-02-05 17:19:52 +00007229 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7230 goto exit;
7231 }
7232 mbedtls_x509_crt_init( chain );
7233
7234 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007235 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007236 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007237
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007238#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7239 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007240 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007241
7242crt_verify:
7243 if( ssl->handshake->ecrs_enabled)
7244 rs_ctx = &ssl->handshake->ecrs_ctx;
7245#endif
7246
Hanno Becker68636192019-02-05 14:36:34 +00007247 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007248 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007249 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007250 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007251
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007252#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007253 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007254 unsigned char *crt_start, *pk_start;
7255 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007256
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007257 /* We parse the CRT chain without copying, so
7258 * these pointers point into the input buffer,
7259 * and are hence still valid after freeing the
7260 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007261
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007262 crt_start = chain->raw.p;
7263 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007264
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007265 pk_start = chain->pk_raw.p;
7266 pk_len = chain->pk_raw.len;
7267
7268 /* Free the CRT structures before computing
7269 * digest and copying the peer's public key. */
7270 mbedtls_x509_crt_free( chain );
7271 mbedtls_free( chain );
7272 chain = NULL;
7273
7274 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007275 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007276 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007277
7278 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7279 if( ret != 0 )
7280 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007281 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007282#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7283 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007284 ssl->session_negotiate->peer_cert = chain;
7285 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007286#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007289
Hanno Becker6bdfab22019-02-05 13:11:17 +00007290exit:
7291
Hanno Becker3dad3112019-02-05 17:19:52 +00007292 if( ret == 0 )
7293 ssl->state++;
7294
7295#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7296 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7297 {
7298 ssl->handshake->ecrs_peer_cert = chain;
7299 chain = NULL;
7300 }
7301#endif
7302
7303 if( chain != NULL )
7304 {
7305 mbedtls_x509_crt_free( chain );
7306 mbedtls_free( chain );
7307 }
7308
Paul Bakker5121ce52009-01-03 21:22:43 +00007309 return( ret );
7310}
Hanno Becker21489932019-02-05 13:20:55 +00007311#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007314{
7315 int ret;
7316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007320 ssl->out_msglen = 1;
7321 ssl->out_msg[0] = 1;
7322
Paul Bakker5121ce52009-01-03 21:22:43 +00007323 ssl->state++;
7324
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007325 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007326 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007328 return( ret );
7329 }
7330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007332
7333 return( 0 );
7334}
7335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007337{
7338 int ret;
7339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007341
Hanno Becker327c93b2018-08-15 13:56:18 +01007342 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007345 return( ret );
7346 }
7347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007351 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7352 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007354 }
7355
Hanno Beckere678eaa2018-08-21 14:57:46 +01007356 /* CCS records are only accepted if they have length 1 and content '1',
7357 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007358
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007359 /*
7360 * Switch to our negotiated transform and session parameters for inbound
7361 * data.
7362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007364 ssl->transform_in = ssl->transform_negotiate;
7365 ssl->session_in = ssl->session_negotiate;
7366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007368 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007371 ssl_dtls_replay_reset( ssl );
7372#endif
7373
7374 /* Increment epoch */
7375 if( ++ssl->in_epoch == 0 )
7376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007378 /* This is highly unlikely to happen for legitimate reasons, so
7379 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007381 }
7382 }
7383 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007385 memset( ssl->in_ctr, 0, 8 );
7386
Hanno Becker79594fd2019-05-08 09:38:41 +01007387 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7390 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007395 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7396 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007398 }
7399 }
7400#endif
7401
Paul Bakker5121ce52009-01-03 21:22:43 +00007402 ssl->state++;
7403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007405
7406 return( 0 );
7407}
7408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7410 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007411{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007412 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7415 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7416 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007417 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007418 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007419#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7421#if defined(MBEDTLS_SHA512_C)
7422 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007423 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7424 else
7425#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426#if defined(MBEDTLS_SHA256_C)
7427 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007428 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007429 else
7430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007434 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007435 }
Paul Bakker380da532012-04-18 16:10:25 +00007436}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007439{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7441 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007442 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7443 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007444#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7446#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007447#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007448 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007449 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7450#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007451 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007452#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007453#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007455#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007456 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007457 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007458#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007459 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007460#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007461#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007463}
7464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007466 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007467{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7469 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007470 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7471 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007472#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7474#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007475#if defined(MBEDTLS_USE_PSA_CRYPTO)
7476 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7477#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007478 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007479#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007482#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007483 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007484#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007485 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007486#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007487#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007489}
7490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7492 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7493static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007494 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007495{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007496 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7497 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007498}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007499#endif
Paul Bakker380da532012-04-18 16:10:25 +00007500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7502#if defined(MBEDTLS_SHA256_C)
7503static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007504 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007505{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007506#if defined(MBEDTLS_USE_PSA_CRYPTO)
7507 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7508#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007509 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007510#endif
Paul Bakker380da532012-04-18 16:10:25 +00007511}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007512#endif
Paul Bakker380da532012-04-18 16:10:25 +00007513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514#if defined(MBEDTLS_SHA512_C)
7515static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007516 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007517{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007518#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007519 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007520#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007521 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007522#endif
Paul Bakker380da532012-04-18 16:10:25 +00007523}
Paul Bakker769075d2012-11-24 11:26:46 +01007524#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007528static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007530{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007531 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007532 mbedtls_md5_context md5;
7533 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007534
Paul Bakker5121ce52009-01-03 21:22:43 +00007535 unsigned char padbuf[48];
7536 unsigned char md5sum[16];
7537 unsigned char sha1sum[20];
7538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007540 if( !session )
7541 session = ssl->session;
7542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007544
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007545 mbedtls_md5_init( &md5 );
7546 mbedtls_sha1_init( &sha1 );
7547
7548 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7549 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007550
7551 /*
7552 * SSLv3:
7553 * hash =
7554 * MD5( master + pad2 +
7555 * MD5( handshake + sender + master + pad1 ) )
7556 * + SHA1( master + pad2 +
7557 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007558 */
7559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007561 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7562 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007563#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007566 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7567 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007568#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007571 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007572
Paul Bakker1ef83d62012-04-11 12:09:53 +00007573 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007574
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007575 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7576 mbedtls_md5_update_ret( &md5, session->master, 48 );
7577 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7578 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007579
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007580 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7581 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7582 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7583 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007584
Paul Bakker1ef83d62012-04-11 12:09:53 +00007585 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007586
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007587 mbedtls_md5_starts_ret( &md5 );
7588 mbedtls_md5_update_ret( &md5, session->master, 48 );
7589 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7590 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7591 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007592
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007593 mbedtls_sha1_starts_ret( &sha1 );
7594 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7595 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7596 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7597 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007600
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007601 mbedtls_md5_free( &md5 );
7602 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007603
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007604 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7605 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7606 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007609}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007613static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007615{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007616 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007617 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007618 mbedtls_md5_context md5;
7619 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007620 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007623 if( !session )
7624 session = ssl->session;
7625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007627
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007628 mbedtls_md5_init( &md5 );
7629 mbedtls_sha1_init( &sha1 );
7630
7631 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7632 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007633
Paul Bakker1ef83d62012-04-11 12:09:53 +00007634 /*
7635 * TLSv1:
7636 * hash = PRF( master, finished_label,
7637 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7638 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007641 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7642 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007643#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007646 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7647 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007648#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007651 ? "client finished"
7652 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007653
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007654 mbedtls_md5_finish_ret( &md5, padbuf );
7655 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007656
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007657 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007658 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007661
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007662 mbedtls_md5_free( &md5 );
7663 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007664
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007665 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007668}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7672#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007673static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007675{
7676 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007677 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007678 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007679#if defined(MBEDTLS_USE_PSA_CRYPTO)
7680 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007681 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007682 psa_status_t status;
7683#else
7684 mbedtls_sha256_context sha256;
7685#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007688 if( !session )
7689 session = ssl->session;
7690
Andrzej Kurekeb342242019-01-29 09:14:33 -05007691 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7692 ? "client finished"
7693 : "server finished";
7694
7695#if defined(MBEDTLS_USE_PSA_CRYPTO)
7696 sha256_psa = psa_hash_operation_init();
7697
7698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7699
7700 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7701 if( status != PSA_SUCCESS )
7702 {
7703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7704 return;
7705 }
7706
7707 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7708 if( status != PSA_SUCCESS )
7709 {
7710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7711 return;
7712 }
7713 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7714#else
7715
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007716 mbedtls_sha256_init( &sha256 );
7717
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007719
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007720 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007721
7722 /*
7723 * TLSv1.2:
7724 * hash = PRF( master, finished_label,
7725 * Hash( handshake ) )[0.11]
7726 */
7727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728#if !defined(MBEDTLS_SHA256_ALT)
7729 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007730 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007731#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007732
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007733 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007734 mbedtls_sha256_free( &sha256 );
7735#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007736
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007737 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007738 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007741
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007742 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007745}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007746#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007749static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007751{
7752 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007753 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007754 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007755#if defined(MBEDTLS_USE_PSA_CRYPTO)
7756 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007757 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007758 psa_status_t status;
7759#else
7760 mbedtls_sha512_context sha512;
7761#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007764 if( !session )
7765 session = ssl->session;
7766
Andrzej Kurekeb342242019-01-29 09:14:33 -05007767 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7768 ? "client finished"
7769 : "server finished";
7770
7771#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007772 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007773
7774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7775
Andrzej Kurek972fba52019-01-30 03:29:12 -05007776 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007777 if( status != PSA_SUCCESS )
7778 {
7779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7780 return;
7781 }
7782
Andrzej Kurek972fba52019-01-30 03:29:12 -05007783 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007784 if( status != PSA_SUCCESS )
7785 {
7786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7787 return;
7788 }
7789 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7790#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007791 mbedtls_sha512_init( &sha512 );
7792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007794
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007795 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007796
7797 /*
7798 * TLSv1.2:
7799 * hash = PRF( master, finished_label,
7800 * Hash( handshake ) )[0.11]
7801 */
7802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007804 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7805 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007806#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007807
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007808 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007809 mbedtls_sha512_free( &sha512 );
7810#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007811
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007812 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007813 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007816
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007817 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007820}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821#endif /* MBEDTLS_SHA512_C */
7822#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007827
7828 /*
7829 * Free our handshake params
7830 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007831 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007833 ssl->handshake = NULL;
7834
7835 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007836 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007837 */
7838 if( ssl->transform )
7839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 mbedtls_ssl_transform_free( ssl->transform );
7841 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007842 }
7843 ssl->transform = ssl->transform_negotiate;
7844 ssl->transform_negotiate = NULL;
7845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007847}
7848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007850{
7851 int resume = ssl->handshake->resume;
7852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855#if defined(MBEDTLS_SSL_RENEGOTIATION)
7856 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007859 ssl->renego_records_seen = 0;
7860 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007861#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007862
7863 /*
7864 * Free the previous session and switch in the current one
7865 */
Paul Bakker0a597072012-09-25 21:55:46 +00007866 if( ssl->session )
7867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007869 /* RFC 7366 3.1: keep the EtM state */
7870 ssl->session_negotiate->encrypt_then_mac =
7871 ssl->session->encrypt_then_mac;
7872#endif
7873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 mbedtls_ssl_session_free( ssl->session );
7875 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007876 }
7877 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007878 ssl->session_negotiate = NULL;
7879
Paul Bakker0a597072012-09-25 21:55:46 +00007880 /*
7881 * Add cache entry
7882 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007883 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007884 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007885 resume == 0 )
7886 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007887 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007889 }
Paul Bakker0a597072012-09-25 21:55:46 +00007890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007892 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007893 ssl->handshake->flight != NULL )
7894 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007895 /* Cancel handshake timer */
7896 ssl_set_timer( ssl, 0 );
7897
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007898 /* Keep last flight around in case we need to resend it:
7899 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007901 }
7902 else
7903#endif
7904 ssl_handshake_wrapup_free_hs_transform( ssl );
7905
Paul Bakker48916f92012-09-16 19:57:18 +00007906 ssl->state++;
7907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007909}
7910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007912{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007913 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007916
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007917 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007918
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007919 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007920
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007921 /*
7922 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7923 * may define some other value. Currently (early 2016), no defined
7924 * ciphersuite does this (and this is unlikely to change as activity has
7925 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007930 ssl->verify_data_len = hash_len;
7931 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007932#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007933
Paul Bakker5121ce52009-01-03 21:22:43 +00007934 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7936 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007937
7938 /*
7939 * In case of session resuming, invert the client and server
7940 * ChangeCipherSpec messages order.
7941 */
Paul Bakker0a597072012-09-25 21:55:46 +00007942 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007945 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007949 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007951#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007952 }
7953 else
7954 ssl->state++;
7955
Paul Bakker48916f92012-09-16 19:57:18 +00007956 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007957 * Switch to our negotiated transform and session parameters for outbound
7958 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007959 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007963 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007964 {
7965 unsigned char i;
7966
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007967 /* Remember current epoch settings for resending */
7968 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007969 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007970
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007971 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007972 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007973
7974 /* Increment epoch */
7975 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007976 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007977 break;
7978
7979 /* The loop goes to its end iff the counter is wrapping */
7980 if( i == 0 )
7981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7983 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007984 }
7985 }
7986 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007988 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007989
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007990 ssl->transform_out = ssl->transform_negotiate;
7991 ssl->session_out = ssl->session_negotiate;
7992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7994 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7999 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008000 }
8001 }
8002#endif
8003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008005 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008007#endif
8008
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008009 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008010 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008012 return( ret );
8013 }
8014
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008015#if defined(MBEDTLS_SSL_PROTO_DTLS)
8016 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8017 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8018 {
8019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8020 return( ret );
8021 }
8022#endif
8023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008025
8026 return( 0 );
8027}
8028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008030#define SSL_MAX_HASH_LEN 36
8031#else
8032#define SSL_MAX_HASH_LEN 12
8033#endif
8034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008036{
Paul Bakker23986e52011-04-24 08:57:21 +00008037 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008038 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008039 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008042
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008043 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008044
Hanno Becker327c93b2018-08-15 13:56:18 +01008045 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008048 return( ret );
8049 }
8050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8055 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008057 }
8058
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008059 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060#if defined(MBEDTLS_SSL_PROTO_SSL3)
8061 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008062 hash_len = 36;
8063 else
8064#endif
8065 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8068 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008071 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8072 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008073 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008074 }
8075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008077 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008080 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8081 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008082 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008083 }
8084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008086 ssl->verify_data_len = hash_len;
8087 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008088#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008089
Paul Bakker0a597072012-09-25 21:55:46 +00008090 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008093 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008095#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008097 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008099#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008100 }
8101 else
8102 ssl->state++;
8103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008105 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008107#endif
8108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008110
8111 return( 0 );
8112}
8113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8119 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8120 mbedtls_md5_init( &handshake->fin_md5 );
8121 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008122 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8123 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008124#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8126#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008127#if defined(MBEDTLS_USE_PSA_CRYPTO)
8128 handshake->fin_sha256_psa = psa_hash_operation_init();
8129 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8130#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008132 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008133#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008134#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008136#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008137 handshake->fin_sha384_psa = psa_hash_operation_init();
8138 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008139#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008141 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008142#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008145
8146 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008147
8148#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8149 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8150 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8151#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008153#if defined(MBEDTLS_DHM_C)
8154 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156#if defined(MBEDTLS_ECDH_C)
8157 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008158#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008159#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008160 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008161#if defined(MBEDTLS_SSL_CLI_C)
8162 handshake->ecjpake_cache = NULL;
8163 handshake->ecjpake_cache_len = 0;
8164#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008165#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008166
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008167#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008168 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008169#endif
8170
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008171#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8172 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8173#endif
Hanno Becker75173122019-02-06 16:18:31 +00008174
8175#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8176 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8177 mbedtls_pk_init( &handshake->peer_pubkey );
8178#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008179}
8180
Hanno Beckera18d1322018-01-03 14:27:32 +00008181void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008182{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8186 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008187
Hanno Beckerd56ed242018-01-03 15:32:51 +00008188#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189 mbedtls_md_init( &transform->md_ctx_enc );
8190 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008191#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008192}
8193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008195{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008197}
8198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008200{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008201 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008202 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008204 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008206 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008207 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008208
8209 /*
8210 * Either the pointers are now NULL or cleared properly and can be freed.
8211 * Now allocate missing structures.
8212 */
8213 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008214 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008215 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008216 }
Paul Bakker48916f92012-09-16 19:57:18 +00008217
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008218 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008219 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008220 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008221 }
Paul Bakker48916f92012-09-16 19:57:18 +00008222
Paul Bakker82788fb2014-10-20 13:59:19 +02008223 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008224 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008225 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008226 }
Paul Bakker48916f92012-09-16 19:57:18 +00008227
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008228 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008229 if( ssl->handshake == NULL ||
8230 ssl->transform_negotiate == NULL ||
8231 ssl->session_negotiate == NULL )
8232 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235 mbedtls_free( ssl->handshake );
8236 mbedtls_free( ssl->transform_negotiate );
8237 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008238
8239 ssl->handshake = NULL;
8240 ssl->transform_negotiate = NULL;
8241 ssl->session_negotiate = NULL;
8242
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008243 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008244 }
8245
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008246 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008248 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008249 ssl_handshake_params_init( ssl->handshake );
8250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008252 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8253 {
8254 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008255
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008256 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8257 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8258 else
8259 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008260
8261 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008262 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008263#endif
8264
Paul Bakker48916f92012-09-16 19:57:18 +00008265 return( 0 );
8266}
8267
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008268#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008269/* Dummy cookie callbacks for defaults */
8270static int ssl_cookie_write_dummy( void *ctx,
8271 unsigned char **p, unsigned char *end,
8272 const unsigned char *cli_id, size_t cli_id_len )
8273{
8274 ((void) ctx);
8275 ((void) p);
8276 ((void) end);
8277 ((void) cli_id);
8278 ((void) cli_id_len);
8279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008281}
8282
8283static int ssl_cookie_check_dummy( void *ctx,
8284 const unsigned char *cookie, size_t cookie_len,
8285 const unsigned char *cli_id, size_t cli_id_len )
8286{
8287 ((void) ctx);
8288 ((void) cookie);
8289 ((void) cookie_len);
8290 ((void) cli_id);
8291 ((void) cli_id_len);
8292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008294}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008295#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008296
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008297/* Once ssl->out_hdr as the address of the beginning of the
8298 * next outgoing record is set, deduce the other pointers.
8299 *
8300 * Note: For TLS, we save the implicit record sequence number
8301 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8302 * and the caller has to make sure there's space for this.
8303 */
8304
8305static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8306 mbedtls_ssl_transform *transform )
8307{
8308#if defined(MBEDTLS_SSL_PROTO_DTLS)
8309 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8310 {
8311 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008312#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008313 ssl->out_cid = ssl->out_ctr + 8;
8314 ssl->out_len = ssl->out_cid;
8315 if( transform != NULL )
8316 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008317#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008318 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008319#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008320 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008321 }
8322 else
8323#endif
8324 {
8325 ssl->out_ctr = ssl->out_hdr - 8;
8326 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008327#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008328 ssl->out_cid = ssl->out_len;
8329#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008330 ssl->out_iv = ssl->out_hdr + 5;
8331 }
8332
8333 /* Adjust out_msg to make space for explicit IV, if used. */
8334 if( transform != NULL &&
8335 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8336 {
8337 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8338 }
8339 else
8340 ssl->out_msg = ssl->out_iv;
8341}
8342
8343/* Once ssl->in_hdr as the address of the beginning of the
8344 * next incoming record is set, deduce the other pointers.
8345 *
8346 * Note: For TLS, we save the implicit record sequence number
8347 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8348 * and the caller has to make sure there's space for this.
8349 */
8350
Hanno Becker79594fd2019-05-08 09:38:41 +01008351static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008352{
Hanno Becker79594fd2019-05-08 09:38:41 +01008353 /* This function sets the pointers to match the case
8354 * of unprotected TLS/DTLS records, with both ssl->in_iv
8355 * and ssl->in_msg pointing to the beginning of the record
8356 * content.
8357 *
8358 * When decrypting a protected record, ssl->in_msg
8359 * will be shifted to point to the beginning of the
8360 * record plaintext.
8361 */
8362
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008363#if defined(MBEDTLS_SSL_PROTO_DTLS)
8364 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8365 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008366 /* This sets the header pointers to match records
8367 * without CID. When we receive a record containing
8368 * a CID, the fields are shifted accordingly in
8369 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008370 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008371#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008372 ssl->in_cid = ssl->in_ctr + 8;
8373 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008374#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008375 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008376#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008377 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008378 }
8379 else
8380#endif
8381 {
8382 ssl->in_ctr = ssl->in_hdr - 8;
8383 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008384#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008385 ssl->in_cid = ssl->in_len;
8386#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008387 ssl->in_iv = ssl->in_hdr + 5;
8388 }
8389
Hanno Becker79594fd2019-05-08 09:38:41 +01008390 /* This will be adjusted at record decryption time. */
8391 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008392}
8393
Paul Bakker5121ce52009-01-03 21:22:43 +00008394/*
8395 * Initialize an SSL context
8396 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008397void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8398{
8399 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8400}
8401
8402/*
8403 * Setup an SSL context
8404 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008405
8406static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8407{
8408 /* Set the incoming and outgoing record pointers. */
8409#if defined(MBEDTLS_SSL_PROTO_DTLS)
8410 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8411 {
8412 ssl->out_hdr = ssl->out_buf;
8413 ssl->in_hdr = ssl->in_buf;
8414 }
8415 else
8416#endif /* MBEDTLS_SSL_PROTO_DTLS */
8417 {
8418 ssl->out_hdr = ssl->out_buf + 8;
8419 ssl->in_hdr = ssl->in_buf + 8;
8420 }
8421
8422 /* Derive other internal pointers. */
8423 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008424 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008425}
8426
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008427int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008428 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008429{
Paul Bakker48916f92012-09-16 19:57:18 +00008430 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008431
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008432 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008433
8434 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008435 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008436 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008437
8438 /* Set to NULL in case of an error condition */
8439 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008440
Angus Grattond8213d02016-05-25 20:56:48 +10008441 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8442 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008443 {
Angus Grattond8213d02016-05-25 20:56:48 +10008444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008445 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008446 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008447 }
8448
8449 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8450 if( ssl->out_buf == NULL )
8451 {
8452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008453 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008454 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008455 }
8456
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008457 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008458
Paul Bakker48916f92012-09-16 19:57:18 +00008459 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008460 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008461
8462 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008463
8464error:
8465 mbedtls_free( ssl->in_buf );
8466 mbedtls_free( ssl->out_buf );
8467
8468 ssl->conf = NULL;
8469
8470 ssl->in_buf = NULL;
8471 ssl->out_buf = NULL;
8472
8473 ssl->in_hdr = NULL;
8474 ssl->in_ctr = NULL;
8475 ssl->in_len = NULL;
8476 ssl->in_iv = NULL;
8477 ssl->in_msg = NULL;
8478
8479 ssl->out_hdr = NULL;
8480 ssl->out_ctr = NULL;
8481 ssl->out_len = NULL;
8482 ssl->out_iv = NULL;
8483 ssl->out_msg = NULL;
8484
k-stachowiak9f7798e2018-07-31 16:52:32 +02008485 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008486}
8487
8488/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008489 * Reset an initialized and used SSL context for re-use while retaining
8490 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008491 *
8492 * If partial is non-zero, keep data in the input buffer and client ID.
8493 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008494 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008495static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008496{
Paul Bakker48916f92012-09-16 19:57:18 +00008497 int ret;
8498
Hanno Becker7e772132018-08-10 12:38:21 +01008499#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8500 !defined(MBEDTLS_SSL_SRV_C)
8501 ((void) partial);
8502#endif
8503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008504 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008505
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008506 /* Cancel any possibly running timer */
8507 ssl_set_timer( ssl, 0 );
8508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509#if defined(MBEDTLS_SSL_RENEGOTIATION)
8510 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008511 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008512
8513 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008514 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8515 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008516#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008517 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008518
Paul Bakker7eb013f2011-10-06 12:37:39 +00008519 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008520 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008521
8522 ssl->in_msgtype = 0;
8523 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008524#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008525 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008526 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008527#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008528#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008529 ssl_dtls_replay_reset( ssl );
8530#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008531
8532 ssl->in_hslen = 0;
8533 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008534
8535 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008536
8537 ssl->out_msgtype = 0;
8538 ssl->out_msglen = 0;
8539 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8541 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008542 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008543#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008544
Hanno Becker19859472018-08-06 09:40:20 +01008545 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8546
Paul Bakker48916f92012-09-16 19:57:18 +00008547 ssl->transform_in = NULL;
8548 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008549
Hanno Becker78640902018-08-13 16:35:15 +01008550 ssl->session_in = NULL;
8551 ssl->session_out = NULL;
8552
Angus Grattond8213d02016-05-25 20:56:48 +10008553 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008554
8555#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008556 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008557#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8558 {
8559 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008560 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008561 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008563#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8564 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8567 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8570 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008571 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008572 }
8573#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008574
Paul Bakker48916f92012-09-16 19:57:18 +00008575 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008577 mbedtls_ssl_transform_free( ssl->transform );
8578 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008579 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008580 }
Paul Bakker48916f92012-09-16 19:57:18 +00008581
Paul Bakkerc0463502013-02-14 11:19:38 +01008582 if( ssl->session )
8583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584 mbedtls_ssl_session_free( ssl->session );
8585 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008586 ssl->session = NULL;
8587 }
8588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008590 ssl->alpn_chosen = NULL;
8591#endif
8592
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008593#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008594#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008595 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008596#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008597 {
8598 mbedtls_free( ssl->cli_id );
8599 ssl->cli_id = NULL;
8600 ssl->cli_id_len = 0;
8601 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008602#endif
8603
Paul Bakker48916f92012-09-16 19:57:18 +00008604 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8605 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008606
8607 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008608}
8609
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008610/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008611 * Reset an initialized and used SSL context for re-use while retaining
8612 * all application-set variables, function pointers and data.
8613 */
8614int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8615{
8616 return( ssl_session_reset_int( ssl, 0 ) );
8617}
8618
8619/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008620 * SSL set accessors
8621 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008622void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008623{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008624 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008625}
8626
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008627void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008628{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008629 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008630}
8631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008633void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008634{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008635 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008636}
8637#endif
8638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008640void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008641{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008642 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008643}
8644#endif
8645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008647
Hanno Becker1841b0a2018-08-24 11:13:57 +01008648void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8649 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008650{
8651 ssl->disable_datagram_packing = !allow_packing;
8652}
8653
8654void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8655 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008656{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008657 conf->hs_timeout_min = min;
8658 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008659}
8660#endif
8661
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008662void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008663{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008664 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008665}
8666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008668void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008669 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008670 void *p_vrfy )
8671{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008672 conf->f_vrfy = f_vrfy;
8673 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008674}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008676
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008677void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008678 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008679 void *p_rng )
8680{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008681 conf->f_rng = f_rng;
8682 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008683}
8684
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008685void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008686 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008687 void *p_dbg )
8688{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008689 conf->f_dbg = f_dbg;
8690 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008691}
8692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008693void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008694 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008695 mbedtls_ssl_send_t *f_send,
8696 mbedtls_ssl_recv_t *f_recv,
8697 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008698{
8699 ssl->p_bio = p_bio;
8700 ssl->f_send = f_send;
8701 ssl->f_recv = f_recv;
8702 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008703}
8704
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008705#if defined(MBEDTLS_SSL_PROTO_DTLS)
8706void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8707{
8708 ssl->mtu = mtu;
8709}
8710#endif
8711
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008712void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008713{
8714 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008715}
8716
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008717void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8718 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008719 mbedtls_ssl_set_timer_t *f_set_timer,
8720 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008721{
8722 ssl->p_timer = p_timer;
8723 ssl->f_set_timer = f_set_timer;
8724 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008725
8726 /* Make sure we start with no timer running */
8727 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008728}
8729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008731void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008732 void *p_cache,
8733 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8734 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008735{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008736 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008737 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008738 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008739}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008742#if defined(MBEDTLS_SSL_CLI_C)
8743int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008744{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008745 int ret;
8746
8747 if( ssl == NULL ||
8748 session == NULL ||
8749 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008750 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008753 }
8754
Hanno Becker52055ae2019-02-06 14:30:46 +00008755 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8756 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008757 return( ret );
8758
Paul Bakker0a597072012-09-25 21:55:46 +00008759 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008760
8761 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008764
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008765void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008766 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008767{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008768 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8769 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8770 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8771 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008772}
8773
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008774void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008775 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008776 int major, int minor )
8777{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008779 return;
8780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008782 return;
8783
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008784 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008785}
8786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008788void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008789 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008790{
8791 conf->cert_profile = profile;
8792}
8793
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008794/* Append a new keycert entry to a (possibly empty) list */
8795static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8796 mbedtls_x509_crt *cert,
8797 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008798{
niisato8ee24222018-06-25 19:05:48 +09008799 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008800
niisato8ee24222018-06-25 19:05:48 +09008801 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8802 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008803 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008804
niisato8ee24222018-06-25 19:05:48 +09008805 new_cert->cert = cert;
8806 new_cert->key = key;
8807 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008808
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008809 /* Update head is the list was null, else add to the end */
8810 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008811 {
niisato8ee24222018-06-25 19:05:48 +09008812 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008813 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008814 else
8815 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008816 mbedtls_ssl_key_cert *cur = *head;
8817 while( cur->next != NULL )
8818 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008819 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008820 }
8821
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008822 return( 0 );
8823}
8824
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008825int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008826 mbedtls_x509_crt *own_cert,
8827 mbedtls_pk_context *pk_key )
8828{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008829 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008830}
8831
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008832void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008833 mbedtls_x509_crt *ca_chain,
8834 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008835{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008836 conf->ca_chain = ca_chain;
8837 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008838
8839#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8840 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8841 * cannot be used together. */
8842 conf->f_ca_cb = NULL;
8843 conf->p_ca_cb = NULL;
8844#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008845}
Hanno Becker5adaad92019-03-27 16:54:37 +00008846
8847#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8848void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008849 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008850 void *p_ca_cb )
8851{
8852 conf->f_ca_cb = f_ca_cb;
8853 conf->p_ca_cb = p_ca_cb;
8854
8855 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8856 * cannot be used together. */
8857 conf->ca_chain = NULL;
8858 conf->ca_crl = NULL;
8859}
8860#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008861#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008862
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008863#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8864int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8865 mbedtls_x509_crt *own_cert,
8866 mbedtls_pk_context *pk_key )
8867{
8868 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8869 own_cert, pk_key ) );
8870}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008871
8872void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8873 mbedtls_x509_crt *ca_chain,
8874 mbedtls_x509_crl *ca_crl )
8875{
8876 ssl->handshake->sni_ca_chain = ca_chain;
8877 ssl->handshake->sni_ca_crl = ca_crl;
8878}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008879
8880void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8881 int authmode )
8882{
8883 ssl->handshake->sni_authmode = authmode;
8884}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008885#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8886
Hanno Becker8927c832019-04-03 12:52:50 +01008887#if defined(MBEDTLS_X509_CRT_PARSE_C)
8888void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8889 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8890 void *p_vrfy )
8891{
8892 ssl->f_vrfy = f_vrfy;
8893 ssl->p_vrfy = p_vrfy;
8894}
8895#endif
8896
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008897#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008898/*
8899 * Set EC J-PAKE password for current handshake
8900 */
8901int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8902 const unsigned char *pw,
8903 size_t pw_len )
8904{
8905 mbedtls_ecjpake_role role;
8906
Janos Follath8eb64132016-06-03 15:40:57 +01008907 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8909
8910 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8911 role = MBEDTLS_ECJPAKE_SERVER;
8912 else
8913 role = MBEDTLS_ECJPAKE_CLIENT;
8914
8915 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8916 role,
8917 MBEDTLS_MD_SHA256,
8918 MBEDTLS_ECP_DP_SECP256R1,
8919 pw, pw_len ) );
8920}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008921#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008924
8925static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8926{
8927 /* Remove reference to existing PSK, if any. */
8928#if defined(MBEDTLS_USE_PSA_CRYPTO)
8929 if( conf->psk_opaque != 0 )
8930 {
8931 /* The maintenance of the PSK key slot is the
8932 * user's responsibility. */
8933 conf->psk_opaque = 0;
8934 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008935 /* This and the following branch should never
8936 * be taken simultaenously as we maintain the
8937 * invariant that raw and opaque PSKs are never
8938 * configured simultaneously. As a safeguard,
8939 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008940#endif /* MBEDTLS_USE_PSA_CRYPTO */
8941 if( conf->psk != NULL )
8942 {
8943 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8944
8945 mbedtls_free( conf->psk );
8946 conf->psk = NULL;
8947 conf->psk_len = 0;
8948 }
8949
8950 /* Remove reference to PSK identity, if any. */
8951 if( conf->psk_identity != NULL )
8952 {
8953 mbedtls_free( conf->psk_identity );
8954 conf->psk_identity = NULL;
8955 conf->psk_identity_len = 0;
8956 }
8957}
8958
Hanno Becker7390c712018-11-15 13:33:04 +00008959/* This function assumes that PSK identity in the SSL config is unset.
8960 * It checks that the provided identity is well-formed and attempts
8961 * to make a copy of it in the SSL config.
8962 * On failure, the PSK identity in the config remains unset. */
8963static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8964 unsigned char const *psk_identity,
8965 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008966{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008967 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008968 if( psk_identity == NULL ||
8969 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008970 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008971 {
8972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8973 }
8974
Hanno Becker7390c712018-11-15 13:33:04 +00008975 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8976 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008977 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008978
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008979 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008980 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008981
8982 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008983}
8984
Hanno Becker7390c712018-11-15 13:33:04 +00008985int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8986 const unsigned char *psk, size_t psk_len,
8987 const unsigned char *psk_identity, size_t psk_identity_len )
8988{
8989 int ret;
8990 /* Remove opaque/raw PSK + PSK Identity */
8991 ssl_conf_remove_psk( conf );
8992
8993 /* Check and set raw PSK */
8994 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8995 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8996 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8997 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8998 conf->psk_len = psk_len;
8999 memcpy( conf->psk, psk, conf->psk_len );
9000
9001 /* Check and set PSK Identity */
9002 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9003 if( ret != 0 )
9004 ssl_conf_remove_psk( conf );
9005
9006 return( ret );
9007}
9008
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009009static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9010{
9011#if defined(MBEDTLS_USE_PSA_CRYPTO)
9012 if( ssl->handshake->psk_opaque != 0 )
9013 {
9014 ssl->handshake->psk_opaque = 0;
9015 }
9016 else
9017#endif /* MBEDTLS_USE_PSA_CRYPTO */
9018 if( ssl->handshake->psk != NULL )
9019 {
9020 mbedtls_platform_zeroize( ssl->handshake->psk,
9021 ssl->handshake->psk_len );
9022 mbedtls_free( ssl->handshake->psk );
9023 ssl->handshake->psk_len = 0;
9024 }
9025}
9026
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009027int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9028 const unsigned char *psk, size_t psk_len )
9029{
9030 if( psk == NULL || ssl->handshake == NULL )
9031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9032
9033 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9035
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009036 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009037
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009038 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009039 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009040
9041 ssl->handshake->psk_len = psk_len;
9042 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9043
9044 return( 0 );
9045}
9046
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009047#if defined(MBEDTLS_USE_PSA_CRYPTO)
9048int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009049 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009050 const unsigned char *psk_identity,
9051 size_t psk_identity_len )
9052{
Hanno Becker7390c712018-11-15 13:33:04 +00009053 int ret;
9054 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009055 ssl_conf_remove_psk( conf );
9056
Hanno Becker7390c712018-11-15 13:33:04 +00009057 /* Check and set opaque PSK */
9058 if( psk_slot == 0 )
9059 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009060 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009061
9062 /* Check and set PSK Identity */
9063 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9064 psk_identity_len );
9065 if( ret != 0 )
9066 ssl_conf_remove_psk( conf );
9067
9068 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009069}
9070
9071int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009072 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009073{
9074 if( psk_slot == 0 || ssl->handshake == NULL )
9075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9076
9077 ssl_remove_psk( ssl );
9078 ssl->handshake->psk_opaque = psk_slot;
9079 return( 0 );
9080}
9081#endif /* MBEDTLS_USE_PSA_CRYPTO */
9082
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009083void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009085 size_t),
9086 void *p_psk )
9087{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009088 conf->f_psk = f_psk;
9089 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009090}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009092
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009093#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009094
9095#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009096int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009097{
9098 int ret;
9099
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009100 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9101 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9102 {
9103 mbedtls_mpi_free( &conf->dhm_P );
9104 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009105 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009106 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009107
9108 return( 0 );
9109}
Hanno Becker470a8c42017-10-04 15:28:46 +01009110#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009111
Hanno Beckera90658f2017-10-04 15:29:08 +01009112int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9113 const unsigned char *dhm_P, size_t P_len,
9114 const unsigned char *dhm_G, size_t G_len )
9115{
9116 int ret;
9117
9118 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9119 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9120 {
9121 mbedtls_mpi_free( &conf->dhm_P );
9122 mbedtls_mpi_free( &conf->dhm_G );
9123 return( ret );
9124 }
9125
9126 return( 0 );
9127}
Paul Bakker5121ce52009-01-03 21:22:43 +00009128
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009129int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009130{
9131 int ret;
9132
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009133 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9134 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9135 {
9136 mbedtls_mpi_free( &conf->dhm_P );
9137 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009138 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009139 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009140
9141 return( 0 );
9142}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009143#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009144
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009145#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9146/*
9147 * Set the minimum length for Diffie-Hellman parameters
9148 */
9149void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9150 unsigned int bitlen )
9151{
9152 conf->dhm_min_bitlen = bitlen;
9153}
9154#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9155
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009156#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009157/*
9158 * Set allowed/preferred hashes for handshake signatures
9159 */
9160void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9161 const int *hashes )
9162{
9163 conf->sig_hashes = hashes;
9164}
Hanno Becker947194e2017-04-07 13:25:49 +01009165#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009166
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009167#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009168/*
9169 * Set the allowed elliptic curves
9170 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009171void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009172 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009173{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009174 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009175}
Hanno Becker947194e2017-04-07 13:25:49 +01009176#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009177
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009178#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009180{
Hanno Becker947194e2017-04-07 13:25:49 +01009181 /* Initialize to suppress unnecessary compiler warning */
9182 size_t hostname_len = 0;
9183
9184 /* Check if new hostname is valid before
9185 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009186 if( hostname != NULL )
9187 {
9188 hostname_len = strlen( hostname );
9189
9190 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9192 }
9193
9194 /* Now it's clear that we will overwrite the old hostname,
9195 * so we can free it safely */
9196
9197 if( ssl->hostname != NULL )
9198 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009199 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009200 mbedtls_free( ssl->hostname );
9201 }
9202
9203 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009204
Paul Bakker5121ce52009-01-03 21:22:43 +00009205 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009206 {
9207 ssl->hostname = NULL;
9208 }
9209 else
9210 {
9211 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009212 if( ssl->hostname == NULL )
9213 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009214
Hanno Becker947194e2017-04-07 13:25:49 +01009215 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009216
Hanno Becker947194e2017-04-07 13:25:49 +01009217 ssl->hostname[hostname_len] = '\0';
9218 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009219
9220 return( 0 );
9221}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009222#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009223
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009224#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009225void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009226 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009227 const unsigned char *, size_t),
9228 void *p_sni )
9229{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009230 conf->f_sni = f_sni;
9231 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009233#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009235#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009236int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009237{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009238 size_t cur_len, tot_len;
9239 const char **p;
9240
9241 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009242 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9243 * MUST NOT be truncated."
9244 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009245 */
9246 tot_len = 0;
9247 for( p = protos; *p != NULL; p++ )
9248 {
9249 cur_len = strlen( *p );
9250 tot_len += cur_len;
9251
9252 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009254 }
9255
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009256 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009257
9258 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009259}
9260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009262{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009263 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009264}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009266
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009267void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009268{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009269 conf->max_major_ver = major;
9270 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009271}
9272
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009273void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009274{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009275 conf->min_major_ver = major;
9276 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009277}
9278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009280void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009281{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009282 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009283}
9284#endif
9285
Janos Follath088ce432017-04-10 12:42:31 +01009286#if defined(MBEDTLS_SSL_SRV_C)
9287void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9288 char cert_req_ca_list )
9289{
9290 conf->cert_req_ca_list = cert_req_ca_list;
9291}
9292#endif
9293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009295void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009296{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009297 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009298}
9299#endif
9300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009302void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009303{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009304 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009305}
9306#endif
9307
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009308#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009309void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009310{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009311 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009312}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009313#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009316int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009317{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009319 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009322 }
9323
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009324 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009325
9326 return( 0 );
9327}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009331void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009332{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009333 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009338void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009339{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009340 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009341}
9342#endif
9343
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009344void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009345{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009346 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009347}
9348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009350void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009351{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009352 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009353}
9354
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009355void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009356{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009357 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009358}
9359
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009360void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009361 const unsigned char period[8] )
9362{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009363 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009368#if defined(MBEDTLS_SSL_CLI_C)
9369void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009370{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009371 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009372}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009373#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009374
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009375#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009376void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9377 mbedtls_ssl_ticket_write_t *f_ticket_write,
9378 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9379 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009380{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009381 conf->f_ticket_write = f_ticket_write;
9382 conf->f_ticket_parse = f_ticket_parse;
9383 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009384}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009385#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009387
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009388#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9389void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9390 mbedtls_ssl_export_keys_t *f_export_keys,
9391 void *p_export_keys )
9392{
9393 conf->f_export_keys = f_export_keys;
9394 conf->p_export_keys = p_export_keys;
9395}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009396
9397void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9398 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9399 void *p_export_keys )
9400{
9401 conf->f_export_keys_ext = f_export_keys_ext;
9402 conf->p_export_keys = p_export_keys;
9403}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009404#endif
9405
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009406#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009407void mbedtls_ssl_conf_async_private_cb(
9408 mbedtls_ssl_config *conf,
9409 mbedtls_ssl_async_sign_t *f_async_sign,
9410 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9411 mbedtls_ssl_async_resume_t *f_async_resume,
9412 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009413 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009414{
9415 conf->f_async_sign_start = f_async_sign;
9416 conf->f_async_decrypt_start = f_async_decrypt;
9417 conf->f_async_resume = f_async_resume;
9418 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009419 conf->p_async_config_data = async_config_data;
9420}
9421
Gilles Peskine8f97af72018-04-26 11:46:10 +02009422void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9423{
9424 return( conf->p_async_config_data );
9425}
9426
Gilles Peskine1febfef2018-04-30 11:54:39 +02009427void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009428{
9429 if( ssl->handshake == NULL )
9430 return( NULL );
9431 else
9432 return( ssl->handshake->user_async_ctx );
9433}
9434
Gilles Peskine1febfef2018-04-30 11:54:39 +02009435void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009436 void *ctx )
9437{
9438 if( ssl->handshake != NULL )
9439 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009440}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009441#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009442
Paul Bakker5121ce52009-01-03 21:22:43 +00009443/*
9444 * SSL get accessors
9445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009447{
9448 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9449}
9450
Hanno Becker8b170a02017-10-10 11:51:19 +01009451int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9452{
9453 /*
9454 * Case A: We're currently holding back
9455 * a message for further processing.
9456 */
9457
9458 if( ssl->keep_current_message == 1 )
9459 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009461 return( 1 );
9462 }
9463
9464 /*
9465 * Case B: Further records are pending in the current datagram.
9466 */
9467
9468#if defined(MBEDTLS_SSL_PROTO_DTLS)
9469 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9470 ssl->in_left > ssl->next_record_offset )
9471 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009473 return( 1 );
9474 }
9475#endif /* MBEDTLS_SSL_PROTO_DTLS */
9476
9477 /*
9478 * Case C: A handshake message is being processed.
9479 */
9480
Hanno Becker8b170a02017-10-10 11:51:19 +01009481 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9482 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009484 return( 1 );
9485 }
9486
9487 /*
9488 * Case D: An application data message is being processed
9489 */
9490 if( ssl->in_offt != NULL )
9491 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009493 return( 1 );
9494 }
9495
9496 /*
9497 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009498 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009499 * we implement support for multiple alerts in single records.
9500 */
9501
9502 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9503 return( 0 );
9504}
9505
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009506uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009507{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009508 if( ssl->session != NULL )
9509 return( ssl->session->verify_result );
9510
9511 if( ssl->session_negotiate != NULL )
9512 return( ssl->session_negotiate->verify_result );
9513
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009514 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009515}
9516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009518{
Paul Bakker926c8e42013-03-06 10:23:34 +01009519 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009520 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009523}
9524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009526{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009528 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009529 {
9530 switch( ssl->minor_ver )
9531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009533 return( "DTLSv1.0" );
9534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009536 return( "DTLSv1.2" );
9537
9538 default:
9539 return( "unknown (DTLS)" );
9540 }
9541 }
9542#endif
9543
Paul Bakker43ca69c2011-01-15 17:35:19 +00009544 switch( ssl->minor_ver )
9545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009547 return( "SSLv3.0" );
9548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009550 return( "TLSv1.0" );
9551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009553 return( "TLSv1.1" );
9554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009556 return( "TLSv1.2" );
9557
Paul Bakker43ca69c2011-01-15 17:35:19 +00009558 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009559 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009560 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009561}
9562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009564{
Hanno Becker3136ede2018-08-17 15:28:19 +01009565 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009567 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009568
Hanno Becker5903de42019-05-03 14:46:38 +01009569 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9570
Hanno Becker78640902018-08-13 16:35:15 +01009571 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009572 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574#if defined(MBEDTLS_ZLIB_SUPPORT)
9575 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9576 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009577#endif
9578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581 case MBEDTLS_MODE_GCM:
9582 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009583 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009585 transform_expansion = transform->minlen;
9586 break;
9587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009589
9590 block_size = mbedtls_cipher_get_block_size(
9591 &transform->cipher_ctx_enc );
9592
Hanno Becker3136ede2018-08-17 15:28:19 +01009593 /* Expansion due to the addition of the MAC. */
9594 transform_expansion += transform->maclen;
9595
9596 /* Expansion due to the addition of CBC padding;
9597 * Theoretically up to 256 bytes, but we never use
9598 * more than the block size of the underlying cipher. */
9599 transform_expansion += block_size;
9600
9601 /* For TLS 1.1 or higher, an explicit IV is added
9602 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009603#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9604 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009605 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009606#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009607
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009608 break;
9609
9610 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009613 }
9614
Hanno Beckera0e20d02019-05-15 14:03:01 +01009615#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009616 if( transform->out_cid_len != 0 )
9617 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009618#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009619
Hanno Becker5903de42019-05-03 14:46:38 +01009620 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009621}
9622
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009623#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9624size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9625{
9626 size_t max_len;
9627
9628 /*
9629 * Assume mfl_code is correct since it was checked when set
9630 */
Angus Grattond8213d02016-05-25 20:56:48 +10009631 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009632
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009633 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009634 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009635 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009636 {
Angus Grattond8213d02016-05-25 20:56:48 +10009637 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009638 }
9639
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009640 /* During a handshake, use the value being negotiated */
9641 if( ssl->session_negotiate != NULL &&
9642 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9643 {
9644 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9645 }
9646
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009647 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009648}
9649#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9650
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009651#if defined(MBEDTLS_SSL_PROTO_DTLS)
9652static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9653{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009654 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9655 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9656 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9657 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9658 return ( 0 );
9659
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009660 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9661 return( ssl->mtu );
9662
9663 if( ssl->mtu == 0 )
9664 return( ssl->handshake->mtu );
9665
9666 return( ssl->mtu < ssl->handshake->mtu ?
9667 ssl->mtu : ssl->handshake->mtu );
9668}
9669#endif /* MBEDTLS_SSL_PROTO_DTLS */
9670
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009671int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9672{
9673 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9674
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009675#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9676 !defined(MBEDTLS_SSL_PROTO_DTLS)
9677 (void) ssl;
9678#endif
9679
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009680#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9681 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9682
9683 if( max_len > mfl )
9684 max_len = mfl;
9685#endif
9686
9687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009688 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009689 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009690 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009691 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9692 const size_t overhead = (size_t) ret;
9693
9694 if( ret < 0 )
9695 return( ret );
9696
9697 if( mtu <= overhead )
9698 {
9699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9700 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9701 }
9702
9703 if( max_len > mtu - overhead )
9704 max_len = mtu - overhead;
9705 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009706#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009707
Hanno Becker0defedb2018-08-10 12:35:02 +01009708#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9709 !defined(MBEDTLS_SSL_PROTO_DTLS)
9710 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009711#endif
9712
9713 return( (int) max_len );
9714}
9715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716#if defined(MBEDTLS_X509_CRT_PARSE_C)
9717const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009718{
9719 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009720 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009721
Hanno Beckere6824572019-02-07 13:18:46 +00009722#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009723 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009724#else
9725 return( NULL );
9726#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009727}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009730#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009731int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9732 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009733{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009734 if( ssl == NULL ||
9735 dst == NULL ||
9736 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009737 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009740 }
9741
Hanno Becker52055ae2019-02-06 14:30:46 +00009742 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009745
Paul Bakker5121ce52009-01-03 21:22:43 +00009746/*
Paul Bakker1961b702013-01-25 14:49:24 +01009747 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009752
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009753 if( ssl == NULL || ssl->conf == NULL )
9754 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009757 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009758 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009759#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009761 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009763#endif
9764
Paul Bakker1961b702013-01-25 14:49:24 +01009765 return( ret );
9766}
9767
9768/*
9769 * Perform the SSL handshake
9770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009772{
9773 int ret = 0;
9774
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009775 if( ssl == NULL || ssl->conf == NULL )
9776 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009782 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009783
9784 if( ret != 0 )
9785 break;
9786 }
9787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009789
9790 return( ret );
9791}
9792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793#if defined(MBEDTLS_SSL_RENEGOTIATION)
9794#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009795/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009796 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009799{
9800 int ret;
9801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009803
9804 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9806 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009807
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009808 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009809 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009811 return( ret );
9812 }
9813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009815
9816 return( 0 );
9817}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009819
9820/*
9821 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 * - any side: calling mbedtls_ssl_renegotiate(),
9823 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9824 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009825 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009826 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009830{
9831 int ret;
9832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009834
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009835 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9836 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009837
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009838 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9839 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009841 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009843 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009844 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009845 ssl->handshake->out_msg_seq = 1;
9846 else
9847 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009848 }
9849#endif
9850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9852 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009857 return( ret );
9858 }
9859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009861
9862 return( 0 );
9863}
9864
9865/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009866 * Renegotiate current connection on client,
9867 * or request renegotiation on server
9868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009870{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009872
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009873 if( ssl == NULL || ssl->conf == NULL )
9874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009876#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009877 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009878 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9881 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009884
9885 /* Did we already try/start sending HelloRequest? */
9886 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009888
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009889 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009890 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009894 /*
9895 * On client, either start the renegotiation process or,
9896 * if already in progress, continue the handshake
9897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009898 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9901 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009902
9903 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009906 return( ret );
9907 }
9908 }
9909 else
9910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009914 return( ret );
9915 }
9916 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009918
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009919 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009920}
9921
9922/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009923 * Check record counters and renegotiate if they're above the limit.
9924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009925static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009926{
Andres AG2196c7f2016-12-15 17:01:16 +00009927 size_t ep_len = ssl_ep_len( ssl );
9928 int in_ctr_cmp;
9929 int out_ctr_cmp;
9930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9932 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009933 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009934 {
9935 return( 0 );
9936 }
9937
Andres AG2196c7f2016-12-15 17:01:16 +00009938 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9939 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009940 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009941 ssl->conf->renego_period + ep_len, 8 - ep_len );
9942
9943 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009944 {
9945 return( 0 );
9946 }
9947
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009950}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009951#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009952
9953/*
9954 * Receive application data decrypted from the SSL layer
9955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009957{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009958 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009959 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009960
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009961 if( ssl == NULL || ssl->conf == NULL )
9962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009966#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009967 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009970 return( ret );
9971
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009972 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009974 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009975 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009976 return( ret );
9977 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009978 }
9979#endif
9980
Hanno Becker4a810fb2017-05-24 16:27:30 +01009981 /*
9982 * Check if renegotiation is necessary and/or handshake is
9983 * in process. If yes, perform/continue, and fall through
9984 * if an unexpected packet is received while the client
9985 * is waiting for the ServerHello.
9986 *
9987 * (There is no equivalent to the last condition on
9988 * the server-side as it is not treated as within
9989 * a handshake while waiting for the ClientHello
9990 * after a renegotiation request.)
9991 */
9992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009993#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009994 ret = ssl_check_ctr_renegotiate( ssl );
9995 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9996 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009999 return( ret );
10000 }
10001#endif
10002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010006 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10007 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010010 return( ret );
10011 }
10012 }
10013
Hanno Beckere41158b2017-10-23 13:30:32 +010010014 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010015 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010016 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010017 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010018 if( ssl->f_get_timer != NULL &&
10019 ssl->f_get_timer( ssl->p_timer ) == -1 )
10020 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010021 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010022 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010023
Hanno Becker327c93b2018-08-15 13:56:18 +010010024 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010025 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010026 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10027 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010028
Hanno Becker4a810fb2017-05-24 16:27:30 +010010029 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10030 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010031 }
10032
10033 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010035 {
10036 /*
10037 * OpenSSL sends empty messages to randomize the IV
10038 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010039 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010041 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010042 return( 0 );
10043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010044 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010045 return( ret );
10046 }
10047 }
10048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010049 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010052
Hanno Becker4a810fb2017-05-24 16:27:30 +010010053 /*
10054 * - For client-side, expect SERVER_HELLO_REQUEST.
10055 * - For server-side, expect CLIENT_HELLO.
10056 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10057 */
10058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010059#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010060 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010062 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010065
10066 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010067#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010068 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010069 {
10070 continue;
10071 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010073 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010074 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010075#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010076
Hanno Becker4a810fb2017-05-24 16:27:30 +010010077#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010078 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010079 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010082
10083 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010085 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010086 {
10087 continue;
10088 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010089#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010091 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010092#endif /* MBEDTLS_SSL_SRV_C */
10093
Hanno Becker21df7f92017-10-17 11:03:26 +010010094#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010095 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010096 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10097 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10098 ssl->conf->allow_legacy_renegotiation ==
10099 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10100 {
10101 /*
10102 * Accept renegotiation request
10103 */
Paul Bakker48916f92012-09-16 19:57:18 +000010104
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010105 /* DTLS clients need to know renego is server-initiated */
10106#if defined(MBEDTLS_SSL_PROTO_DTLS)
10107 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10108 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10109 {
10110 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10111 }
10112#endif
10113 ret = ssl_start_renegotiation( ssl );
10114 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10115 ret != 0 )
10116 {
10117 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10118 return( ret );
10119 }
10120 }
10121 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010122#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010123 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010124 /*
10125 * Refuse renegotiation
10126 */
10127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130#if defined(MBEDTLS_SSL_PROTO_SSL3)
10131 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010132 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010133 /* SSLv3 does not have a "no_renegotiation" warning, so
10134 we send a fatal alert and abort the connection. */
10135 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10136 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10137 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010138 }
10139 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010140#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10141#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10142 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10143 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010145 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10146 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10147 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010148 {
10149 return( ret );
10150 }
Paul Bakker48916f92012-09-16 19:57:18 +000010151 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010152 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010153#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10154 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10157 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010158 }
Paul Bakker48916f92012-09-16 19:57:18 +000010159 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010160
Hanno Becker90333da2017-10-10 11:27:13 +010010161 /* At this point, we don't know whether the renegotiation has been
10162 * completed or not. The cases to consider are the following:
10163 * 1) The renegotiation is complete. In this case, no new record
10164 * has been read yet.
10165 * 2) The renegotiation is incomplete because the client received
10166 * an application data record while awaiting the ServerHello.
10167 * 3) The renegotiation is incomplete because the client received
10168 * a non-handshake, non-application data message while awaiting
10169 * the ServerHello.
10170 * In each of these case, looping will be the proper action:
10171 * - For 1), the next iteration will read a new record and check
10172 * if it's application data.
10173 * - For 2), the loop condition isn't satisfied as application data
10174 * is present, hence continue is the same as break
10175 * - For 3), the loop condition is satisfied and read_record
10176 * will re-deliver the message that was held back by the client
10177 * when expecting the ServerHello.
10178 */
10179 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010180 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010181#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010182 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010183 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010184 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010185 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010186 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010189 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010190 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010191 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010192 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010193 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010194#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10197 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010200 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010201 }
10202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10206 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010207 }
10208
10209 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010210
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010211 /* We're going to return something now, cancel timer,
10212 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010213 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010214 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010215
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010216#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010217 /* If we requested renego but received AppData, resend HelloRequest.
10218 * Do it now, after setting in_offt, to avoid taking this branch
10219 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010221 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010223 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010224 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010227 return( ret );
10228 }
10229 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010231#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010232 }
10233
10234 n = ( len < ssl->in_msglen )
10235 ? len : ssl->in_msglen;
10236
10237 memcpy( buf, ssl->in_offt, n );
10238 ssl->in_msglen -= n;
10239
10240 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010241 {
10242 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010243 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010244 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010245 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010246 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010247 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010248 /* more data available */
10249 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010250 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010253
Paul Bakker23986e52011-04-24 08:57:21 +000010254 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010255}
10256
10257/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010258 * Send application data to be encrypted by the SSL layer, taking care of max
10259 * fragment length and buffer size.
10260 *
10261 * According to RFC 5246 Section 6.2.1:
10262 *
10263 * Zero-length fragments of Application data MAY be sent as they are
10264 * potentially useful as a traffic analysis countermeasure.
10265 *
10266 * Therefore, it is possible that the input message length is 0 and the
10267 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010268 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010269static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010270 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010271{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010272 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10273 const size_t max_len = (size_t) ret;
10274
10275 if( ret < 0 )
10276 {
10277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10278 return( ret );
10279 }
10280
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010281 if( len > max_len )
10282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010284 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010287 "maximum fragment length: %d > %d",
10288 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010290 }
10291 else
10292#endif
10293 len = max_len;
10294 }
Paul Bakker887bd502011-06-08 13:10:54 +000010295
Paul Bakker5121ce52009-01-03 21:22:43 +000010296 if( ssl->out_left != 0 )
10297 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010298 /*
10299 * The user has previously tried to send the data and
10300 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10301 * written. In this case, we expect the high-level write function
10302 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010304 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010307 return( ret );
10308 }
10309 }
Paul Bakker887bd502011-06-08 13:10:54 +000010310 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010311 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010312 /*
10313 * The user is trying to send a message the first time, so we need to
10314 * copy the data into the internal buffers and setup the data structure
10315 * to keep track of partial writes
10316 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010317 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010319 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010320
Hanno Becker67bc7c32018-08-06 11:33:50 +010010321 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010324 return( ret );
10325 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010326 }
10327
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010328 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010329}
10330
10331/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010332 * Write application data, doing 1/n-1 splitting if necessary.
10333 *
10334 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010335 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010336 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010338#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010339static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010340 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010341{
10342 int ret;
10343
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010344 if( ssl->conf->cbc_record_splitting ==
10345 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010346 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10348 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10349 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010350 {
10351 return( ssl_write_real( ssl, buf, len ) );
10352 }
10353
10354 if( ssl->split_done == 0 )
10355 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010356 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010357 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010358 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010359 }
10360
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010361 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10362 return( ret );
10363 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010364
10365 return( ret + 1 );
10366}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010368
10369/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010370 * Write application data (public-facing wrapper)
10371 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010372int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010373{
10374 int ret;
10375
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010377
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010378 if( ssl == NULL || ssl->conf == NULL )
10379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10380
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010381#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010382 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10383 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010384 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010385 return( ret );
10386 }
10387#endif
10388
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010389 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010390 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010391 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010392 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010394 return( ret );
10395 }
10396 }
10397
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010398#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010399 ret = ssl_write_split( ssl, buf, len );
10400#else
10401 ret = ssl_write_real( ssl, buf, len );
10402#endif
10403
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010405
10406 return( ret );
10407}
10408
10409/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010410 * Notify the peer that the connection is being closed
10411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010413{
10414 int ret;
10415
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010416 if( ssl == NULL || ssl->conf == NULL )
10417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010420
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010421 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010422 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010426 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10427 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10428 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010431 return( ret );
10432 }
10433 }
10434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010436
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010437 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010438}
10439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010440void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010441{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010442 if( transform == NULL )
10443 return;
10444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010445#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010446 deflateEnd( &transform->ctx_deflate );
10447 inflateEnd( &transform->ctx_inflate );
10448#endif
10449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10451 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010452
Hanno Beckerd56ed242018-01-03 15:32:51 +000010453#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010454 mbedtls_md_free( &transform->md_ctx_enc );
10455 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010456#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010457
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010458 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010459}
10460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461#if defined(MBEDTLS_X509_CRT_PARSE_C)
10462static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010464 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010465
10466 while( cur != NULL )
10467 {
10468 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010469 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010470 cur = next;
10471 }
10472}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010474
Hanno Becker0271f962018-08-16 13:23:47 +010010475#if defined(MBEDTLS_SSL_PROTO_DTLS)
10476
10477static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10478{
10479 unsigned offset;
10480 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10481
10482 if( hs == NULL )
10483 return;
10484
Hanno Becker283f5ef2018-08-24 09:34:47 +010010485 ssl_free_buffered_record( ssl );
10486
Hanno Becker0271f962018-08-16 13:23:47 +010010487 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010488 ssl_buffering_free_slot( ssl, offset );
10489}
10490
10491static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10492 uint8_t slot )
10493{
10494 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10495 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010496
10497 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10498 return;
10499
Hanno Beckere605b192018-08-21 15:59:07 +010010500 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010501 {
Hanno Beckere605b192018-08-21 15:59:07 +010010502 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010503 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010504 mbedtls_free( hs_buf->data );
10505 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010506 }
10507}
10508
10509#endif /* MBEDTLS_SSL_PROTO_DTLS */
10510
Gilles Peskine9b562d52018-04-25 20:32:43 +020010511void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010512{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010513 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10514
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010515 if( handshake == NULL )
10516 return;
10517
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010518#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10519 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10520 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010521 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010522 handshake->async_in_progress = 0;
10523 }
10524#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10525
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010526#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10527 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10528 mbedtls_md5_free( &handshake->fin_md5 );
10529 mbedtls_sha1_free( &handshake->fin_sha1 );
10530#endif
10531#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10532#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010533#if defined(MBEDTLS_USE_PSA_CRYPTO)
10534 psa_hash_abort( &handshake->fin_sha256_psa );
10535#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010536 mbedtls_sha256_free( &handshake->fin_sha256 );
10537#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010538#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010539#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010540#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010541 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010542#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010543 mbedtls_sha512_free( &handshake->fin_sha512 );
10544#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010545#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010546#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010548#if defined(MBEDTLS_DHM_C)
10549 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010550#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010551#if defined(MBEDTLS_ECDH_C)
10552 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010553#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010554#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010555 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010556#if defined(MBEDTLS_SSL_CLI_C)
10557 mbedtls_free( handshake->ecjpake_cache );
10558 handshake->ecjpake_cache = NULL;
10559 handshake->ecjpake_cache_len = 0;
10560#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010561#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010562
Janos Follath4ae5c292016-02-10 11:27:43 +000010563#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10564 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010565 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010566 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010567#endif
10568
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010569#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10570 if( handshake->psk != NULL )
10571 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010572 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010573 mbedtls_free( handshake->psk );
10574 }
10575#endif
10576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010577#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10578 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010579 /*
10580 * Free only the linked list wrapper, not the keys themselves
10581 * since the belong to the SNI callback
10582 */
10583 if( handshake->sni_key_cert != NULL )
10584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010585 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010586
10587 while( cur != NULL )
10588 {
10589 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010590 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010591 cur = next;
10592 }
10593 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010594#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010595
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010596#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010597 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010598 if( handshake->ecrs_peer_cert != NULL )
10599 {
10600 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10601 mbedtls_free( handshake->ecrs_peer_cert );
10602 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010603#endif
10604
Hanno Becker75173122019-02-06 16:18:31 +000010605#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10606 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10607 mbedtls_pk_free( &handshake->peer_pubkey );
10608#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010610#if defined(MBEDTLS_SSL_PROTO_DTLS)
10611 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010612 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010613 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010614#endif
10615
Hanno Becker4a63ed42019-01-08 11:39:35 +000010616#if defined(MBEDTLS_ECDH_C) && \
10617 defined(MBEDTLS_USE_PSA_CRYPTO)
10618 psa_destroy_key( handshake->ecdh_psa_privkey );
10619#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10620
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010621 mbedtls_platform_zeroize( handshake,
10622 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010623}
10624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010625void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010626{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010627 if( session == NULL )
10628 return;
10629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010631 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010632#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010633
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010634#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010636#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010637
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010638 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010639}
10640
Paul Bakker5121ce52009-01-03 21:22:43 +000010641/*
10642 * Free an SSL context
10643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010645{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010646 if( ssl == NULL )
10647 return;
10648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010650
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010651 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010652 {
Angus Grattond8213d02016-05-25 20:56:48 +100010653 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010655 }
10656
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010657 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010658 {
Angus Grattond8213d02016-05-25 20:56:48 +100010659 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010660 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010661 }
10662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010663#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010664 if( ssl->compress_buf != NULL )
10665 {
Angus Grattond8213d02016-05-25 20:56:48 +100010666 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010667 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010668 }
10669#endif
10670
Paul Bakker48916f92012-09-16 19:57:18 +000010671 if( ssl->transform )
10672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010673 mbedtls_ssl_transform_free( ssl->transform );
10674 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010675 }
10676
10677 if( ssl->handshake )
10678 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010679 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010680 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10681 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683 mbedtls_free( ssl->handshake );
10684 mbedtls_free( ssl->transform_negotiate );
10685 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010686 }
10687
Paul Bakkerc0463502013-02-14 11:19:38 +010010688 if( ssl->session )
10689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690 mbedtls_ssl_session_free( ssl->session );
10691 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010692 }
10693
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010694#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010695 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010696 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010697 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010699 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010700#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10703 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10706 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010707 }
10708#endif
10709
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010710#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010711 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010712#endif
10713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010715
Paul Bakker86f04f42013-02-14 11:20:09 +010010716 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010717 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010718}
10719
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010720/*
10721 * Initialze mbedtls_ssl_config
10722 */
10723void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10724{
10725 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10726}
10727
Simon Butcherc97b6972015-12-27 23:48:17 +000010728#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010729static int ssl_preset_default_hashes[] = {
10730#if defined(MBEDTLS_SHA512_C)
10731 MBEDTLS_MD_SHA512,
10732 MBEDTLS_MD_SHA384,
10733#endif
10734#if defined(MBEDTLS_SHA256_C)
10735 MBEDTLS_MD_SHA256,
10736 MBEDTLS_MD_SHA224,
10737#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010738#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010739 MBEDTLS_MD_SHA1,
10740#endif
10741 MBEDTLS_MD_NONE
10742};
Simon Butcherc97b6972015-12-27 23:48:17 +000010743#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010744
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010745static int ssl_preset_suiteb_ciphersuites[] = {
10746 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10747 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10748 0
10749};
10750
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010751#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010752static int ssl_preset_suiteb_hashes[] = {
10753 MBEDTLS_MD_SHA256,
10754 MBEDTLS_MD_SHA384,
10755 MBEDTLS_MD_NONE
10756};
10757#endif
10758
10759#if defined(MBEDTLS_ECP_C)
10760static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010761#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010762 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010763#endif
10764#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010765 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010766#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010767 MBEDTLS_ECP_DP_NONE
10768};
10769#endif
10770
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010771/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010772 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010773 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010774int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010775 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010776{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010777#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010778 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010779#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010780
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010781 /* Use the functions here so that they are covered in tests,
10782 * but otherwise access member directly for efficiency */
10783 mbedtls_ssl_conf_endpoint( conf, endpoint );
10784 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010785
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010786 /*
10787 * Things that are common to all presets
10788 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010789#if defined(MBEDTLS_SSL_CLI_C)
10790 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10791 {
10792 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10793#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10794 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10795#endif
10796 }
10797#endif
10798
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010799#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010800 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010801#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010802
10803#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10804 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10805#endif
10806
10807#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10808 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10809#endif
10810
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010811#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10812 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10813#endif
10814
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010815#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010816 conf->f_cookie_write = ssl_cookie_write_dummy;
10817 conf->f_cookie_check = ssl_cookie_check_dummy;
10818#endif
10819
10820#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10821 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10822#endif
10823
Janos Follath088ce432017-04-10 12:42:31 +010010824#if defined(MBEDTLS_SSL_SRV_C)
10825 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10826#endif
10827
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010828#if defined(MBEDTLS_SSL_PROTO_DTLS)
10829 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10830 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10831#endif
10832
10833#if defined(MBEDTLS_SSL_RENEGOTIATION)
10834 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010835 memset( conf->renego_period, 0x00, 2 );
10836 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010837#endif
10838
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010839#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10840 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10841 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010842 const unsigned char dhm_p[] =
10843 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10844 const unsigned char dhm_g[] =
10845 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10846
Hanno Beckera90658f2017-10-04 15:29:08 +010010847 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10848 dhm_p, sizeof( dhm_p ),
10849 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010850 {
10851 return( ret );
10852 }
10853 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010854#endif
10855
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010856 /*
10857 * Preset-specific defaults
10858 */
10859 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010860 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010861 /*
10862 * NSA Suite B
10863 */
10864 case MBEDTLS_SSL_PRESET_SUITEB:
10865 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10866 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10867 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10868 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10869
10870 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10871 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10872 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10873 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10874 ssl_preset_suiteb_ciphersuites;
10875
10876#if defined(MBEDTLS_X509_CRT_PARSE_C)
10877 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010878#endif
10879
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010880#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010881 conf->sig_hashes = ssl_preset_suiteb_hashes;
10882#endif
10883
10884#if defined(MBEDTLS_ECP_C)
10885 conf->curve_list = ssl_preset_suiteb_curves;
10886#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010887 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010888
10889 /*
10890 * Default
10891 */
10892 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010893 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10894 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10895 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10896 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10897 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10898 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10899 MBEDTLS_SSL_MIN_MINOR_VERSION :
10900 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010901 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10902 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10903
10904#if defined(MBEDTLS_SSL_PROTO_DTLS)
10905 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10906 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10907#endif
10908
10909 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10910 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10911 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10912 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10913 mbedtls_ssl_list_ciphersuites();
10914
10915#if defined(MBEDTLS_X509_CRT_PARSE_C)
10916 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10917#endif
10918
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010919#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010920 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010921#endif
10922
10923#if defined(MBEDTLS_ECP_C)
10924 conf->curve_list = mbedtls_ecp_grp_id_list();
10925#endif
10926
10927#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10928 conf->dhm_min_bitlen = 1024;
10929#endif
10930 }
10931
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010932 return( 0 );
10933}
10934
10935/*
10936 * Free mbedtls_ssl_config
10937 */
10938void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10939{
10940#if defined(MBEDTLS_DHM_C)
10941 mbedtls_mpi_free( &conf->dhm_P );
10942 mbedtls_mpi_free( &conf->dhm_G );
10943#endif
10944
10945#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10946 if( conf->psk != NULL )
10947 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010948 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010949 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010950 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010951 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010952 }
10953
10954 if( conf->psk_identity != NULL )
10955 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010956 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010957 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010958 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010959 conf->psk_identity_len = 0;
10960 }
10961#endif
10962
10963#if defined(MBEDTLS_X509_CRT_PARSE_C)
10964 ssl_key_cert_free( conf->key_cert );
10965#endif
10966
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010967 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010968}
10969
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010970#if defined(MBEDTLS_PK_C) && \
10971 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010972/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010973 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010975unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010976{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010977#if defined(MBEDTLS_RSA_C)
10978 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10979 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010980#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010981#if defined(MBEDTLS_ECDSA_C)
10982 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10983 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010984#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010985 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010986}
10987
Hanno Becker7e5437a2017-04-28 17:15:26 +010010988unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10989{
10990 switch( type ) {
10991 case MBEDTLS_PK_RSA:
10992 return( MBEDTLS_SSL_SIG_RSA );
10993 case MBEDTLS_PK_ECDSA:
10994 case MBEDTLS_PK_ECKEY:
10995 return( MBEDTLS_SSL_SIG_ECDSA );
10996 default:
10997 return( MBEDTLS_SSL_SIG_ANON );
10998 }
10999}
11000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011001mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011002{
11003 switch( sig )
11004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011005#if defined(MBEDTLS_RSA_C)
11006 case MBEDTLS_SSL_SIG_RSA:
11007 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011008#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011009#if defined(MBEDTLS_ECDSA_C)
11010 case MBEDTLS_SSL_SIG_ECDSA:
11011 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011012#endif
11013 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011014 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011015 }
11016}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011017#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011018
Hanno Becker7e5437a2017-04-28 17:15:26 +010011019#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11020 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11021
11022/* Find an entry in a signature-hash set matching a given hash algorithm. */
11023mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11024 mbedtls_pk_type_t sig_alg )
11025{
11026 switch( sig_alg )
11027 {
11028 case MBEDTLS_PK_RSA:
11029 return( set->rsa );
11030 case MBEDTLS_PK_ECDSA:
11031 return( set->ecdsa );
11032 default:
11033 return( MBEDTLS_MD_NONE );
11034 }
11035}
11036
11037/* Add a signature-hash-pair to a signature-hash set */
11038void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11039 mbedtls_pk_type_t sig_alg,
11040 mbedtls_md_type_t md_alg )
11041{
11042 switch( sig_alg )
11043 {
11044 case MBEDTLS_PK_RSA:
11045 if( set->rsa == MBEDTLS_MD_NONE )
11046 set->rsa = md_alg;
11047 break;
11048
11049 case MBEDTLS_PK_ECDSA:
11050 if( set->ecdsa == MBEDTLS_MD_NONE )
11051 set->ecdsa = md_alg;
11052 break;
11053
11054 default:
11055 break;
11056 }
11057}
11058
11059/* Allow exactly one hash algorithm for each signature. */
11060void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11061 mbedtls_md_type_t md_alg )
11062{
11063 set->rsa = md_alg;
11064 set->ecdsa = md_alg;
11065}
11066
11067#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11068 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11069
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011070/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011071 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011073mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011074{
11075 switch( hash )
11076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011077#if defined(MBEDTLS_MD5_C)
11078 case MBEDTLS_SSL_HASH_MD5:
11079 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011080#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011081#if defined(MBEDTLS_SHA1_C)
11082 case MBEDTLS_SSL_HASH_SHA1:
11083 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011085#if defined(MBEDTLS_SHA256_C)
11086 case MBEDTLS_SSL_HASH_SHA224:
11087 return( MBEDTLS_MD_SHA224 );
11088 case MBEDTLS_SSL_HASH_SHA256:
11089 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011091#if defined(MBEDTLS_SHA512_C)
11092 case MBEDTLS_SSL_HASH_SHA384:
11093 return( MBEDTLS_MD_SHA384 );
11094 case MBEDTLS_SSL_HASH_SHA512:
11095 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011096#endif
11097 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011098 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011099 }
11100}
11101
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011102/*
11103 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11104 */
11105unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11106{
11107 switch( md )
11108 {
11109#if defined(MBEDTLS_MD5_C)
11110 case MBEDTLS_MD_MD5:
11111 return( MBEDTLS_SSL_HASH_MD5 );
11112#endif
11113#if defined(MBEDTLS_SHA1_C)
11114 case MBEDTLS_MD_SHA1:
11115 return( MBEDTLS_SSL_HASH_SHA1 );
11116#endif
11117#if defined(MBEDTLS_SHA256_C)
11118 case MBEDTLS_MD_SHA224:
11119 return( MBEDTLS_SSL_HASH_SHA224 );
11120 case MBEDTLS_MD_SHA256:
11121 return( MBEDTLS_SSL_HASH_SHA256 );
11122#endif
11123#if defined(MBEDTLS_SHA512_C)
11124 case MBEDTLS_MD_SHA384:
11125 return( MBEDTLS_SSL_HASH_SHA384 );
11126 case MBEDTLS_MD_SHA512:
11127 return( MBEDTLS_SSL_HASH_SHA512 );
11128#endif
11129 default:
11130 return( MBEDTLS_SSL_HASH_NONE );
11131 }
11132}
11133
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011134#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011135/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011136 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011137 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011138 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011139int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011141 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011142
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011143 if( ssl->conf->curve_list == NULL )
11144 return( -1 );
11145
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011146 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011147 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011148 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011149
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011150 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011151}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011152#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011153
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011154#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011155/*
11156 * Check if a hash proposed by the peer is in our list.
11157 * Return 0 if we're willing to use it, -1 otherwise.
11158 */
11159int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11160 mbedtls_md_type_t md )
11161{
11162 const int *cur;
11163
11164 if( ssl->conf->sig_hashes == NULL )
11165 return( -1 );
11166
11167 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11168 if( *cur == (int) md )
11169 return( 0 );
11170
11171 return( -1 );
11172}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011173#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011175#if defined(MBEDTLS_X509_CRT_PARSE_C)
11176int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11177 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011178 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011179 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011180{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011181 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011182#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011183 int usage = 0;
11184#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011185#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011186 const char *ext_oid;
11187 size_t ext_len;
11188#endif
11189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011190#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11191 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011192 ((void) cert);
11193 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011194 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011195#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011197#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11198 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011199 {
11200 /* Server part of the key exchange */
11201 switch( ciphersuite->key_exchange )
11202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011203 case MBEDTLS_KEY_EXCHANGE_RSA:
11204 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011205 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011206 break;
11207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011208 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11209 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11210 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11211 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011212 break;
11213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011214 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11215 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011216 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011217 break;
11218
11219 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011220 case MBEDTLS_KEY_EXCHANGE_NONE:
11221 case MBEDTLS_KEY_EXCHANGE_PSK:
11222 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11223 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011224 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011225 usage = 0;
11226 }
11227 }
11228 else
11229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011230 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11231 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011232 }
11233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011234 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011235 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011236 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011237 ret = -1;
11238 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011239#else
11240 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011241#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011243#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11244 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011246 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11247 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011248 }
11249 else
11250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011251 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11252 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011253 }
11254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011255 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011256 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011257 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011258 ret = -1;
11259 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011260#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011261
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011262 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011263}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011264#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011265
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011266/*
11267 * Convert version numbers to/from wire format
11268 * and, for DTLS, to/from TLS equivalent.
11269 *
11270 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011271 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011272 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11273 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011275void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011276 unsigned char ver[2] )
11277{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011278#if defined(MBEDTLS_SSL_PROTO_DTLS)
11279 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011281 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011282 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11283
11284 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11285 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11286 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011287 else
11288#else
11289 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011290#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011291 {
11292 ver[0] = (unsigned char) major;
11293 ver[1] = (unsigned char) minor;
11294 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011295}
11296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011297void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011298 const unsigned char ver[2] )
11299{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011300#if defined(MBEDTLS_SSL_PROTO_DTLS)
11301 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011302 {
11303 *major = 255 - ver[0] + 2;
11304 *minor = 255 - ver[1] + 1;
11305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011306 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011307 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11308 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011309 else
11310#else
11311 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011312#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011313 {
11314 *major = ver[0];
11315 *minor = ver[1];
11316 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011317}
11318
Simon Butcher99000142016-10-13 17:21:01 +010011319int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11320{
11321#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11322 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11323 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11324
11325 switch( md )
11326 {
11327#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11328#if defined(MBEDTLS_MD5_C)
11329 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011330 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011331#endif
11332#if defined(MBEDTLS_SHA1_C)
11333 case MBEDTLS_SSL_HASH_SHA1:
11334 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11335 break;
11336#endif
11337#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11338#if defined(MBEDTLS_SHA512_C)
11339 case MBEDTLS_SSL_HASH_SHA384:
11340 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11341 break;
11342#endif
11343#if defined(MBEDTLS_SHA256_C)
11344 case MBEDTLS_SSL_HASH_SHA256:
11345 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11346 break;
11347#endif
11348 default:
11349 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11350 }
11351
11352 return 0;
11353#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11354 (void) ssl;
11355 (void) md;
11356
11357 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11358#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11359}
11360
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011361#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11362 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11363int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11364 unsigned char *output,
11365 unsigned char *data, size_t data_len )
11366{
11367 int ret = 0;
11368 mbedtls_md5_context mbedtls_md5;
11369 mbedtls_sha1_context mbedtls_sha1;
11370
11371 mbedtls_md5_init( &mbedtls_md5 );
11372 mbedtls_sha1_init( &mbedtls_sha1 );
11373
11374 /*
11375 * digitally-signed struct {
11376 * opaque md5_hash[16];
11377 * opaque sha_hash[20];
11378 * };
11379 *
11380 * md5_hash
11381 * MD5(ClientHello.random + ServerHello.random
11382 * + ServerParams);
11383 * sha_hash
11384 * SHA(ClientHello.random + ServerHello.random
11385 * + ServerParams);
11386 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011387 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011388 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_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_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011393 ssl->handshake->randbytes, 64 ) ) != 0 )
11394 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011396 goto exit;
11397 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011398 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011399 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011401 goto exit;
11402 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011403 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011404 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011406 goto exit;
11407 }
11408
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011409 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011410 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011412 goto exit;
11413 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011414 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011415 ssl->handshake->randbytes, 64 ) ) != 0 )
11416 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011418 goto exit;
11419 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011420 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011421 data_len ) ) != 0 )
11422 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011424 goto exit;
11425 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011426 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011427 output + 16 ) ) != 0 )
11428 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011430 goto exit;
11431 }
11432
11433exit:
11434 mbedtls_md5_free( &mbedtls_md5 );
11435 mbedtls_sha1_free( &mbedtls_sha1 );
11436
11437 if( ret != 0 )
11438 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11439 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11440
11441 return( ret );
11442
11443}
11444#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11445 MBEDTLS_SSL_PROTO_TLS1_1 */
11446
11447#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11448 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011449
11450#if defined(MBEDTLS_USE_PSA_CRYPTO)
11451int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11452 unsigned char *hash, size_t *hashlen,
11453 unsigned char *data, size_t data_len,
11454 mbedtls_md_type_t md_alg )
11455{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011456 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011457 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011458 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11459
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011461
11462 if( ( status = psa_hash_setup( &hash_operation,
11463 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011464 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011465 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011466 goto exit;
11467 }
11468
Andrzej Kurek814feff2019-01-14 04:35:19 -050011469 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11470 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011471 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011472 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011473 goto exit;
11474 }
11475
Andrzej Kurek814feff2019-01-14 04:35:19 -050011476 if( ( status = psa_hash_update( &hash_operation,
11477 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011478 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011479 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011480 goto exit;
11481 }
11482
Andrzej Kurek814feff2019-01-14 04:35:19 -050011483 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11484 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011485 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011486 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011487 goto exit;
11488 }
11489
11490exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011491 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011492 {
11493 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11494 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011495 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011496 {
11497 case PSA_ERROR_NOT_SUPPORTED:
11498 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011499 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011500 case PSA_ERROR_BUFFER_TOO_SMALL:
11501 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11502 case PSA_ERROR_INSUFFICIENT_MEMORY:
11503 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11504 default:
11505 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11506 }
11507 }
11508 return( 0 );
11509}
11510
11511#else
11512
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011513int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011514 unsigned char *hash, size_t *hashlen,
11515 unsigned char *data, size_t data_len,
11516 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011517{
11518 int ret = 0;
11519 mbedtls_md_context_t ctx;
11520 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011521 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011522
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011524
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011525 mbedtls_md_init( &ctx );
11526
11527 /*
11528 * digitally-signed struct {
11529 * opaque client_random[32];
11530 * opaque server_random[32];
11531 * ServerDHParams params;
11532 * };
11533 */
11534 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11535 {
11536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11537 goto exit;
11538 }
11539 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11540 {
11541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11542 goto exit;
11543 }
11544 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11545 {
11546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11547 goto exit;
11548 }
11549 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11550 {
11551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11552 goto exit;
11553 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011554 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011555 {
11556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11557 goto exit;
11558 }
11559
11560exit:
11561 mbedtls_md_free( &ctx );
11562
11563 if( ret != 0 )
11564 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11565 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11566
11567 return( ret );
11568}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011569#endif /* MBEDTLS_USE_PSA_CRYPTO */
11570
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011571#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11572 MBEDTLS_SSL_PROTO_TLS1_2 */
11573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011574#endif /* MBEDTLS_SSL_TLS_C */