blob: 15ce501404bbe303da1e7599f41302398399b784 [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/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001581 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001582 *
1583 * Parameters:
1584 * [in/out] handshake
1585 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1586 * (PSA-PSK) ciphersuite_info
1587 * [out] premaster (cleared)
1588 * [in] minor_ver (to compute hash_len)
1589 * [in] hash_alg (to compute hash_len)
1590 * [out] master
1591 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1592 * debug: conf->f_dbg, conf->p_dbg
1593 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
1594 * PSA-PSA: conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001595 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001596static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
1597 int minor_ver,
1598 mbedtls_md_type_t hash_alg,
1599 unsigned char *master,
1600 mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001601{
1602 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001603
1604 /* cf. RFC 5246, Section 8.1:
1605 * "The master secret is always exactly 48 bytes in length." */
1606 size_t const master_secret_len = 48;
1607
1608#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1609 unsigned char session_hash[48];
1610#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1611
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001612 /* The label for the KDF used for key expansion.
1613 * This is either "master secret" or "extended master secret"
1614 * depending on whether the Extended Master Secret extension
1615 * is used. */
1616 char const *lbl = "master secret";
1617
1618 /* The salt for the KDF used for key expansion.
1619 * - If the Extended Master Secret extension is not used,
1620 * this is ClientHello.Random + ServerHello.Random
1621 * (see Sect. 8.1 in RFC 5246).
1622 * - If the Extended Master Secret extension is used,
1623 * this is the transcript of the handshake so far.
1624 * (see Sect. 4 in RFC 7627). */
1625 unsigned char const *salt = handshake->randbytes;
1626 size_t salt_len = 64;
1627
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001628#if !defined(MBEDTLS_DEBUG_C) && \
1629 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1630 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1631 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
1632 (void) ssl;
1633#endif
1634#if !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) || !defined(MBEDTLS_SSL_PROTO_TLS1_2)
1635 (void) minor_ver;
1636#if defined(MBEDTLS_SHA512_C)
1637 (void) hash_alg;
1638#endif
1639#endif
1640
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001641 if( handshake->resume != 0 )
1642 {
1643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001644 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001645 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001646
1647#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001648 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001649 {
1650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001651
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001652 lbl = "extended master secret";
1653 salt = session_hash;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001654 handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001655#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001656 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001657 {
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001658#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001659 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001660 {
1661 salt_len = 48;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001662 }
1663 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001664#endif /* MBEDTLS_SHA512_C */
1665 salt_len = 32;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001666 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001667 else
1668#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1669 salt_len = 36;
1670
1671 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
1672 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001673#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1674
1675#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001676 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1677 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1678 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001679 ssl_use_opaque_psk( ssl ) == 1 )
1680 {
1681 /* Perform PSK-to-MS expansion in a single step. */
1682 psa_status_t status;
1683 psa_algorithm_t alg;
1684 psa_key_handle_t psk;
1685 psa_key_derivation_operation_t derivation =
1686 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001687
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001689
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001690 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001691 if( handshake->psk_opaque != 0 )
1692 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001693
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001694 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001695 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001696 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001697 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1698
1699 status = psa_key_derivation( &derivation, psk, alg,
1700 salt, salt_len,
1701 (unsigned char const *) lbl,
1702 (size_t) strlen( lbl ),
1703 master_secret_len );
1704 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001705 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001706 psa_key_derivation_abort( &derivation );
1707 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001708 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001709
1710 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001711 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001712 master_secret_len );
1713 if( status != PSA_SUCCESS )
1714 {
1715 psa_key_derivation_abort( &derivation );
1716 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1717 }
1718
1719 status = psa_key_derivation_abort( &derivation );
1720 if( status != PSA_SUCCESS )
1721 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1722 }
1723 else
1724#endif
1725 {
1726 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1727 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001728 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001729 master_secret_len );
1730 if( ret != 0 )
1731 {
1732 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1733 return( ret );
1734 }
1735
1736 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1737 handshake->premaster,
1738 handshake->pmslen );
1739
1740 mbedtls_platform_zeroize( handshake->premaster,
1741 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001742 }
1743
1744 return( 0 );
1745}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001746
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001747int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1748{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001749 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001750 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1751 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001752
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001753 ret = ssl_set_handshake_prfs( ssl->handshake,
1754 ssl->minor_ver,
1755 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001756 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001757 {
1758 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001759 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001760 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001761
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001762 ret = ssl_compute_master( ssl->handshake,
1763 ssl->minor_ver,
1764 ciphersuite_info->mac,
1765 ssl->session_negotiate->master,
1766 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001767 if( ret != 0 )
1768 {
1769 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1770 return( ret );
1771 }
1772
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001773 return( ssl_populate_transform( ssl ) );
1774}
1775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#if defined(MBEDTLS_SSL_PROTO_SSL3)
1777void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001778{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001779 mbedtls_md5_context md5;
1780 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 unsigned char pad_1[48];
1782 unsigned char pad_2[48];
1783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001786 mbedtls_md5_init( &md5 );
1787 mbedtls_sha1_init( &sha1 );
1788
1789 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1790 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Paul Bakker380da532012-04-18 16:10:25 +00001792 memset( pad_1, 0x36, 48 );
1793 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001795 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1796 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1797 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001799 mbedtls_md5_starts_ret( &md5 );
1800 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1801 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1802 mbedtls_md5_update_ret( &md5, hash, 16 );
1803 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001804
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001805 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1806 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1807 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001808
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001809 mbedtls_sha1_starts_ret( &sha1 );
1810 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1811 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1812 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1813 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001817
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001818 mbedtls_md5_free( &md5 );
1819 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001820
Paul Bakker380da532012-04-18 16:10:25 +00001821 return;
1822}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1826void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001827{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001828 mbedtls_md5_context md5;
1829 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001832
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001833 mbedtls_md5_init( &md5 );
1834 mbedtls_sha1_init( &sha1 );
1835
1836 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1837 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001838
Andrzej Kurekeb342242019-01-29 09:14:33 -05001839 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001840 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001844
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001845 mbedtls_md5_free( &md5 );
1846 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001847
Paul Bakker380da532012-04-18 16:10:25 +00001848 return;
1849}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1853#if defined(MBEDTLS_SHA256_C)
1854void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001855{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001856#if defined(MBEDTLS_USE_PSA_CRYPTO)
1857 size_t hash_size;
1858 psa_status_t status;
1859 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1860
1861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1862 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1863 if( status != PSA_SUCCESS )
1864 {
1865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1866 return;
1867 }
1868
1869 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1870 if( status != PSA_SUCCESS )
1871 {
1872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1873 return;
1874 }
1875 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1877#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001878 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001879
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001880 mbedtls_sha256_init( &sha256 );
1881
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001883
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001884 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001885 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001889
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001890 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001891#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001892 return;
1893}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896#if defined(MBEDTLS_SHA512_C)
1897void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001898{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001899#if defined(MBEDTLS_USE_PSA_CRYPTO)
1900 size_t hash_size;
1901 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001902 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001903
1904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001905 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001906 if( status != PSA_SUCCESS )
1907 {
1908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1909 return;
1910 }
1911
Andrzej Kurek972fba52019-01-30 03:29:12 -05001912 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001913 if( status != PSA_SUCCESS )
1914 {
1915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1916 return;
1917 }
1918 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1920#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001921 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001922
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001923 mbedtls_sha512_init( &sha512 );
1924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001926
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001927 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001928 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001933 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001934#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 return;
1936}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937#endif /* MBEDTLS_SHA512_C */
1938#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1941int 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 +02001942{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001943 unsigned char *p = ssl->handshake->premaster;
1944 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001945 const unsigned char *psk = ssl->conf->psk;
1946 size_t psk_len = ssl->conf->psk_len;
1947
1948 /* If the psk callback was called, use its result */
1949 if( ssl->handshake->psk != NULL )
1950 {
1951 psk = ssl->handshake->psk;
1952 psk_len = ssl->handshake->psk_len;
1953 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001954
1955 /*
1956 * PMS = struct {
1957 * opaque other_secret<0..2^16-1>;
1958 * opaque psk<0..2^16-1>;
1959 * };
1960 * with "other_secret" depending on the particular key exchange
1961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1963 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001964 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001965 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001967
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001968 *(p++) = (unsigned char)( psk_len >> 8 );
1969 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001970
1971 if( end < p || (size_t)( end - p ) < psk_len )
1972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1973
1974 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001975 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001976 }
1977 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1979#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1980 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001981 {
1982 /*
1983 * other_secret already set by the ClientKeyExchange message,
1984 * and is 48 bytes long
1985 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001986 if( end - p < 2 )
1987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1988
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001989 *p++ = 0;
1990 *p++ = 48;
1991 p += 48;
1992 }
1993 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1995#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1996 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001997 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001998 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001999 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002000
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002001 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002003 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002004 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002007 return( ret );
2008 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002009 *(p++) = (unsigned char)( len >> 8 );
2010 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002011 p += len;
2012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002014 }
2015 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2017#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2018 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002019 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002020 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002021 size_t zlen;
2022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002024 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002025 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002028 return( ret );
2029 }
2030
2031 *(p++) = (unsigned char)( zlen >> 8 );
2032 *(p++) = (unsigned char)( zlen );
2033 p += zlen;
2034
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002035 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2036 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002037 }
2038 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2042 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002043 }
2044
2045 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002046 if( end - p < 2 )
2047 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002048
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002049 *(p++) = (unsigned char)( psk_len >> 8 );
2050 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002051
2052 if( end < p || (size_t)( end - p ) < psk_len )
2053 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2054
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002055 memcpy( p, psk, psk_len );
2056 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002057
2058 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2059
2060 return( 0 );
2061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002065/*
2066 * SSLv3.0 MAC functions
2067 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002068#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002069static void ssl_mac( mbedtls_md_context_t *md_ctx,
2070 const unsigned char *secret,
2071 const unsigned char *buf, size_t len,
2072 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002073 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002074{
2075 unsigned char header[11];
2076 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002077 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2079 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002080
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002081 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002083 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002084 else
Paul Bakker68884e32013-01-07 18:20:04 +01002085 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
2087 memcpy( header, ctr, 8 );
2088 header[ 8] = (unsigned char) type;
2089 header[ 9] = (unsigned char)( len >> 8 );
2090 header[10] = (unsigned char)( len );
2091
Paul Bakker68884e32013-01-07 18:20:04 +01002092 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 mbedtls_md_starts( md_ctx );
2094 mbedtls_md_update( md_ctx, secret, md_size );
2095 mbedtls_md_update( md_ctx, padding, padlen );
2096 mbedtls_md_update( md_ctx, header, 11 );
2097 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002098 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002099
Paul Bakker68884e32013-01-07 18:20:04 +01002100 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 mbedtls_md_starts( md_ctx );
2102 mbedtls_md_update( md_ctx, secret, md_size );
2103 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002104 mbedtls_md_update( md_ctx, out, md_size );
2105 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002106}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002108
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002109/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002110 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002111#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002112 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2113 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2114 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2115/* This function makes sure every byte in the memory region is accessed
2116 * (in ascending addresses order) */
2117static void ssl_read_memory( unsigned char *p, size_t len )
2118{
2119 unsigned char acc = 0;
2120 volatile unsigned char force;
2121
2122 for( ; len != 0; p++, len-- )
2123 acc ^= *p;
2124
2125 force = acc;
2126 (void) force;
2127}
2128#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2129
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002130/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002132 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002133
Hanno Beckera0e20d02019-05-15 14:03:01 +01002134#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002135/* This functions transforms a DTLS plaintext fragment and a record content
2136 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002137 *
2138 * struct {
2139 * opaque content[DTLSPlaintext.length];
2140 * ContentType real_type;
2141 * uint8 zeros[length_of_padding];
2142 * } DTLSInnerPlaintext;
2143 *
2144 * Input:
2145 * - `content`: The beginning of the buffer holding the
2146 * plaintext to be wrapped.
2147 * - `*content_size`: The length of the plaintext in Bytes.
2148 * - `max_len`: The number of Bytes available starting from
2149 * `content`. This must be `>= *content_size`.
2150 * - `rec_type`: The desired record content type.
2151 *
2152 * Output:
2153 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2154 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2155 *
2156 * Returns:
2157 * - `0` on success.
2158 * - A negative error code if `max_len` didn't offer enough space
2159 * for the expansion.
2160 */
2161static int ssl_cid_build_inner_plaintext( unsigned char *content,
2162 size_t *content_size,
2163 size_t remaining,
2164 uint8_t rec_type )
2165{
2166 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002167 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2168 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2169 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002170
2171 /* Write real content type */
2172 if( remaining == 0 )
2173 return( -1 );
2174 content[ len ] = rec_type;
2175 len++;
2176 remaining--;
2177
2178 if( remaining < pad )
2179 return( -1 );
2180 memset( content + len, 0, pad );
2181 len += pad;
2182 remaining -= pad;
2183
2184 *content_size = len;
2185 return( 0 );
2186}
2187
Hanno Becker07dc97d2019-05-20 15:08:01 +01002188/* This function parses a DTLSInnerPlaintext structure.
2189 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002190static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2191 size_t *content_size,
2192 uint8_t *rec_type )
2193{
2194 size_t remaining = *content_size;
2195
2196 /* Determine length of padding by skipping zeroes from the back. */
2197 do
2198 {
2199 if( remaining == 0 )
2200 return( -1 );
2201 remaining--;
2202 } while( content[ remaining ] == 0 );
2203
2204 *content_size = remaining;
2205 *rec_type = content[ remaining ];
2206
2207 return( 0 );
2208}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002209#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002210
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002211/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002212 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002213static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002214 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002215 mbedtls_record *rec )
2216{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002217 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002218 *
2219 * additional_data = seq_num + TLSCompressed.type +
2220 * TLSCompressed.version + TLSCompressed.length;
2221 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002222 * For the CID extension, this is extended as follows
2223 * (quoting draft-ietf-tls-dtls-connection-id-05,
2224 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002225 *
2226 * additional_data = seq_num + DTLSPlaintext.type +
2227 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002228 * cid +
2229 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002230 * length_of_DTLSInnerPlaintext;
2231 */
2232
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2234 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002235 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002236
Hanno Beckera0e20d02019-05-15 14:03:01 +01002237#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002238 if( rec->cid_len != 0 )
2239 {
2240 memcpy( add_data + 11, rec->cid, rec->cid_len );
2241 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2242 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2243 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2244 *add_data_len = 13 + 1 + rec->cid_len;
2245 }
2246 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002247#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002248 {
2249 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2250 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2251 *add_data_len = 13;
2252 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002253}
2254
Hanno Beckera18d1322018-01-03 14:27:32 +00002255int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2256 mbedtls_ssl_transform *transform,
2257 mbedtls_record *rec,
2258 int (*f_rng)(void *, unsigned char *, size_t),
2259 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002262 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002263 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002264 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002265 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002266 size_t post_avail;
2267
2268 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002269#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002270 ((void) ssl);
2271#endif
2272
2273 /* The PRNG is used for dynamic IV generation that's used
2274 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2275#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2276 ( defined(MBEDTLS_AES_C) || \
2277 defined(MBEDTLS_ARIA_C) || \
2278 defined(MBEDTLS_CAMELLIA_C) ) && \
2279 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2280 ((void) f_rng);
2281 ((void) p_rng);
2282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002286 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002287 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2290 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002291 if( rec == NULL
2292 || rec->buf == NULL
2293 || rec->buf_len < rec->data_offset
2294 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002295#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002296 || rec->cid_len != 0
2297#endif
2298 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002299 {
2300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002302 }
2303
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002304 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002305 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002307 data, rec->data_len );
2308
2309 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2310
2311 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2312 {
2313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2314 (unsigned) rec->data_len,
2315 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2316 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2317 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002318
Hanno Beckera0e20d02019-05-15 14:03:01 +01002319#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002320 /*
2321 * Add CID information
2322 */
2323 rec->cid_len = transform->out_cid_len;
2324 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2325 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002326
2327 if( rec->cid_len != 0 )
2328 {
2329 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002330 * Wrap plaintext into DTLSInnerPlaintext structure.
2331 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002332 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002333 * Note that this changes `rec->data_len`, and hence
2334 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002335 */
2336 if( ssl_cid_build_inner_plaintext( data,
2337 &rec->data_len,
2338 post_avail,
2339 rec->type ) != 0 )
2340 {
2341 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2342 }
2343
2344 rec->type = MBEDTLS_SSL_MSG_CID;
2345 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002346#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002347
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002348 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2349
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002351 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002352 */
Hanno Becker52344c22018-01-03 15:24:20 +00002353#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 if( mode == MBEDTLS_MODE_STREAM ||
2355 ( mode == MBEDTLS_MODE_CBC
2356#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002357 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002358#endif
2359 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002360 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002361 if( post_avail < transform->maclen )
2362 {
2363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2364 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2365 }
2366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002368 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002369 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002370 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002371 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2372 data, rec->data_len, rec->ctr, rec->type, mac );
2373 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002374 }
2375 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002376#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2378 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002379 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002380 {
Hanno Becker992b6872017-11-09 18:57:39 +00002381 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2382
Hanno Beckercab87e62019-04-29 13:52:53 +01002383 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002384
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002385 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002386 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002387 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2388 data, rec->data_len );
2389 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2390 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2391
2392 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002393 }
2394 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002395#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002399 }
2400
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002401 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2402 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002403
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002404 rec->data_len += transform->maclen;
2405 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002406 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002407 }
Hanno Becker52344c22018-01-03 15:24:20 +00002408#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002410 /*
2411 * Encrypt
2412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2414 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002416 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002417 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002419 "including %d bytes of padding",
2420 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002422 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2423 transform->iv_enc, transform->ivlen,
2424 data, rec->data_len,
2425 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002428 return( ret );
2429 }
2430
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002431 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 }
Paul Bakker68884e32013-01-07 18:20:04 +01002437 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002439
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002440#if defined(MBEDTLS_GCM_C) || \
2441 defined(MBEDTLS_CCM_C) || \
2442 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002444 mode == MBEDTLS_MODE_CCM ||
2445 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002446 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002447 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002448 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002449 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002450
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002451 /* Check that there's space for both the authentication tag
2452 * and the explicit IV before and after the record content. */
2453 if( post_avail < transform->taglen ||
2454 rec->data_offset < explicit_iv_len )
2455 {
2456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2457 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2458 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002459
Paul Bakker68884e32013-01-07 18:20:04 +01002460 /*
2461 * Generate IV
2462 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002463 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2464 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002465 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002466 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002467 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2468 explicit_iv_len );
2469 /* Prefix record content with explicit IV. */
2470 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002471 }
2472 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2473 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002474 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002475 unsigned char i;
2476
2477 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2478
2479 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002480 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002481 }
2482 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002483 {
2484 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2486 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002487 }
2488
Hanno Beckercab87e62019-04-29 13:52:53 +01002489 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002490
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002491 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2492 iv, transform->ivlen );
2493 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002494 data - explicit_iv_len, explicit_iv_len );
2495 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002496 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002498 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002499 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002500
Paul Bakker68884e32013-01-07 18:20:04 +01002501 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002502 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002503 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002504
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002505 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002506 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002507 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002508 data, rec->data_len, /* source */
2509 data, &rec->data_len, /* destination */
2510 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002513 return( ret );
2514 }
2515
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002516 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2517 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002518
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002519 rec->data_len += transform->taglen + explicit_iv_len;
2520 rec->data_offset -= explicit_iv_len;
2521 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002522 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002523 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2526#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002527 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002530 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002531 size_t padlen, i;
2532 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002533
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002534 /* Currently we're always using minimal padding
2535 * (up to 255 bytes would be allowed). */
2536 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2537 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 padlen = 0;
2539
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002540 /* Check there's enough space in the buffer for the padding. */
2541 if( post_avail < padlen + 1 )
2542 {
2543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2544 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2545 }
2546
Paul Bakker5121ce52009-01-03 21:22:43 +00002547 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002548 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002550 rec->data_len += padlen + 1;
2551 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002554 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002555 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2556 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002557 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002558 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002559 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002560 if( f_rng == NULL )
2561 {
2562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2564 }
2565
2566 if( rec->data_offset < transform->ivlen )
2567 {
2568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2569 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2570 }
2571
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002572 /*
2573 * Generate IV
2574 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002575 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002576 if( ret != 0 )
2577 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002578
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002579 memcpy( data - transform->ivlen, transform->iv_enc,
2580 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002581
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002582 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002586 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002587 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002588 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002590 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2591 transform->iv_enc,
2592 transform->ivlen,
2593 data, rec->data_len,
2594 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002597 return( ret );
2598 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002599
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002600 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002604 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002607 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002608 {
2609 /*
2610 * Save IV in SSL3 and TLS1
2611 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002612 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2613 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002615 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002616#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002617 {
2618 data -= transform->ivlen;
2619 rec->data_offset -= transform->ivlen;
2620 rec->data_len += transform->ivlen;
2621 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002624 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002625 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002626 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2627
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002628 /*
2629 * MAC(MAC_write_key, seq_num +
2630 * TLSCipherText.type +
2631 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002632 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002633 * IV + // except for TLS 1.0
2634 * ENC(content + padding + padding_length));
2635 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002636
2637 if( post_avail < transform->maclen)
2638 {
2639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2640 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2641 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002642
Hanno Beckercab87e62019-04-29 13:52:53 +01002643 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002646 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002647 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002648
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002649 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002650 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002651 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2652 data, rec->data_len );
2653 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2654 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002655
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002656 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002657
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002658 rec->data_len += transform->maclen;
2659 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002660 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002661 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002664 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002666 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2669 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002671
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002672 /* Make extra sure authentication was performed, exactly once */
2673 if( auth_done != 1 )
2674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002677 }
2678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
2681 return( 0 );
2682}
2683
Hanno Becker605949f2019-07-12 08:23:59 +01002684int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002685 mbedtls_ssl_transform *transform,
2686 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002687{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002688 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002690 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002691#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002692 size_t padlen = 0, correct = 1;
2693#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002694 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002695 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002696 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002697
Hanno Beckera18d1322018-01-03 14:27:32 +00002698#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002699 ((void) ssl);
2700#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002703 if( rec == NULL ||
2704 rec->buf == NULL ||
2705 rec->buf_len < rec->data_offset ||
2706 rec->buf_len - rec->data_offset < rec->data_len )
2707 {
2708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002710 }
2711
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002712 data = rec->buf + rec->data_offset;
2713 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
Hanno Beckera0e20d02019-05-15 14:03:01 +01002715#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002716 /*
2717 * Match record's CID with incoming CID.
2718 */
Hanno Becker938489a2019-05-08 13:02:22 +01002719 if( rec->cid_len != transform->in_cid_len ||
2720 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2721 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002722 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002723 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002724#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2727 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002728 {
2729 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002730 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2731 transform->iv_dec,
2732 transform->ivlen,
2733 data, rec->data_len,
2734 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002737 return( ret );
2738 }
2739
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002740 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002744 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 }
Paul Bakker68884e32013-01-07 18:20:04 +01002746 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002748#if defined(MBEDTLS_GCM_C) || \
2749 defined(MBEDTLS_CCM_C) || \
2750 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002752 mode == MBEDTLS_MODE_CCM ||
2753 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002754 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002755 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002756 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002757
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002758 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002759 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002760 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002761
2762 /* Check that there's enough space for the explicit IV
2763 * (at the beginning of the record) and the MAC (at the
2764 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002765 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002768 "+ taglen (%d)", rec->data_len,
2769 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002771 }
Paul Bakker68884e32013-01-07 18:20:04 +01002772
Hanno Beckerd96a6522019-07-10 13:55:25 +01002773#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002774 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2775 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002776 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002777
Hanno Beckerd96a6522019-07-10 13:55:25 +01002778 /* Fixed */
2779 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2780 /* Explicit */
2781 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002782 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002783 else
2784#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2785#if defined(MBEDTLS_CHACHAPOLY_C)
2786 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002787 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002788 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002789 unsigned char i;
2790
2791 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2792
2793 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002794 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002795 }
2796 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002797#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002798 {
2799 /* Reminder if we ever add an AEAD mode with a different size */
2800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2801 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2802 }
2803
Hanno Beckerd96a6522019-07-10 13:55:25 +01002804 /* Group changes to data, data_len, and add_data, because
2805 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002806 data += explicit_iv_len;
2807 rec->data_offset += explicit_iv_len;
2808 rec->data_len -= explicit_iv_len + transform->taglen;
2809
Hanno Beckercab87e62019-04-29 13:52:53 +01002810 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002811 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002812 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002813
Hanno Beckerd96a6522019-07-10 13:55:25 +01002814 /* Because of the check above, we know that there are
2815 * explicit_iv_len Bytes preceeding data, and taglen
2816 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002817 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002818 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002819
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002820 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002821 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002822 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002823
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002824 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002825 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002826 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002827 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2828 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002829 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002830 data, rec->data_len,
2831 data, &olen,
2832 data + rec->data_len,
2833 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2838 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002839
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002840 return( ret );
2841 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002842 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002843
Hanno Beckerd96a6522019-07-10 13:55:25 +01002844 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002845 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2848 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002849 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002851 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2853#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002854 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002856 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002857 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002858
Paul Bakker5121ce52009-01-03 21:22:43 +00002859 /*
Paul Bakker45829992013-01-03 14:52:21 +01002860 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002863 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2864 {
2865 /* The ciphertext is prefixed with the CBC IV. */
2866 minlen += transform->ivlen;
2867 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002868#endif
Paul Bakker45829992013-01-03 14:52:21 +01002869
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002870 /* Size considerations:
2871 *
2872 * - The CBC cipher text must not be empty and hence
2873 * at least of size transform->ivlen.
2874 *
2875 * Together with the potential IV-prefix, this explains
2876 * the first of the two checks below.
2877 *
2878 * - The record must contain a MAC, either in plain or
2879 * encrypted, depending on whether Encrypt-then-MAC
2880 * is used or not.
2881 * - If it is, the message contains the IV-prefix,
2882 * the CBC ciphertext, and the MAC.
2883 * - If it is not, the padded plaintext, and hence
2884 * the CBC ciphertext, has at least length maclen + 1
2885 * because there is at least the padding length byte.
2886 *
2887 * As the CBC ciphertext is not empty, both cases give the
2888 * lower bound minlen + maclen + 1 on the record size, which
2889 * we test for in the second check below.
2890 */
2891 if( rec->data_len < minlen + transform->ivlen ||
2892 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002895 "+ 1 ) ( + expl IV )", rec->data_len,
2896 transform->ivlen,
2897 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002899 }
2900
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002901 /*
2902 * Authenticate before decrypt if enabled
2903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002905 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002906 {
Hanno Becker992b6872017-11-09 18:57:39 +00002907 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002910
Hanno Beckerd96a6522019-07-10 13:55:25 +01002911 /* Update data_len in tandem with add_data.
2912 *
2913 * The subtraction is safe because of the previous check
2914 * data_len >= minlen + maclen + 1.
2915 *
2916 * Afterwards, we know that data + data_len is followed by at
2917 * least maclen Bytes, which justifies the call to
2918 * mbedtls_ssl_safer_memcmp() below.
2919 *
2920 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002921 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002922 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002923
Hanno Beckerd96a6522019-07-10 13:55:25 +01002924 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002925 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2926 add_data_len );
2927 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2928 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002929 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2930 data, rec->data_len );
2931 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2932 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002933
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002934 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2935 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002936 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002937 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002938
Hanno Beckerd96a6522019-07-10 13:55:25 +01002939 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002940 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2941 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002945 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002946 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002947 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002949
2950 /*
2951 * Check length sanity
2952 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002953
2954 /* We know from above that data_len > minlen >= 0,
2955 * so the following check in particular implies that
2956 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002957 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002960 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002962 }
2963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002965 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002966 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002967 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002968 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002969 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002970 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002971 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002972
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002973 data += transform->ivlen;
2974 rec->data_offset += transform->ivlen;
2975 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002976 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002978
Hanno Beckerd96a6522019-07-10 13:55:25 +01002979 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2980
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002981 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2982 transform->iv_dec, transform->ivlen,
2983 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002986 return( ret );
2987 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002988
Hanno Beckerd96a6522019-07-10 13:55:25 +01002989 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002990 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2993 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002994 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002997 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002998 {
2999 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01003000 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3001 * records is equivalent to CBC decryption of the concatenation
3002 * of the records; in other words, IVs are maintained across
3003 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003004 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003005 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3006 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003007 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003008#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003009
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003010 /* Safe since data_len >= minlen + maclen + 1, so after having
3011 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003012 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3013 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003014 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003015
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003016 if( auth_done == 1 )
3017 {
3018 correct *= ( rec->data_len >= padlen + 1 );
3019 padlen *= ( rec->data_len >= padlen + 1 );
3020 }
3021 else
Paul Bakker45829992013-01-03 14:52:21 +01003022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003024 if( rec->data_len < transform->maclen + padlen + 1 )
3025 {
3026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3027 rec->data_len,
3028 transform->maclen,
3029 padlen + 1 ) );
3030 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003031#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003032
3033 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3034 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003037 padlen++;
3038
3039 /* Regardless of the validity of the padding,
3040 * we have data_len >= padlen here. */
3041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003043 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003045 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047#if defined(MBEDTLS_SSL_DEBUG_ALL)
3048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003049 "should be no more than %d",
3050 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003051#endif
Paul Bakker45829992013-01-03 14:52:21 +01003052 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 }
3054 }
3055 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3057#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3058 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003059 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003060 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003061 /* The padding check involves a series of up to 256
3062 * consecutive memory reads at the end of the record
3063 * plaintext buffer. In order to hide the length and
3064 * validity of the padding, always perform exactly
3065 * `min(256,plaintext_len)` reads (but take into account
3066 * only the last `padlen` bytes for the padding check). */
3067 size_t pad_count = 0;
3068 size_t real_count = 0;
3069 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003070
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003071 /* Index of first padding byte; it has been ensured above
3072 * that the subtraction is safe. */
3073 size_t const padding_idx = rec->data_len - padlen;
3074 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3075 size_t const start_idx = rec->data_len - num_checks;
3076 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003077
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003078 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003079 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003080 real_count |= ( idx >= padding_idx );
3081 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003082 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003083 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003086 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003088#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003089 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003091 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3093 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3096 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003097 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003098
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003099 /* If the padding was found to be invalid, padlen == 0
3100 * and the subtraction is safe. If the padding was found valid,
3101 * padlen hasn't been changed and the previous assertion
3102 * data_len >= padlen still holds. */
3103 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003105 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003107 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003111 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003112
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003113#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003115 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003116#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003117
3118 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003119 * Authenticate if not done yet.
3120 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003121 */
Hanno Becker52344c22018-01-03 15:24:20 +00003122#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003123 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003124 {
Hanno Becker992b6872017-11-09 18:57:39 +00003125 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003126
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003127 /* If the initial value of padlen was such that
3128 * data_len < maclen + padlen + 1, then padlen
3129 * got reset to 1, and the initial check
3130 * data_len >= minlen + maclen + 1
3131 * guarantees that at this point we still
3132 * have at least data_len >= maclen.
3133 *
3134 * If the initial value of padlen was such that
3135 * data_len >= maclen + padlen + 1, then we have
3136 * subtracted either padlen + 1 (if the padding was correct)
3137 * or 0 (if the padding was incorrect) since then,
3138 * hence data_len >= maclen in any case.
3139 */
3140 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003141 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003144 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003145 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003146 ssl_mac( &transform->md_ctx_dec,
3147 transform->mac_dec,
3148 data, rec->data_len,
3149 rec->ctr, rec->type,
3150 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003151 }
3152 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3154#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3155 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003156 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003157 {
3158 /*
3159 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003160 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003161 *
3162 * Known timing attacks:
3163 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3164 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003165 * To compensate for different timings for the MAC calculation
3166 * depending on how much padding was removed (which is determined
3167 * by padlen), process extra_run more blocks through the hash
3168 * function.
3169 *
3170 * The formula in the paper is
3171 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3172 * where L1 is the size of the header plus the decrypted message
3173 * plus CBC padding and L2 is the size of the header plus the
3174 * decrypted message. This is for an underlying hash function
3175 * with 64-byte blocks.
3176 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3177 * correctly. We round down instead of up, so -56 is the correct
3178 * value for our calculations instead of -55.
3179 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003180 * Repeat the formula rather than defining a block_size variable.
3181 * This avoids requiring division by a variable at runtime
3182 * (which would be marginally less efficient and would require
3183 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003184 */
3185 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003186 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003187
3188 /*
3189 * The next two sizes are the minimum and maximum values of
3190 * in_msglen over all padlen values.
3191 *
3192 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003193 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003194 *
3195 * Note that max_len + maclen is never more than the buffer
3196 * length, as we previously did in_msglen -= maclen too.
3197 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003198 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003199 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3200
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003201 memset( tmp, 0, sizeof( tmp ) );
3202
3203 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003204 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003205#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3206 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003207 case MBEDTLS_MD_MD5:
3208 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003209 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003210 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003211 extra_run =
3212 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3213 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003214 break;
3215#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003216#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003217 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003218 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003219 extra_run =
3220 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3221 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003222 break;
3223#endif
3224 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003226 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3227 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003228
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003229 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003230
Hanno Beckercab87e62019-04-29 13:52:53 +01003231 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3232 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003233 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3234 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003235 /* Make sure we access everything even when padlen > 0. This
3236 * makes the synchronisation requirements for just-in-time
3237 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003238 ssl_read_memory( data + rec->data_len, padlen );
3239 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003240
3241 /* Call mbedtls_md_process at least once due to cache attacks
3242 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003243 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003244 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003245
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003246 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003247
3248 /* Make sure we access all the memory that could contain the MAC,
3249 * before we check it in the next code block. This makes the
3250 * synchronisation requirements for just-in-time Prime+Probe
3251 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003252 ssl_read_memory( data + min_len,
3253 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003254 }
3255 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3257 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3260 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003261 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003262
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003263#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003264 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3265 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003266#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003267
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003268 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3269 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271#if defined(MBEDTLS_SSL_DEBUG_ALL)
3272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003273#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003274 correct = 0;
3275 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003276 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003277 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003278
3279 /*
3280 * Finally check the correct flag
3281 */
3282 if( correct == 0 )
3283 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003284#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003285
3286 /* Make extra sure authentication was performed, exactly once */
3287 if( auth_done != 1 )
3288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
Hanno Beckera0e20d02019-05-15 14:03:01 +01003293#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003294 if( rec->cid_len != 0 )
3295 {
3296 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3297 &rec->type );
3298 if( ret != 0 )
3299 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3300 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003301#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003304
3305 return( 0 );
3306}
3307
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003308#undef MAC_NONE
3309#undef MAC_PLAINTEXT
3310#undef MAC_CIPHERTEXT
3311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003313/*
3314 * Compression/decompression functions
3315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003317{
3318 int ret;
3319 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003320 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003321 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003322 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003325
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003326 if( len_pre == 0 )
3327 return( 0 );
3328
Paul Bakker2770fbd2012-07-03 13:30:23 +00003329 memcpy( msg_pre, ssl->out_msg, len_pre );
3330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003332 ssl->out_msglen ) );
3333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003335 ssl->out_msg, ssl->out_msglen );
3336
Paul Bakker48916f92012-09-16 19:57:18 +00003337 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3338 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3339 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003340 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003341
Paul Bakker48916f92012-09-16 19:57:18 +00003342 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003343 if( ret != Z_OK )
3344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3346 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003347 }
3348
Angus Grattond8213d02016-05-25 20:56:48 +10003349 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003350 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003353 ssl->out_msglen ) );
3354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003356 ssl->out_msg, ssl->out_msglen );
3357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003359
3360 return( 0 );
3361}
3362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003364{
3365 int ret;
3366 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003367 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003368 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003369 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003372
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003373 if( len_pre == 0 )
3374 return( 0 );
3375
Paul Bakker2770fbd2012-07-03 13:30:23 +00003376 memcpy( msg_pre, ssl->in_msg, len_pre );
3377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003379 ssl->in_msglen ) );
3380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003382 ssl->in_msg, ssl->in_msglen );
3383
Paul Bakker48916f92012-09-16 19:57:18 +00003384 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3385 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3386 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003387 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003388 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003389
Paul Bakker48916f92012-09-16 19:57:18 +00003390 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003391 if( ret != Z_OK )
3392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3394 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003395 }
3396
Angus Grattond8213d02016-05-25 20:56:48 +10003397 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003398 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003401 ssl->in_msglen ) );
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003404 ssl->in_msg, ssl->in_msglen );
3405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003407
3408 return( 0 );
3409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3413static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415#if defined(MBEDTLS_SSL_PROTO_DTLS)
3416static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003417{
3418 /* If renegotiation is not enforced, retransmit until we would reach max
3419 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003420 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003421 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003422 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003423 unsigned char doublings = 1;
3424
3425 while( ratio != 0 )
3426 {
3427 ++doublings;
3428 ratio >>= 1;
3429 }
3430
3431 if( ++ssl->renego_records_seen > doublings )
3432 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003434 return( 0 );
3435 }
3436 }
3437
3438 return( ssl_write_hello_request( ssl ) );
3439}
3440#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003442
Paul Bakker5121ce52009-01-03 21:22:43 +00003443/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003444 * Fill the input message buffer by appending data to it.
3445 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003446 *
3447 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3448 * available (from this read and/or a previous one). Otherwise, an error code
3449 * is returned (possibly EOF or WANT_READ).
3450 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003451 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3452 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3453 * since we always read a whole datagram at once.
3454 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003455 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003456 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003459{
Paul Bakker23986e52011-04-24 08:57:21 +00003460 int ret;
3461 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003464
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003465 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003468 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003470 }
3471
Angus Grattond8213d02016-05-25 20:56:48 +10003472 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003476 }
3477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003479 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003480 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003481 uint32_t timeout;
3482
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003483 /* Just to be sure */
3484 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3485 {
3486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3487 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3489 }
3490
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003491 /*
3492 * The point is, we need to always read a full datagram at once, so we
3493 * sometimes read more then requested, and handle the additional data.
3494 * It could be the rest of the current record (while fetching the
3495 * header) and/or some other records in the same datagram.
3496 */
3497
3498 /*
3499 * Move to the next record in the already read datagram if applicable
3500 */
3501 if( ssl->next_record_offset != 0 )
3502 {
3503 if( ssl->in_left < ssl->next_record_offset )
3504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3506 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003507 }
3508
3509 ssl->in_left -= ssl->next_record_offset;
3510
3511 if( ssl->in_left != 0 )
3512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003514 ssl->next_record_offset ) );
3515 memmove( ssl->in_hdr,
3516 ssl->in_hdr + ssl->next_record_offset,
3517 ssl->in_left );
3518 }
3519
3520 ssl->next_record_offset = 0;
3521 }
3522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003524 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003525
3526 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003527 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003528 */
3529 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003532 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003533 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003534
3535 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003536 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003537 * are not at the beginning of a new record, the caller did something
3538 * wrong.
3539 */
3540 if( ssl->in_left != 0 )
3541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003544 }
3545
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003546 /*
3547 * Don't even try to read if time's out already.
3548 * This avoids by-passing the timer when repeatedly receiving messages
3549 * that will end up being dropped.
3550 */
3551 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003552 {
3553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003554 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003555 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003556 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003557 {
Angus Grattond8213d02016-05-25 20:56:48 +10003558 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003561 timeout = ssl->handshake->retransmit_timeout;
3562 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003563 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003566
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003567 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003568 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3569 timeout );
3570 else
3571 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003574
3575 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003577 }
3578
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003579 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003582 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003585 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003586 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003589 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003590 }
3591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003595 return( ret );
3596 }
3597
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003598 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003599 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003601 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003603 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003604 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003607 return( ret );
3608 }
3609
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003610 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003611 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003613 }
3614
Paul Bakker5121ce52009-01-03 21:22:43 +00003615 if( ret < 0 )
3616 return( ret );
3617
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003618 ssl->in_left = ret;
3619 }
3620 else
3621#endif
3622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003624 ssl->in_left, nb_want ) );
3625
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003626 while( ssl->in_left < nb_want )
3627 {
3628 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003629
3630 if( ssl_check_timer( ssl ) != 0 )
3631 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3632 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003633 {
3634 if( ssl->f_recv_timeout != NULL )
3635 {
3636 ret = ssl->f_recv_timeout( ssl->p_bio,
3637 ssl->in_hdr + ssl->in_left, len,
3638 ssl->conf->read_timeout );
3639 }
3640 else
3641 {
3642 ret = ssl->f_recv( ssl->p_bio,
3643 ssl->in_hdr + ssl->in_left, len );
3644 }
3645 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003648 ssl->in_left, nb_want ) );
3649 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003650
3651 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003653
3654 if( ret < 0 )
3655 return( ret );
3656
mohammad160352aecb92018-03-28 23:41:40 -07003657 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003658 {
Darryl Green11999bb2018-03-13 15:22:58 +00003659 MBEDTLS_SSL_DEBUG_MSG( 1,
3660 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003661 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3663 }
3664
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003665 ssl->in_left += ret;
3666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003667 }
3668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003670
3671 return( 0 );
3672}
3673
3674/*
3675 * Flush any data not yet written
3676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003678{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003679 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003680 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003683
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003684 if( ssl->f_send == NULL )
3685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003687 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003689 }
3690
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003691 /* Avoid incrementing counter if data is flushed */
3692 if( ssl->out_left == 0 )
3693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003695 return( 0 );
3696 }
3697
Paul Bakker5121ce52009-01-03 21:22:43 +00003698 while( ssl->out_left > 0 )
3699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003701 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003702
Hanno Becker2b1e3542018-08-06 11:19:13 +01003703 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003704 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003707
3708 if( ret <= 0 )
3709 return( ret );
3710
mohammad160352aecb92018-03-28 23:41:40 -07003711 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003712 {
Darryl Green11999bb2018-03-13 15:22:58 +00003713 MBEDTLS_SSL_DEBUG_MSG( 1,
3714 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003715 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3717 }
3718
Paul Bakker5121ce52009-01-03 21:22:43 +00003719 ssl->out_left -= ret;
3720 }
3721
Hanno Becker2b1e3542018-08-06 11:19:13 +01003722#if defined(MBEDTLS_SSL_PROTO_DTLS)
3723 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003724 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003725 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003726 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003727 else
3728#endif
3729 {
3730 ssl->out_hdr = ssl->out_buf + 8;
3731 }
3732 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003735
3736 return( 0 );
3737}
3738
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003739/*
3740 * Functions to handle the DTLS retransmission state machine
3741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003743/*
3744 * Append current handshake message to current outgoing flight
3745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003747{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3750 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3751 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003752
3753 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003754 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003755 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003758 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003759 }
3760
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003761 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003762 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003765 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003766 }
3767
3768 /* Copy current handshake message with headers */
3769 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3770 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003771 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003772 msg->next = NULL;
3773
3774 /* Append to the current flight */
3775 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003776 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003777 else
3778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003779 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003780 while( cur->next != NULL )
3781 cur = cur->next;
3782 cur->next = msg;
3783 }
3784
Hanno Becker3b235902018-08-06 09:54:53 +01003785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003786 return( 0 );
3787}
3788
3789/*
3790 * Free the current flight of handshake messages
3791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003793{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794 mbedtls_ssl_flight_item *cur = flight;
3795 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003796
3797 while( cur != NULL )
3798 {
3799 next = cur->next;
3800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801 mbedtls_free( cur->p );
3802 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003803
3804 cur = next;
3805 }
3806}
3807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003808#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3809static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003810#endif
3811
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003812/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003813 * Swap transform_out and out_ctr with the alternative ones
3814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003816{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003818 unsigned char tmp_out_ctr[8];
3819
3820 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003823 return;
3824 }
3825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003827
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003828 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003829 tmp_transform = ssl->transform_out;
3830 ssl->transform_out = ssl->handshake->alt_transform_out;
3831 ssl->handshake->alt_transform_out = tmp_transform;
3832
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003833 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003834 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3835 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003836 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003837
3838 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003839 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3842 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3847 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003848 }
3849 }
3850#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003851}
3852
3853/*
3854 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003855 */
3856int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3857{
3858 int ret = 0;
3859
3860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3861
3862 ret = mbedtls_ssl_flight_transmit( ssl );
3863
3864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3865
3866 return( ret );
3867}
3868
3869/*
3870 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003871 *
3872 * Need to remember the current message in case flush_output returns
3873 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003874 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003875 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003876int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003877{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003878 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003882 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003884
3885 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003886 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003887 ssl_swap_epochs( ssl );
3888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003890 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003891
3892 while( ssl->handshake->cur_msg != NULL )
3893 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003894 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003895 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003896
Hanno Beckere1dcb032018-08-17 16:47:58 +01003897 int const is_finished =
3898 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3899 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3900
Hanno Becker04da1892018-08-14 13:22:10 +01003901 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3902 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3903
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003904 /* Swap epochs before sending Finished: we can't do it after
3905 * sending ChangeCipherSpec, in case write returns WANT_READ.
3906 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003907 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003908 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003910 ssl_swap_epochs( ssl );
3911 }
3912
Hanno Becker67bc7c32018-08-06 11:33:50 +01003913 ret = ssl_get_remaining_payload_in_datagram( ssl );
3914 if( ret < 0 )
3915 return( ret );
3916 max_frag_len = (size_t) ret;
3917
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003918 /* CCS is copied as is, while HS messages may need fragmentation */
3919 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3920 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003921 if( max_frag_len == 0 )
3922 {
3923 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3924 return( ret );
3925
3926 continue;
3927 }
3928
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003929 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003930 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003931 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003932
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003933 /* Update position inside current message */
3934 ssl->handshake->cur_msg_p += cur->len;
3935 }
3936 else
3937 {
3938 const unsigned char * const p = ssl->handshake->cur_msg_p;
3939 const size_t hs_len = cur->len - 12;
3940 const size_t frag_off = p - ( cur->p + 12 );
3941 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003942 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003943
Hanno Beckere1dcb032018-08-17 16:47:58 +01003944 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003945 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003946 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003947 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003948
Hanno Becker67bc7c32018-08-06 11:33:50 +01003949 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3950 return( ret );
3951
3952 continue;
3953 }
3954 max_hs_frag_len = max_frag_len - 12;
3955
3956 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3957 max_hs_frag_len : rem_len;
3958
3959 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003960 {
3961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003962 (unsigned) cur_hs_frag_len,
3963 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003964 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003965
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003966 /* Messages are stored with handshake headers as if not fragmented,
3967 * copy beginning of headers then fill fragmentation fields.
3968 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3969 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003970
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003971 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3972 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3973 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3974
Hanno Becker67bc7c32018-08-06 11:33:50 +01003975 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3976 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3977 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003978
3979 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3980
Hanno Becker3f7b9732018-08-28 09:53:25 +01003981 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003982 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3983 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003984 ssl->out_msgtype = cur->type;
3985
3986 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003987 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003988 }
3989
3990 /* If done with the current message move to the next one if any */
3991 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3992 {
3993 if( cur->next != NULL )
3994 {
3995 ssl->handshake->cur_msg = cur->next;
3996 ssl->handshake->cur_msg_p = cur->next->p + 12;
3997 }
3998 else
3999 {
4000 ssl->handshake->cur_msg = NULL;
4001 ssl->handshake->cur_msg_p = NULL;
4002 }
4003 }
4004
4005 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004006 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004009 return( ret );
4010 }
4011 }
4012
Hanno Becker67bc7c32018-08-06 11:33:50 +01004013 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4014 return( ret );
4015
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004016 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4018 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004019 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004022 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4023 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004024
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004026
4027 return( 0 );
4028}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004029
4030/*
4031 * To be called when the last message of an incoming flight is received.
4032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004034{
4035 /* We won't need to resend that one any more */
4036 ssl_flight_free( ssl->handshake->flight );
4037 ssl->handshake->flight = NULL;
4038 ssl->handshake->cur_msg = NULL;
4039
4040 /* The next incoming flight will start with this msg_seq */
4041 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4042
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004043 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004044 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004045
Hanno Becker0271f962018-08-16 13:23:47 +01004046 /* Clear future message buffering structure. */
4047 ssl_buffering_free( ssl );
4048
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004049 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004050 ssl_set_timer( ssl, 0 );
4051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4053 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004056 }
4057 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004059}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004060
4061/*
4062 * To be called when the last message of an outgoing flight is send.
4063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004065{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004066 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004067 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4070 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004073 }
4074 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004076}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004077#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004078
Paul Bakker5121ce52009-01-03 21:22:43 +00004079/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004080 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004081 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004082
4083/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004084 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004085 *
4086 * - fill in handshake headers
4087 * - update handshake checksum
4088 * - DTLS: save message for resending
4089 * - then pass to the record layer
4090 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004091 * DTLS: except for HelloRequest, messages are only queued, and will only be
4092 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004093 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004094 * Inputs:
4095 * - ssl->out_msglen: 4 + actual handshake message len
4096 * (4 is the size of handshake headers for TLS)
4097 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4098 * - ssl->out_msg + 4: the handshake message body
4099 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004100 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004101 * - ssl->out_msglen: the length of the record contents
4102 * (including handshake headers but excluding record headers)
4103 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004104 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004105int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004106{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004107 int ret;
4108 const size_t hs_len = ssl->out_msglen - 4;
4109 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004110
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4112
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004113 /*
4114 * Sanity checks
4115 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004116 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004117 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4118 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004119 /* In SSLv3, the client might send a NoCertificate alert. */
4120#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4121 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4122 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4123 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4124#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4125 {
4126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4127 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4128 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004129 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004130
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004131 /* Whenever we send anything different from a
4132 * HelloRequest we should be in a handshake - double check. */
4133 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4134 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004135 ssl->handshake == NULL )
4136 {
4137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4138 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4139 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004141#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004142 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004143 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004145 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4147 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004148 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004149#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004150
Hanno Beckerb50a2532018-08-06 11:52:54 +01004151 /* Double-check that we did not exceed the bounds
4152 * of the outgoing record buffer.
4153 * This should never fail as the various message
4154 * writing functions must obey the bounds of the
4155 * outgoing record buffer, but better be safe.
4156 *
4157 * Note: We deliberately do not check for the MTU or MFL here.
4158 */
4159 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4160 {
4161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4162 "size %u, maximum %u",
4163 (unsigned) ssl->out_msglen,
4164 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4166 }
4167
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004168 /*
4169 * Fill handshake headers
4170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004172 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004173 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4174 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4175 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004176
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004177 /*
4178 * DTLS has additional fields in the Handshake layer,
4179 * between the length field and the actual payload:
4180 * uint16 message_seq;
4181 * uint24 fragment_offset;
4182 * uint24 fragment_length;
4183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004184#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004185 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004186 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004187 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004188 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004189 {
4190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4191 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004192 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004193 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4195 }
4196
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004197 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004198 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004199
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004200 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004201 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004202 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004203 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4204 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4205 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004206 }
4207 else
4208 {
4209 ssl->out_msg[4] = 0;
4210 ssl->out_msg[5] = 0;
4211 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004212
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004213 /* Handshake hashes are computed without fragmentation,
4214 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004215 memset( ssl->out_msg + 6, 0x00, 3 );
4216 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004217 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004219
Hanno Becker0207e532018-08-28 10:28:28 +01004220 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004221 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4222 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004223 }
4224
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004225 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004227 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004228 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4229 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004230 {
4231 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004234 return( ret );
4235 }
4236 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004237 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004238#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004239 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004240 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004241 {
4242 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4243 return( ret );
4244 }
4245 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004246
4247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4248
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004249 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004250}
4251
4252/*
4253 * Record layer functions
4254 */
4255
4256/*
4257 * Write current record.
4258 *
4259 * Uses:
4260 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4261 * - ssl->out_msglen: length of the record content (excl headers)
4262 * - ssl->out_msg: record content
4263 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004264int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004265{
4266 int ret, done = 0;
4267 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004268 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004269
4270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004273 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004275 {
4276 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004279 return( ret );
4280 }
4281
4282 len = ssl->out_msglen;
4283 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4287 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 ret = mbedtls_ssl_hw_record_write( ssl );
4292 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4295 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004296 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004297
4298 if( ret == 0 )
4299 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004300 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004302 if( !done )
4303 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004304 unsigned i;
4305 size_t protected_record_size;
4306
Hanno Becker6430faf2019-05-08 11:57:13 +01004307 /* Skip writing the record content type to after the encryption,
4308 * as it may change when using the CID extension. */
4309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004311 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004312
Hanno Becker19859472018-08-06 09:40:20 +01004313 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004314 ssl->out_len[0] = (unsigned char)( len >> 8 );
4315 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004316
Paul Bakker48916f92012-09-16 19:57:18 +00004317 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004318 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004319 mbedtls_record rec;
4320
4321 rec.buf = ssl->out_iv;
4322 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4323 ( ssl->out_iv - ssl->out_buf );
4324 rec.data_len = ssl->out_msglen;
4325 rec.data_offset = ssl->out_msg - rec.buf;
4326
4327 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4328 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4329 ssl->conf->transport, rec.ver );
4330 rec.type = ssl->out_msgtype;
4331
Hanno Beckera0e20d02019-05-15 14:03:01 +01004332#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004333 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004334 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004335#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004336
Hanno Beckera18d1322018-01-03 14:27:32 +00004337 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004338 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004341 return( ret );
4342 }
4343
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004344 if( rec.data_offset != 0 )
4345 {
4346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4347 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4348 }
4349
Hanno Becker6430faf2019-05-08 11:57:13 +01004350 /* Update the record content type and CID. */
4351 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004352#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004353 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004354#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004355 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004356 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4357 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004358 }
4359
Hanno Becker5903de42019-05-03 14:46:38 +01004360 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004361
4362#if defined(MBEDTLS_SSL_PROTO_DTLS)
4363 /* In case of DTLS, double-check that we don't exceed
4364 * the remaining space in the datagram. */
4365 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4366 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004367 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004368 if( ret < 0 )
4369 return( ret );
4370
4371 if( protected_record_size > (size_t) ret )
4372 {
4373 /* Should never happen */
4374 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4375 }
4376 }
4377#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004378
Hanno Becker6430faf2019-05-08 11:57:13 +01004379 /* Now write the potentially updated record content type. */
4380 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004383 "version = [%d:%d], msglen = %d",
4384 ssl->out_hdr[0], ssl->out_hdr[1],
4385 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004388 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004389
4390 ssl->out_left += protected_record_size;
4391 ssl->out_hdr += protected_record_size;
4392 ssl_update_out_pointers( ssl, ssl->transform_out );
4393
Hanno Becker04484622018-08-06 09:49:38 +01004394 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4395 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4396 break;
4397
4398 /* The loop goes to its end iff the counter is wrapping */
4399 if( i == ssl_ep_len( ssl ) )
4400 {
4401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4402 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4403 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004404 }
4405
Hanno Becker67bc7c32018-08-06 11:33:50 +01004406#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004407 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4408 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004409 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004410 size_t remaining;
4411 ret = ssl_get_remaining_payload_in_datagram( ssl );
4412 if( ret < 0 )
4413 {
4414 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4415 ret );
4416 return( ret );
4417 }
4418
4419 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004420 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004421 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004422 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004423 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004424 else
4425 {
Hanno Becker513815a2018-08-20 11:56:09 +01004426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004427 }
4428 }
4429#endif /* MBEDTLS_SSL_PROTO_DTLS */
4430
4431 if( ( flush == SSL_FORCE_FLUSH ) &&
4432 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004435 return( ret );
4436 }
4437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004439
4440 return( 0 );
4441}
4442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004444
4445static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4446{
4447 if( ssl->in_msglen < ssl->in_hslen ||
4448 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4449 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4450 {
4451 return( 1 );
4452 }
4453 return( 0 );
4454}
Hanno Becker44650b72018-08-16 12:51:11 +01004455
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004456static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004457{
4458 return( ( ssl->in_msg[9] << 16 ) |
4459 ( ssl->in_msg[10] << 8 ) |
4460 ssl->in_msg[11] );
4461}
4462
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004463static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004464{
4465 return( ( ssl->in_msg[6] << 16 ) |
4466 ( ssl->in_msg[7] << 8 ) |
4467 ssl->in_msg[8] );
4468}
4469
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004470static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004471{
4472 uint32_t msg_len, frag_off, frag_len;
4473
4474 msg_len = ssl_get_hs_total_len( ssl );
4475 frag_off = ssl_get_hs_frag_off( ssl );
4476 frag_len = ssl_get_hs_frag_len( ssl );
4477
4478 if( frag_off > msg_len )
4479 return( -1 );
4480
4481 if( frag_len > msg_len - frag_off )
4482 return( -1 );
4483
4484 if( frag_len + 12 > ssl->in_msglen )
4485 return( -1 );
4486
4487 return( 0 );
4488}
4489
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004490/*
4491 * Mark bits in bitmask (used for DTLS HS reassembly)
4492 */
4493static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4494{
4495 unsigned int start_bits, end_bits;
4496
4497 start_bits = 8 - ( offset % 8 );
4498 if( start_bits != 8 )
4499 {
4500 size_t first_byte_idx = offset / 8;
4501
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004502 /* Special case */
4503 if( len <= start_bits )
4504 {
4505 for( ; len != 0; len-- )
4506 mask[first_byte_idx] |= 1 << ( start_bits - len );
4507
4508 /* Avoid potential issues with offset or len becoming invalid */
4509 return;
4510 }
4511
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004512 offset += start_bits; /* Now offset % 8 == 0 */
4513 len -= start_bits;
4514
4515 for( ; start_bits != 0; start_bits-- )
4516 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4517 }
4518
4519 end_bits = len % 8;
4520 if( end_bits != 0 )
4521 {
4522 size_t last_byte_idx = ( offset + len ) / 8;
4523
4524 len -= end_bits; /* Now len % 8 == 0 */
4525
4526 for( ; end_bits != 0; end_bits-- )
4527 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4528 }
4529
4530 memset( mask + offset / 8, 0xFF, len / 8 );
4531}
4532
4533/*
4534 * Check that bitmask is full
4535 */
4536static int ssl_bitmask_check( unsigned char *mask, size_t len )
4537{
4538 size_t i;
4539
4540 for( i = 0; i < len / 8; i++ )
4541 if( mask[i] != 0xFF )
4542 return( -1 );
4543
4544 for( i = 0; i < len % 8; i++ )
4545 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4546 return( -1 );
4547
4548 return( 0 );
4549}
4550
Hanno Becker56e205e2018-08-16 09:06:12 +01004551/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004552static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004553 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004554{
Hanno Becker56e205e2018-08-16 09:06:12 +01004555 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004556
Hanno Becker56e205e2018-08-16 09:06:12 +01004557 alloc_len = 12; /* Handshake header */
4558 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004559
Hanno Beckerd07df862018-08-16 09:14:58 +01004560 if( add_bitmap )
4561 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004562
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004563 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004564}
Hanno Becker56e205e2018-08-16 09:06:12 +01004565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004567
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004568static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004569{
4570 return( ( ssl->in_msg[1] << 16 ) |
4571 ( ssl->in_msg[2] << 8 ) |
4572 ssl->in_msg[3] );
4573}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004574
Simon Butcher99000142016-10-13 17:21:01 +01004575int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004576{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004580 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004582 }
4583
Hanno Becker12555c62018-08-16 12:47:53 +01004584 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004587 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004588 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004591 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004592 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004593 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004594 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004595
Hanno Becker44650b72018-08-16 12:51:11 +01004596 if( ssl_check_hs_header( ssl ) != 0 )
4597 {
4598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4599 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4600 }
4601
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004602 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004603 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4604 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4605 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4606 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004607 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004608 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4609 {
4610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4611 recv_msg_seq,
4612 ssl->handshake->in_msg_seq ) );
4613 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4614 }
4615
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004616 /* Retransmit only on last message from previous flight, to avoid
4617 * too many retransmissions.
4618 * Besides, No sane server ever retransmits HelloVerifyRequest */
4619 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004623 "message_seq = %d, start_of_flight = %d",
4624 recv_msg_seq,
4625 ssl->handshake->in_flight_start_seq ) );
4626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004630 return( ret );
4631 }
4632 }
4633 else
4634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004636 "message_seq = %d, expected = %d",
4637 recv_msg_seq,
4638 ssl->handshake->in_msg_seq ) );
4639 }
4640
Hanno Becker90333da2017-10-10 11:27:13 +01004641 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004642 }
4643 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004644
Hanno Becker6d97ef52018-08-16 13:09:04 +01004645 /* Message reassembly is handled alongside buffering of future
4646 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004647 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004648 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004649 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004652 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004653 }
4654 }
4655 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004657 /* With TLS we don't handle fragmentation (for now) */
4658 if( ssl->in_msglen < ssl->in_hslen )
4659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4661 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004662 }
4663
Simon Butcher99000142016-10-13 17:21:01 +01004664 return( 0 );
4665}
4666
4667void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4668{
Hanno Becker0271f962018-08-16 13:23:47 +01004669 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004670
Hanno Becker0271f962018-08-16 13:23:47 +01004671 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004672 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004673 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004674 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004675
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004676 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004678 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004679 ssl->handshake != NULL )
4680 {
Hanno Becker0271f962018-08-16 13:23:47 +01004681 unsigned offset;
4682 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004683
Hanno Becker0271f962018-08-16 13:23:47 +01004684 /* Increment handshake sequence number */
4685 hs->in_msg_seq++;
4686
4687 /*
4688 * Clear up handshake buffering and reassembly structure.
4689 */
4690
4691 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004692 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004693
4694 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004695 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4696 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004697 offset++, hs_buf++ )
4698 {
4699 *hs_buf = *(hs_buf + 1);
4700 }
4701
4702 /* Create a fresh last entry */
4703 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004704 }
4705#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004706}
4707
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004708/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004709 * DTLS anti-replay: RFC 6347 4.1.2.6
4710 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004711 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4712 * Bit n is set iff record number in_window_top - n has been seen.
4713 *
4714 * Usually, in_window_top is the last record number seen and the lsb of
4715 * in_window is set. The only exception is the initial state (record number 0
4716 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004718#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4719static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004720{
4721 ssl->in_window_top = 0;
4722 ssl->in_window = 0;
4723}
4724
4725static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4726{
4727 return( ( (uint64_t) buf[0] << 40 ) |
4728 ( (uint64_t) buf[1] << 32 ) |
4729 ( (uint64_t) buf[2] << 24 ) |
4730 ( (uint64_t) buf[3] << 16 ) |
4731 ( (uint64_t) buf[4] << 8 ) |
4732 ( (uint64_t) buf[5] ) );
4733}
4734
4735/*
4736 * Return 0 if sequence number is acceptable, -1 otherwise
4737 */
Hanno Becker0183d692019-07-12 08:50:37 +01004738int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004739{
4740 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4741 uint64_t bit;
4742
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004743 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004744 return( 0 );
4745
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004746 if( rec_seqnum > ssl->in_window_top )
4747 return( 0 );
4748
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004749 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004750
4751 if( bit >= 64 )
4752 return( -1 );
4753
4754 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4755 return( -1 );
4756
4757 return( 0 );
4758}
4759
4760/*
4761 * Update replay window on new validated record
4762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004764{
4765 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4766
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004767 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004768 return;
4769
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004770 if( rec_seqnum > ssl->in_window_top )
4771 {
4772 /* Update window_top and the contents of the window */
4773 uint64_t shift = rec_seqnum - ssl->in_window_top;
4774
4775 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004776 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004777 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004778 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004779 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004780 ssl->in_window |= 1;
4781 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004782
4783 ssl->in_window_top = rec_seqnum;
4784 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004785 else
4786 {
4787 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004788 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004789
4790 if( bit < 64 ) /* Always true, but be extra sure */
4791 ssl->in_window |= (uint64_t) 1 << bit;
4792 }
4793}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004795
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004796#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004797/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004798static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4799
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004800/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004801 * Without any SSL context, check if a datagram looks like a ClientHello with
4802 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004803 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004804 *
4805 * - if cookie is valid, return 0
4806 * - if ClientHello looks superficially valid but cookie is not,
4807 * fill obuf and set olen, then
4808 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4809 * - otherwise return a specific error code
4810 */
4811static int ssl_check_dtls_clihlo_cookie(
4812 mbedtls_ssl_cookie_write_t *f_cookie_write,
4813 mbedtls_ssl_cookie_check_t *f_cookie_check,
4814 void *p_cookie,
4815 const unsigned char *cli_id, size_t cli_id_len,
4816 const unsigned char *in, size_t in_len,
4817 unsigned char *obuf, size_t buf_len, size_t *olen )
4818{
4819 size_t sid_len, cookie_len;
4820 unsigned char *p;
4821
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004822 /*
4823 * Structure of ClientHello with record and handshake headers,
4824 * and expected values. We don't need to check a lot, more checks will be
4825 * done when actually parsing the ClientHello - skipping those checks
4826 * avoids code duplication and does not make cookie forging any easier.
4827 *
4828 * 0-0 ContentType type; copied, must be handshake
4829 * 1-2 ProtocolVersion version; copied
4830 * 3-4 uint16 epoch; copied, must be 0
4831 * 5-10 uint48 sequence_number; copied
4832 * 11-12 uint16 length; (ignored)
4833 *
4834 * 13-13 HandshakeType msg_type; (ignored)
4835 * 14-16 uint24 length; (ignored)
4836 * 17-18 uint16 message_seq; copied
4837 * 19-21 uint24 fragment_offset; copied, must be 0
4838 * 22-24 uint24 fragment_length; (ignored)
4839 *
4840 * 25-26 ProtocolVersion client_version; (ignored)
4841 * 27-58 Random random; (ignored)
4842 * 59-xx SessionID session_id; 1 byte len + sid_len content
4843 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4844 * ...
4845 *
4846 * Minimum length is 61 bytes.
4847 */
4848 if( in_len < 61 ||
4849 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4850 in[3] != 0 || in[4] != 0 ||
4851 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4852 {
4853 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4854 }
4855
4856 sid_len = in[59];
4857 if( sid_len > in_len - 61 )
4858 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4859
4860 cookie_len = in[60 + sid_len];
4861 if( cookie_len > in_len - 60 )
4862 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4863
4864 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4865 cli_id, cli_id_len ) == 0 )
4866 {
4867 /* Valid cookie */
4868 return( 0 );
4869 }
4870
4871 /*
4872 * If we get here, we've got an invalid cookie, let's prepare HVR.
4873 *
4874 * 0-0 ContentType type; copied
4875 * 1-2 ProtocolVersion version; copied
4876 * 3-4 uint16 epoch; copied
4877 * 5-10 uint48 sequence_number; copied
4878 * 11-12 uint16 length; olen - 13
4879 *
4880 * 13-13 HandshakeType msg_type; hello_verify_request
4881 * 14-16 uint24 length; olen - 25
4882 * 17-18 uint16 message_seq; copied
4883 * 19-21 uint24 fragment_offset; copied
4884 * 22-24 uint24 fragment_length; olen - 25
4885 *
4886 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4887 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4888 *
4889 * Minimum length is 28.
4890 */
4891 if( buf_len < 28 )
4892 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4893
4894 /* Copy most fields and adapt others */
4895 memcpy( obuf, in, 25 );
4896 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4897 obuf[25] = 0xfe;
4898 obuf[26] = 0xff;
4899
4900 /* Generate and write actual cookie */
4901 p = obuf + 28;
4902 if( f_cookie_write( p_cookie,
4903 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4904 {
4905 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4906 }
4907
4908 *olen = p - obuf;
4909
4910 /* Go back and fill length fields */
4911 obuf[27] = (unsigned char)( *olen - 28 );
4912
4913 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4914 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4915 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4916
4917 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4918 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4919
4920 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4921}
4922
4923/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004924 * Handle possible client reconnect with the same UDP quadruplet
4925 * (RFC 6347 Section 4.2.8).
4926 *
4927 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4928 * that looks like a ClientHello.
4929 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004930 * - if the input looks like a ClientHello without cookies,
4931 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004932 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004933 * - if the input looks like a ClientHello with a valid cookie,
4934 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004935 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004936 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004937 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004938 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004939 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4940 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004941 */
4942static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4943{
4944 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004945 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004946
Hanno Becker2fddd372019-07-10 14:37:41 +01004947 if( ssl->conf->f_cookie_write == NULL ||
4948 ssl->conf->f_cookie_check == NULL )
4949 {
4950 /* If we can't use cookies to verify reachability of the peer,
4951 * drop the record. */
4952 return( 0 );
4953 }
4954
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004955 ret = ssl_check_dtls_clihlo_cookie(
4956 ssl->conf->f_cookie_write,
4957 ssl->conf->f_cookie_check,
4958 ssl->conf->p_cookie,
4959 ssl->cli_id, ssl->cli_id_len,
4960 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004961 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004962
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004963 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4964
4965 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004966 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004967 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004968 * If the error is permanent we'll catch it later,
4969 * if it's not, then hopefully it'll work next time. */
4970 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004971 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004972 }
4973
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004974 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004975 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004976 /* Got a valid cookie, partially reset context */
4977 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4978 {
4979 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4980 return( ret );
4981 }
4982
4983 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004984 }
4985
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004986 return( ret );
4987}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004988#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004989
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004990static int ssl_check_record_type( uint8_t record_type )
4991{
4992 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4993 record_type != MBEDTLS_SSL_MSG_ALERT &&
4994 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4995 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4996 {
4997 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4998 }
4999
5000 return( 0 );
5001}
5002
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005003/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005004 * ContentType type;
5005 * ProtocolVersion version;
5006 * uint16 epoch; // DTLS only
5007 * uint48 sequence_number; // DTLS only
5008 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005009 *
5010 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005011 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005012 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5013 *
5014 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005015 * 1. proceed with the record if this function returns 0
5016 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5017 * 3. return CLIENT_RECONNECT if this function return that value
5018 * 4. drop the whole datagram if this function returns anything else.
5019 * Point 2 is needed when the peer is resending, and we have already received
5020 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005021 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005022static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005023 unsigned char *buf,
5024 size_t len,
5025 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005026{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005027 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005028
Hanno Beckere5e7e782019-07-11 12:29:35 +01005029 size_t const rec_hdr_type_offset = 0;
5030 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005031
Hanno Beckere5e7e782019-07-11 12:29:35 +01005032 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5033 rec_hdr_type_len;
5034 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005035
Hanno Beckere5e7e782019-07-11 12:29:35 +01005036 size_t const rec_hdr_ctr_len = 8;
5037#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005038 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005039 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5040 rec_hdr_version_len;
5041
Hanno Beckera0e20d02019-05-15 14:03:01 +01005042#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005043 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5044 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005045 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005046#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5047#endif /* MBEDTLS_SSL_PROTO_DTLS */
5048
5049 size_t rec_hdr_len_offset; /* To be determined */
5050 size_t const rec_hdr_len_len = 2;
5051
5052 /*
5053 * Check minimum lengths for record header.
5054 */
5055
5056#if defined(MBEDTLS_SSL_PROTO_DTLS)
5057 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5058 {
5059 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5060 }
5061 else
5062#endif /* MBEDTLS_SSL_PROTO_DTLS */
5063 {
5064 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5065 }
5066
5067 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5068 {
5069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5070 (unsigned) len,
5071 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5072 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5073 }
5074
5075 /*
5076 * Parse and validate record content type
5077 */
5078
5079 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005080
5081 /* Check record content type */
5082#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5083 rec->cid_len = 0;
5084
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005085 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005086 ssl->conf->cid_len != 0 &&
5087 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005088 {
5089 /* Shift pointers to account for record header including CID
5090 * struct {
5091 * ContentType special_type = tls12_cid;
5092 * ProtocolVersion version;
5093 * uint16 epoch;
5094 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005095 * opaque cid[cid_length]; // Additional field compared to
5096 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005097 * uint16 length;
5098 * opaque enc_content[DTLSCiphertext.length];
5099 * } DTLSCiphertext;
5100 */
5101
5102 /* So far, we only support static CID lengths
5103 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005104 rec_hdr_cid_len = ssl->conf->cid_len;
5105 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005106
Hanno Beckere5e7e782019-07-11 12:29:35 +01005107 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005108 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5110 (unsigned) len,
5111 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005112 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005113 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005114
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005115 /* configured CID len is guaranteed at most 255, see
5116 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5117 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005118 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005119 }
5120 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005121#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005122 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005123 if( ssl_check_record_type( rec->type ) )
5124 {
Hanno Becker54229812019-07-12 14:40:00 +01005125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5126 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005127 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5128 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005129 }
5130
Hanno Beckere5e7e782019-07-11 12:29:35 +01005131 /*
5132 * Parse and validate record version
5133 */
5134
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005135 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5136 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005137 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5138 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005139 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005140
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005141 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5144 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005145 }
5146
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005147 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5150 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005151 }
5152
Hanno Beckere5e7e782019-07-11 12:29:35 +01005153 /*
5154 * Parse/Copy record sequence number.
5155 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005156
Hanno Beckere5e7e782019-07-11 12:29:35 +01005157#if defined(MBEDTLS_SSL_PROTO_DTLS)
5158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005159 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005160 /* Copy explicit record sequence number from input buffer. */
5161 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5162 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005163 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005164 else
5165#endif /* MBEDTLS_SSL_PROTO_DTLS */
5166 {
5167 /* Copy implicit record sequence number from SSL context structure. */
5168 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5169 }
Paul Bakker40e46942009-01-03 21:51:57 +00005170
Hanno Beckere5e7e782019-07-11 12:29:35 +01005171 /*
5172 * Parse record length.
5173 */
5174
Hanno Beckere5e7e782019-07-11 12:29:35 +01005175 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005176 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5177 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005178 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005179
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005180 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005181 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005182 rec->type,
5183 major_ver, minor_ver, rec->data_len ) );
5184
5185 rec->buf = buf;
5186 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005187
Hanno Beckerd417cc92019-07-26 08:20:27 +01005188 if( rec->data_len == 0 )
5189 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005190
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005191 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005192 * DTLS-related tests.
5193 * Check epoch before checking length constraint because
5194 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5195 * message gets duplicated before the corresponding Finished message,
5196 * the second ChangeCipherSpec should be discarded because it belongs
5197 * to an old epoch, but not because its length is shorter than
5198 * the minimum record length for packets using the new record transform.
5199 * Note that these two kinds of failures are handled differently,
5200 * as an unexpected record is silently skipped but an invalid
5201 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005202 */
5203#if defined(MBEDTLS_SSL_PROTO_DTLS)
5204 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5205 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005206 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005207
Hanno Becker955a5c92019-07-10 17:12:07 +01005208 /* Check that the datagram is large enough to contain a record
5209 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005210 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005211 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5213 (unsigned) len,
5214 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005215 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5216 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005217
Hanno Becker37cfe732019-07-10 17:20:01 +01005218 /* Records from other, non-matching epochs are silently discarded.
5219 * (The case of same-port Client reconnects must be considered in
5220 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005221 if( rec_epoch != ssl->in_epoch )
5222 {
5223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5224 "expected %d, received %d",
5225 ssl->in_epoch, rec_epoch ) );
5226
Hanno Becker552f7472019-07-19 10:59:12 +01005227 /* Records from the next epoch are considered for buffering
5228 * (concretely: early Finished messages). */
5229 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005230 {
Hanno Becker552f7472019-07-19 10:59:12 +01005231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5232 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005233 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005234
Hanno Becker2fddd372019-07-10 14:37:41 +01005235 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005236 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005237#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005238 /* For records from the correct epoch, check whether their
5239 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005240 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005241 {
5242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5243 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5244 }
5245#endif
5246 }
5247#endif /* MBEDTLS_SSL_PROTO_DTLS */
5248
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005249 return( 0 );
5250}
Paul Bakker5121ce52009-01-03 21:22:43 +00005251
Paul Bakker5121ce52009-01-03 21:22:43 +00005252
Hanno Becker2fddd372019-07-10 14:37:41 +01005253#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5254static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5255{
5256 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5257
5258 /*
5259 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5260 * access the first byte of record content (handshake type), as we
5261 * have an active transform (possibly iv_len != 0), so use the
5262 * fact that the record header len is 13 instead.
5263 */
5264 if( rec_epoch == 0 &&
5265 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5266 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5267 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5268 ssl->in_left > 13 &&
5269 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5270 {
5271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5272 "from the same port" ) );
5273 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005274 }
5275
5276 return( 0 );
5277}
Hanno Becker2fddd372019-07-10 14:37:41 +01005278#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005279
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005280/*
5281 * If applicable, decrypt (and decompress) record content
5282 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005283static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5284 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005285{
5286 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005289 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5292 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296 ret = mbedtls_ssl_hw_record_read( ssl );
5297 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5300 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005301 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005302
5303 if( ret == 0 )
5304 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005305 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005307 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005308 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005309 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005310
Hanno Beckera18d1322018-01-03 14:27:32 +00005311 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005312 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005315
Hanno Beckera0e20d02019-05-15 14:03:01 +01005316#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005317 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5318 ssl->conf->ignore_unexpected_cid
5319 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5320 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005322 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005323 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005324#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005325
Paul Bakker5121ce52009-01-03 21:22:43 +00005326 return( ret );
5327 }
5328
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005329 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005330 {
5331 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005332 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005333 }
5334
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005335 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005336 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005337
Hanno Beckera0e20d02019-05-15 14:03:01 +01005338#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005339 /* We have already checked the record content type
5340 * in ssl_parse_record_header(), failing or silently
5341 * dropping the record in the case of an unknown type.
5342 *
5343 * Since with the use of CIDs, the record content type
5344 * might change during decryption, re-check the record
5345 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005346 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005347 {
5348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5349 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5350 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005351#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005352
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005353 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005354 {
5355#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5356 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005357 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005358 {
5359 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5361 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5362 }
5363#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5364
5365 ssl->nb_zero++;
5366
5367 /*
5368 * Three or more empty messages may be a DoS attack
5369 * (excessive CPU consumption).
5370 */
5371 if( ssl->nb_zero > 3 )
5372 {
5373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005374 "messages, possible DoS attack" ) );
5375 /* Treat the records as if they were not properly authenticated,
5376 * thereby failing the connection if we see more than allowed
5377 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005378 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5379 }
5380 }
5381 else
5382 ssl->nb_zero = 0;
5383
5384#if defined(MBEDTLS_SSL_PROTO_DTLS)
5385 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5386 {
5387 ; /* in_ctr read from peer, not maintained internally */
5388 }
5389 else
5390#endif
5391 {
5392 unsigned i;
5393 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5394 if( ++ssl->in_ctr[i - 1] != 0 )
5395 break;
5396
5397 /* The loop goes to its end iff the counter is wrapping */
5398 if( i == ssl_ep_len( ssl ) )
5399 {
5400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5401 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5402 }
5403 }
5404
Paul Bakker5121ce52009-01-03 21:22:43 +00005405 }
5406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005408 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005409 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005410 {
5411 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005414 return( ret );
5415 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005416 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005420 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005423 }
5424#endif
5425
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005426 /* Check actual (decrypted) record content length against
5427 * configured maximum. */
5428 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5429 {
5430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5431 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5432 }
5433
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005434 return( 0 );
5435}
5436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005438
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005439/*
5440 * Read a record.
5441 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005442 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5443 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5444 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005445 */
Hanno Becker1097b342018-08-15 14:09:41 +01005446
5447/* Helper functions for mbedtls_ssl_read_record(). */
5448static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005449static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5450static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005451
Hanno Becker327c93b2018-08-15 13:56:18 +01005452int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005453 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005454{
5455 int ret;
5456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005458
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005459 if( ssl->keep_current_message == 0 )
5460 {
5461 do {
Simon Butcher99000142016-10-13 17:21:01 +01005462
Hanno Becker26994592018-08-15 14:14:59 +01005463 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005464 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005465 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005466
Hanno Beckere74d5562018-08-15 14:26:08 +01005467 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005468 {
Hanno Becker40f50842018-08-15 14:48:01 +01005469#if defined(MBEDTLS_SSL_PROTO_DTLS)
5470 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005471
Hanno Becker40f50842018-08-15 14:48:01 +01005472 /* We only check for buffered messages if the
5473 * current datagram is fully consumed. */
5474 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005475 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005476 {
Hanno Becker40f50842018-08-15 14:48:01 +01005477 if( ssl_load_buffered_message( ssl ) == 0 )
5478 have_buffered = 1;
5479 }
5480
5481 if( have_buffered == 0 )
5482#endif /* MBEDTLS_SSL_PROTO_DTLS */
5483 {
5484 ret = ssl_get_next_record( ssl );
5485 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5486 continue;
5487
5488 if( ret != 0 )
5489 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005490 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005491 return( ret );
5492 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005493 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005494 }
5495
5496 ret = mbedtls_ssl_handle_message_type( ssl );
5497
Hanno Becker40f50842018-08-15 14:48:01 +01005498#if defined(MBEDTLS_SSL_PROTO_DTLS)
5499 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5500 {
5501 /* Buffer future message */
5502 ret = ssl_buffer_message( ssl );
5503 if( ret != 0 )
5504 return( ret );
5505
5506 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5507 }
5508#endif /* MBEDTLS_SSL_PROTO_DTLS */
5509
Hanno Becker90333da2017-10-10 11:27:13 +01005510 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5511 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005512
5513 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005514 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005515 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005516 return( ret );
5517 }
5518
Hanno Becker327c93b2018-08-15 13:56:18 +01005519 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005520 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005521 {
5522 mbedtls_ssl_update_handshake_status( ssl );
5523 }
Simon Butcher99000142016-10-13 17:21:01 +01005524 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005525 else
Simon Butcher99000142016-10-13 17:21:01 +01005526 {
Hanno Becker02f59072018-08-15 14:00:24 +01005527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005528 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005529 }
5530
5531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5532
5533 return( 0 );
5534}
5535
Hanno Becker40f50842018-08-15 14:48:01 +01005536#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005537static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005538{
Hanno Becker40f50842018-08-15 14:48:01 +01005539 if( ssl->in_left > ssl->next_record_offset )
5540 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005541
Hanno Becker40f50842018-08-15 14:48:01 +01005542 return( 0 );
5543}
5544
5545static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5546{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005547 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005548 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005549 int ret = 0;
5550
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005551 if( hs == NULL )
5552 return( -1 );
5553
Hanno Beckere00ae372018-08-20 09:39:42 +01005554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5555
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005556 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5557 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5558 {
5559 /* Check if we have seen a ChangeCipherSpec before.
5560 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005561 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005562 {
5563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5564 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005565 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005566 }
5567
Hanno Becker39b8bc92018-08-28 17:17:13 +01005568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005569 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5570 ssl->in_msglen = 1;
5571 ssl->in_msg[0] = 1;
5572
5573 /* As long as they are equal, the exact value doesn't matter. */
5574 ssl->in_left = 0;
5575 ssl->next_record_offset = 0;
5576
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005577 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005578 goto exit;
5579 }
Hanno Becker37f95322018-08-16 13:55:32 +01005580
Hanno Beckerb8f50142018-08-28 10:01:34 +01005581#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005582 /* Debug only */
5583 {
5584 unsigned offset;
5585 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5586 {
5587 hs_buf = &hs->buffering.hs[offset];
5588 if( hs_buf->is_valid == 1 )
5589 {
5590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5591 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005592 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005593 }
5594 }
5595 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005596#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005597
5598 /* Check if we have buffered and/or fully reassembled the
5599 * next handshake message. */
5600 hs_buf = &hs->buffering.hs[0];
5601 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5602 {
5603 /* Synthesize a record containing the buffered HS message. */
5604 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5605 ( hs_buf->data[2] << 8 ) |
5606 hs_buf->data[3];
5607
5608 /* Double-check that we haven't accidentally buffered
5609 * a message that doesn't fit into the input buffer. */
5610 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5611 {
5612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5614 }
5615
5616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5617 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5618 hs_buf->data, msg_len + 12 );
5619
5620 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5621 ssl->in_hslen = msg_len + 12;
5622 ssl->in_msglen = msg_len + 12;
5623 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5624
5625 ret = 0;
5626 goto exit;
5627 }
5628 else
5629 {
5630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5631 hs->in_msg_seq ) );
5632 }
5633
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005634 ret = -1;
5635
5636exit:
5637
5638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5639 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005640}
5641
Hanno Beckera02b0b42018-08-21 17:20:27 +01005642static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5643 size_t desired )
5644{
5645 int offset;
5646 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5648 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005649
Hanno Becker01315ea2018-08-21 17:22:17 +01005650 /* Get rid of future records epoch first, if such exist. */
5651 ssl_free_buffered_record( ssl );
5652
5653 /* Check if we have enough space available now. */
5654 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5655 hs->buffering.total_bytes_buffered ) )
5656 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005658 return( 0 );
5659 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005660
Hanno Becker4f432ad2018-08-28 10:02:32 +01005661 /* We don't have enough space to buffer the next expected handshake
5662 * message. Remove buffers used for future messages to gain space,
5663 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005664 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5665 offset >= 0; offset-- )
5666 {
5667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5668 offset ) );
5669
Hanno Beckerb309b922018-08-23 13:18:05 +01005670 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005671
5672 /* Check if we have enough space available now. */
5673 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5674 hs->buffering.total_bytes_buffered ) )
5675 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005677 return( 0 );
5678 }
5679 }
5680
5681 return( -1 );
5682}
5683
Hanno Becker40f50842018-08-15 14:48:01 +01005684static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5685{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005686 int ret = 0;
5687 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5688
5689 if( hs == NULL )
5690 return( 0 );
5691
5692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5693
5694 switch( ssl->in_msgtype )
5695 {
5696 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005698
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005699 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005700 break;
5701
5702 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005703 {
5704 unsigned recv_msg_seq_offset;
5705 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5706 mbedtls_ssl_hs_buffer *hs_buf;
5707 size_t msg_len = ssl->in_hslen - 12;
5708
5709 /* We should never receive an old handshake
5710 * message - double-check nonetheless. */
5711 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5712 {
5713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5714 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5715 }
5716
5717 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5718 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5719 {
5720 /* Silently ignore -- message too far in the future */
5721 MBEDTLS_SSL_DEBUG_MSG( 2,
5722 ( "Ignore future HS message with sequence number %u, "
5723 "buffering window %u - %u",
5724 recv_msg_seq, ssl->handshake->in_msg_seq,
5725 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5726
5727 goto exit;
5728 }
5729
5730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5731 recv_msg_seq, recv_msg_seq_offset ) );
5732
5733 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5734
5735 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005736 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005737 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005738 size_t reassembly_buf_sz;
5739
Hanno Becker37f95322018-08-16 13:55:32 +01005740 hs_buf->is_fragmented =
5741 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5742
5743 /* We copy the message back into the input buffer
5744 * after reassembly, so check that it's not too large.
5745 * This is an implementation-specific limitation
5746 * and not one from the standard, hence it is not
5747 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005748 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005749 {
5750 /* Ignore message */
5751 goto exit;
5752 }
5753
Hanno Beckere0b150f2018-08-21 15:51:03 +01005754 /* Check if we have enough space to buffer the message. */
5755 if( hs->buffering.total_bytes_buffered >
5756 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5757 {
5758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5759 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5760 }
5761
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005762 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5763 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005764
5765 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5766 hs->buffering.total_bytes_buffered ) )
5767 {
5768 if( recv_msg_seq_offset > 0 )
5769 {
5770 /* If we can't buffer a future message because
5771 * of space limitations -- ignore. */
5772 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",
5773 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5774 (unsigned) hs->buffering.total_bytes_buffered ) );
5775 goto exit;
5776 }
Hanno Beckere1801392018-08-21 16:51:05 +01005777 else
5778 {
5779 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",
5780 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5781 (unsigned) hs->buffering.total_bytes_buffered ) );
5782 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005783
Hanno Beckera02b0b42018-08-21 17:20:27 +01005784 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005785 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005786 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",
5787 (unsigned) msg_len,
5788 (unsigned) reassembly_buf_sz,
5789 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005790 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005791 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5792 goto exit;
5793 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005794 }
5795
5796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5797 msg_len ) );
5798
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005799 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5800 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005801 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005802 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005803 goto exit;
5804 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005805 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005806
5807 /* Prepare final header: copy msg_type, length and message_seq,
5808 * then add standardised fragment_offset and fragment_length */
5809 memcpy( hs_buf->data, ssl->in_msg, 6 );
5810 memset( hs_buf->data + 6, 0, 3 );
5811 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5812
5813 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005814
5815 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005816 }
5817 else
5818 {
5819 /* Make sure msg_type and length are consistent */
5820 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5821 {
5822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5823 /* Ignore */
5824 goto exit;
5825 }
5826 }
5827
Hanno Becker4422bbb2018-08-20 09:40:19 +01005828 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005829 {
5830 size_t frag_len, frag_off;
5831 unsigned char * const msg = hs_buf->data + 12;
5832
5833 /*
5834 * Check and copy current fragment
5835 */
5836
5837 /* Validation of header fields already done in
5838 * mbedtls_ssl_prepare_handshake_record(). */
5839 frag_off = ssl_get_hs_frag_off( ssl );
5840 frag_len = ssl_get_hs_frag_len( ssl );
5841
5842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5843 frag_off, frag_len ) );
5844 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5845
5846 if( hs_buf->is_fragmented )
5847 {
5848 unsigned char * const bitmask = msg + msg_len;
5849 ssl_bitmask_set( bitmask, frag_off, frag_len );
5850 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5851 msg_len ) == 0 );
5852 }
5853 else
5854 {
5855 hs_buf->is_complete = 1;
5856 }
5857
5858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5859 hs_buf->is_complete ? "" : "not yet " ) );
5860 }
5861
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005862 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005863 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005864
5865 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005866 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005867 break;
5868 }
5869
5870exit:
5871
5872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5873 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005874}
5875#endif /* MBEDTLS_SSL_PROTO_DTLS */
5876
Hanno Becker1097b342018-08-15 14:09:41 +01005877static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005878{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005879 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005880 * Consume last content-layer message and potentially
5881 * update in_msglen which keeps track of the contents'
5882 * consumption state.
5883 *
5884 * (1) Handshake messages:
5885 * Remove last handshake message, move content
5886 * and adapt in_msglen.
5887 *
5888 * (2) Alert messages:
5889 * Consume whole record content, in_msglen = 0.
5890 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005891 * (3) Change cipher spec:
5892 * Consume whole record content, in_msglen = 0.
5893 *
5894 * (4) Application data:
5895 * Don't do anything - the record layer provides
5896 * the application data as a stream transport
5897 * and consumes through mbedtls_ssl_read only.
5898 *
5899 */
5900
5901 /* Case (1): Handshake messages */
5902 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005903 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005904 /* Hard assertion to be sure that no application data
5905 * is in flight, as corrupting ssl->in_msglen during
5906 * ssl->in_offt != NULL is fatal. */
5907 if( ssl->in_offt != NULL )
5908 {
5909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5911 }
5912
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005913 /*
5914 * Get next Handshake message in the current record
5915 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005916
Hanno Becker4a810fb2017-05-24 16:27:30 +01005917 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005918 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005919 * current handshake content: If DTLS handshake
5920 * fragmentation is used, that's the fragment
5921 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005922 * size here is faulty and should be changed at
5923 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005924 * (2) While it doesn't seem to cause problems, one
5925 * has to be very careful not to assume that in_hslen
5926 * is always <= in_msglen in a sensible communication.
5927 * Again, it's wrong for DTLS handshake fragmentation.
5928 * The following check is therefore mandatory, and
5929 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005930 * Additionally, ssl->in_hslen might be arbitrarily out of
5931 * bounds after handling a DTLS message with an unexpected
5932 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005933 */
5934 if( ssl->in_hslen < ssl->in_msglen )
5935 {
5936 ssl->in_msglen -= ssl->in_hslen;
5937 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5938 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005939
Hanno Becker4a810fb2017-05-24 16:27:30 +01005940 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5941 ssl->in_msg, ssl->in_msglen );
5942 }
5943 else
5944 {
5945 ssl->in_msglen = 0;
5946 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005947
Hanno Becker4a810fb2017-05-24 16:27:30 +01005948 ssl->in_hslen = 0;
5949 }
5950 /* Case (4): Application data */
5951 else if( ssl->in_offt != NULL )
5952 {
5953 return( 0 );
5954 }
5955 /* Everything else (CCS & Alerts) */
5956 else
5957 {
5958 ssl->in_msglen = 0;
5959 }
5960
Hanno Becker1097b342018-08-15 14:09:41 +01005961 return( 0 );
5962}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005963
Hanno Beckere74d5562018-08-15 14:26:08 +01005964static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5965{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005966 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005967 return( 1 );
5968
5969 return( 0 );
5970}
5971
Hanno Becker5f066e72018-08-16 14:56:31 +01005972#if defined(MBEDTLS_SSL_PROTO_DTLS)
5973
5974static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5975{
5976 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5977 if( hs == NULL )
5978 return;
5979
Hanno Becker01315ea2018-08-21 17:22:17 +01005980 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005981 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005982 hs->buffering.total_bytes_buffered -=
5983 hs->buffering.future_record.len;
5984
5985 mbedtls_free( hs->buffering.future_record.data );
5986 hs->buffering.future_record.data = NULL;
5987 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005988}
5989
5990static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5991{
5992 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5993 unsigned char * rec;
5994 size_t rec_len;
5995 unsigned rec_epoch;
5996
5997 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5998 return( 0 );
5999
6000 if( hs == NULL )
6001 return( 0 );
6002
Hanno Becker5f066e72018-08-16 14:56:31 +01006003 rec = hs->buffering.future_record.data;
6004 rec_len = hs->buffering.future_record.len;
6005 rec_epoch = hs->buffering.future_record.epoch;
6006
6007 if( rec == NULL )
6008 return( 0 );
6009
Hanno Becker4cb782d2018-08-20 11:19:05 +01006010 /* Only consider loading future records if the
6011 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006012 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006013 return( 0 );
6014
Hanno Becker5f066e72018-08-16 14:56:31 +01006015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6016
6017 if( rec_epoch != ssl->in_epoch )
6018 {
6019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6020 goto exit;
6021 }
6022
6023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6024
6025 /* Double-check that the record is not too large */
6026 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6027 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6028 {
6029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6030 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6031 }
6032
6033 memcpy( ssl->in_hdr, rec, rec_len );
6034 ssl->in_left = rec_len;
6035 ssl->next_record_offset = 0;
6036
6037 ssl_free_buffered_record( ssl );
6038
6039exit:
6040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6041 return( 0 );
6042}
6043
Hanno Becker519f15d2019-07-11 12:43:20 +01006044static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6045 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006046{
6047 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006048
6049 /* Don't buffer future records outside handshakes. */
6050 if( hs == NULL )
6051 return( 0 );
6052
6053 /* Only buffer handshake records (we are only interested
6054 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006055 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006056 return( 0 );
6057
6058 /* Don't buffer more than one future epoch record. */
6059 if( hs->buffering.future_record.data != NULL )
6060 return( 0 );
6061
Hanno Becker01315ea2018-08-21 17:22:17 +01006062 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006063 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006064 hs->buffering.total_bytes_buffered ) )
6065 {
6066 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 +01006067 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006068 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006069 return( 0 );
6070 }
6071
Hanno Becker5f066e72018-08-16 14:56:31 +01006072 /* Buffer record */
6073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6074 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006075 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006076
6077 /* ssl_parse_record_header() only considers records
6078 * of the next epoch as candidates for buffering. */
6079 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006080 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006081
6082 hs->buffering.future_record.data =
6083 mbedtls_calloc( 1, hs->buffering.future_record.len );
6084 if( hs->buffering.future_record.data == NULL )
6085 {
6086 /* If we run out of RAM trying to buffer a
6087 * record from the next epoch, just ignore. */
6088 return( 0 );
6089 }
6090
Hanno Becker519f15d2019-07-11 12:43:20 +01006091 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006092
Hanno Becker519f15d2019-07-11 12:43:20 +01006093 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006094 return( 0 );
6095}
6096
6097#endif /* MBEDTLS_SSL_PROTO_DTLS */
6098
Hanno Beckere74d5562018-08-15 14:26:08 +01006099static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006100{
6101 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006102 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006103
Hanno Becker5f066e72018-08-16 14:56:31 +01006104#if defined(MBEDTLS_SSL_PROTO_DTLS)
6105 /* We might have buffered a future record; if so,
6106 * and if the epoch matches now, load it.
6107 * On success, this call will set ssl->in_left to
6108 * the length of the buffered record, so that
6109 * the calls to ssl_fetch_input() below will
6110 * essentially be no-ops. */
6111 ret = ssl_load_buffered_record( ssl );
6112 if( ret != 0 )
6113 return( ret );
6114#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006115
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006116 /* Ensure that we have enough space available for the default form
6117 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6118 * with no space for CIDs counted in). */
6119 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6120 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006123 return( ret );
6124 }
6125
Hanno Beckere5e7e782019-07-11 12:29:35 +01006126 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6127 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006130 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006131 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006132 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6133 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006134 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006135 if( ret != 0 )
6136 return( ret );
6137
6138 /* Fall through to handling of unexpected records */
6139 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6140 }
6141
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006142 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6143 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006144#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006145 /* Reset in pointers to default state for TLS/DTLS records,
6146 * assuming no CID and no offset between record content and
6147 * record plaintext. */
6148 ssl_update_in_pointers( ssl );
6149
Hanno Becker7ae20e02019-07-12 08:33:49 +01006150 /* Setup internal message pointers from record structure. */
6151 ssl->in_msgtype = rec.type;
6152#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6153 ssl->in_len = ssl->in_cid + rec.cid_len;
6154#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6155 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6156 ssl->in_msglen = rec.data_len;
6157
Hanno Becker2fddd372019-07-10 14:37:41 +01006158 ret = ssl_check_client_reconnect( ssl );
6159 if( ret != 0 )
6160 return( ret );
6161#endif
6162
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006163 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006164 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006165
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6167 "(header)" ) );
6168 }
6169 else
6170 {
6171 /* Skip invalid record and the rest of the datagram */
6172 ssl->next_record_offset = 0;
6173 ssl->in_left = 0;
6174
6175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6176 "(header)" ) );
6177 }
6178
6179 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006180 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006181 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006182 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006183#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006184 {
6185 return( ret );
6186 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006187 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006189#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006190 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006191 {
Hanno Beckera8814792019-07-10 15:01:45 +01006192 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006193 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006194 if( ssl->next_record_offset < ssl->in_left )
6195 {
6196 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6197 }
6198 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006199 else
6200#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006201 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006202 /*
6203 * Fetch record contents from underlying transport.
6204 */
Hanno Beckera3175662019-07-11 12:50:29 +01006205 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006206 if( ret != 0 )
6207 {
6208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6209 return( ret );
6210 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006211
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006212 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006213 }
6214
6215 /*
6216 * Decrypt record contents.
6217 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006218
Hanno Beckerfdf66042019-07-11 13:07:45 +01006219 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006222 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006223 {
6224 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006225 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006226 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006227 /* Except when waiting for Finished as a bad mac here
6228 * probably means something went wrong in the handshake
6229 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6230 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6231 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6232 {
6233#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6234 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6235 {
6236 mbedtls_ssl_send_alert_message( ssl,
6237 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6238 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6239 }
6240#endif
6241 return( ret );
6242 }
6243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006245 if( ssl->conf->badmac_limit != 0 &&
6246 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6249 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006250 }
6251#endif
6252
Hanno Becker4a810fb2017-05-24 16:27:30 +01006253 /* As above, invalid records cause
6254 * dismissal of the whole datagram. */
6255
6256 ssl->next_record_offset = 0;
6257 ssl->in_left = 0;
6258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006260 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006261 }
6262
6263 return( ret );
6264 }
6265 else
6266#endif
6267 {
6268 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6270 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272 mbedtls_ssl_send_alert_message( ssl,
6273 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6274 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006275 }
6276#endif
6277 return( ret );
6278 }
6279 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006280
Hanno Becker44d89b22019-07-12 09:40:44 +01006281
6282 /* Reset in pointers to default state for TLS/DTLS records,
6283 * assuming no CID and no offset between record content and
6284 * record plaintext. */
6285 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006286#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6287 ssl->in_len = ssl->in_cid + rec.cid_len;
6288#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6289 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006290
Hanno Becker8685c822019-07-12 09:37:30 +01006291 /* The record content type may change during decryption,
6292 * so re-read it. */
6293 ssl->in_msgtype = rec.type;
6294 /* Also update the input buffer, because unfortunately
6295 * the server-side ssl_parse_client_hello() reparses the
6296 * record header when receiving a ClientHello initiating
6297 * a renegotiation. */
6298 ssl->in_hdr[0] = rec.type;
6299 ssl->in_msg = rec.buf + rec.data_offset;
6300 ssl->in_msglen = rec.data_len;
6301 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6302 ssl->in_len[1] = (unsigned char)( rec.data_len );
6303
Simon Butcher99000142016-10-13 17:21:01 +01006304 return( 0 );
6305}
6306
6307int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6308{
6309 int ret;
6310
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006311 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006312 * Handle particular types of records
6313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006315 {
Simon Butcher99000142016-10-13 17:21:01 +01006316 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6317 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006318 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006320 }
6321
Hanno Beckere678eaa2018-08-21 14:57:46 +01006322 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006323 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006324 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006325 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6327 ssl->in_msglen ) );
6328 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006329 }
6330
Hanno Beckere678eaa2018-08-21 14:57:46 +01006331 if( ssl->in_msg[0] != 1 )
6332 {
6333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6334 ssl->in_msg[0] ) );
6335 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6336 }
6337
6338#if defined(MBEDTLS_SSL_PROTO_DTLS)
6339 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6340 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6341 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6342 {
6343 if( ssl->handshake == NULL )
6344 {
6345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6346 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6347 }
6348
6349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6350 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6351 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006352#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006353 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006356 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006357 if( ssl->in_msglen != 2 )
6358 {
6359 /* Note: Standard allows for more than one 2 byte alert
6360 to be packed in a single message, but Mbed TLS doesn't
6361 currently support this. */
6362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6363 ssl->in_msglen ) );
6364 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6365 }
6366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006368 ssl->in_msg[0], ssl->in_msg[1] ) );
6369
6370 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006371 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006376 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006378 }
6379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6381 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6384 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006385 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006386
6387#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6388 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6389 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6390 {
Hanno Becker90333da2017-10-10 11:27:13 +01006391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006392 /* Will be handled when trying to parse ServerHello */
6393 return( 0 );
6394 }
6395#endif
6396
6397#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6398 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6399 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6400 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6401 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6402 {
6403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6404 /* Will be handled in mbedtls_ssl_parse_certificate() */
6405 return( 0 );
6406 }
6407#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6408
6409 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006410 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006411 }
6412
Hanno Beckerc76c6192017-06-06 10:03:17 +01006413#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006414 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006415 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006416 /* Drop unexpected ApplicationData records,
6417 * except at the beginning of renegotiations */
6418 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6419 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6420#if defined(MBEDTLS_SSL_RENEGOTIATION)
6421 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6422 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006423#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006424 )
6425 {
6426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6427 return( MBEDTLS_ERR_SSL_NON_FATAL );
6428 }
6429
6430 if( ssl->handshake != NULL &&
6431 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6432 {
6433 ssl_handshake_wrapup_free_hs_transform( ssl );
6434 }
6435 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006436#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006437
Paul Bakker5121ce52009-01-03 21:22:43 +00006438 return( 0 );
6439}
6440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006442{
6443 int ret;
6444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6446 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6447 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006448 {
6449 return( ret );
6450 }
6451
6452 return( 0 );
6453}
6454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006456 unsigned char level,
6457 unsigned char message )
6458{
6459 int ret;
6460
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006461 if( ssl == NULL || ssl->conf == NULL )
6462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006468 ssl->out_msglen = 2;
6469 ssl->out_msg[0] = level;
6470 ssl->out_msg[1] = message;
6471
Hanno Becker67bc7c32018-08-06 11:33:50 +01006472 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006475 return( ret );
6476 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006478
6479 return( 0 );
6480}
6481
Hanno Beckerb9d44792019-02-08 07:19:04 +00006482#if defined(MBEDTLS_X509_CRT_PARSE_C)
6483static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6484{
6485#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6486 if( session->peer_cert != NULL )
6487 {
6488 mbedtls_x509_crt_free( session->peer_cert );
6489 mbedtls_free( session->peer_cert );
6490 session->peer_cert = NULL;
6491 }
6492#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6493 if( session->peer_cert_digest != NULL )
6494 {
6495 /* Zeroization is not necessary. */
6496 mbedtls_free( session->peer_cert_digest );
6497 session->peer_cert_digest = NULL;
6498 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6499 session->peer_cert_digest_len = 0;
6500 }
6501#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6502}
6503#endif /* MBEDTLS_X509_CRT_PARSE_C */
6504
Paul Bakker5121ce52009-01-03 21:22:43 +00006505/*
6506 * Handshake functions
6507 */
Hanno Becker21489932019-02-05 13:20:55 +00006508#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006509/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006510int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006511{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006512 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6513 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006516
Hanno Becker7177a882019-02-05 13:36:46 +00006517 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006520 ssl->state++;
6521 return( 0 );
6522 }
6523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006526}
6527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006529{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006530 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6531 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006534
Hanno Becker7177a882019-02-05 13:36:46 +00006535 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( 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, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006538 ssl->state++;
6539 return( 0 );
6540 }
6541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006544}
Gilles Peskinef9828522017-05-03 12:28:43 +02006545
Hanno Becker21489932019-02-05 13:20:55 +00006546#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006547/* Some certificate support -> implement write and parse */
6548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006552 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006554 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6555 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006558
Hanno Becker7177a882019-02-05 13:36:46 +00006559 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006562 ssl->state++;
6563 return( 0 );
6564 }
6565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006567 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006568 {
6569 if( ssl->client_auth == 0 )
6570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006572 ssl->state++;
6573 return( 0 );
6574 }
6575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 /*
6578 * If using SSLv3 and got no cert, send an Alert message
6579 * (otherwise an empty Certificate message will be sent).
6580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006581 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6582 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006583 {
6584 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6586 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6587 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006590 goto write_msg;
6591 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006593 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594#endif /* MBEDTLS_SSL_CLI_C */
6595#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006596 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6601 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006602 }
6603 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006607
6608 /*
6609 * 0 . 0 handshake type
6610 * 1 . 3 handshake length
6611 * 4 . 6 length of all certs
6612 * 7 . 9 length of cert. 1
6613 * 10 . n-1 peer certificate
6614 * n . n+2 length of cert. 2
6615 * n+3 . ... upper level cert, etc.
6616 */
6617 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006618 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006619
Paul Bakker29087132010-03-21 21:03:34 +00006620 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006621 {
6622 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006623 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006626 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006628 }
6629
6630 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6631 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6632 ssl->out_msg[i + 2] = (unsigned char)( n );
6633
6634 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6635 i += n; crt = crt->next;
6636 }
6637
6638 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6639 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6640 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6641
6642 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6644 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006645
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006646#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006647write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006648#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006649
6650 ssl->state++;
6651
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006652 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006653 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 return( ret );
6656 }
6657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006659
Paul Bakkered27a042013-04-18 22:46:23 +02006660 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006661}
6662
Hanno Becker84879e32019-01-31 07:44:03 +00006663#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006664
6665#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006666static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6667 unsigned char *crt_buf,
6668 size_t crt_buf_len )
6669{
6670 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6671
6672 if( peer_crt == NULL )
6673 return( -1 );
6674
6675 if( peer_crt->raw.len != crt_buf_len )
6676 return( -1 );
6677
Hanno Becker46f34d02019-02-08 14:00:04 +00006678 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006679}
Hanno Becker177475a2019-02-05 17:02:46 +00006680#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6681static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6682 unsigned char *crt_buf,
6683 size_t crt_buf_len )
6684{
6685 int ret;
6686 unsigned char const * const peer_cert_digest =
6687 ssl->session->peer_cert_digest;
6688 mbedtls_md_type_t const peer_cert_digest_type =
6689 ssl->session->peer_cert_digest_type;
6690 mbedtls_md_info_t const * const digest_info =
6691 mbedtls_md_info_from_type( peer_cert_digest_type );
6692 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6693 size_t digest_len;
6694
6695 if( peer_cert_digest == NULL || digest_info == NULL )
6696 return( -1 );
6697
6698 digest_len = mbedtls_md_get_size( digest_info );
6699 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6700 return( -1 );
6701
6702 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6703 if( ret != 0 )
6704 return( -1 );
6705
6706 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6707}
6708#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006709#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006710
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006711/*
6712 * Once the certificate message is read, parse it into a cert chain and
6713 * perform basic checks, but leave actual verification to the caller
6714 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006715static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6716 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006717{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006718 int ret;
6719#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6720 int crt_cnt=0;
6721#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006722 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006723 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006728 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6729 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006731 }
6732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6734 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006737 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6738 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006740 }
6741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006743
Paul Bakker5121ce52009-01-03 21:22:43 +00006744 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006746 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006747 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006748
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006749 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006753 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6754 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006756 }
6757
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006758 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6759 i += 3;
6760
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006761 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006762 while( i < ssl->in_hslen )
6763 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006764 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006765 if ( i + 3 > ssl->in_hslen ) {
6766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006767 mbedtls_ssl_send_alert_message( ssl,
6768 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6769 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006770 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6771 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006772 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6773 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006774 if( ssl->in_msg[i] != 0 )
6775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006777 mbedtls_ssl_send_alert_message( ssl,
6778 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6779 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006781 }
6782
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006783 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006784 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6785 | (unsigned int) ssl->in_msg[i + 2];
6786 i += 3;
6787
6788 if( n < 128 || i + n > ssl->in_hslen )
6789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006791 mbedtls_ssl_send_alert_message( ssl,
6792 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6793 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006795 }
6796
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006797 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006798#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6799 if( crt_cnt++ == 0 &&
6800 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6801 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006802 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006803 /* During client-side renegotiation, check that the server's
6804 * end-CRTs hasn't changed compared to the initial handshake,
6805 * mitigating the triple handshake attack. On success, reuse
6806 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006807 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6808 if( ssl_check_peer_crt_unchanged( ssl,
6809 &ssl->in_msg[i],
6810 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006811 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6813 mbedtls_ssl_send_alert_message( ssl,
6814 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6815 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6816 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006817 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006818
6819 /* Now we can safely free the original chain. */
6820 ssl_clear_peer_cert( ssl->session );
6821 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006822#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6823
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006824 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006825#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006826 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006827#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006828 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006829 * it in-place from the input buffer instead of making a copy. */
6830 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6831#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006832 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006833 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006834 case 0: /*ok*/
6835 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6836 /* Ignore certificate with an unknown algorithm: maybe a
6837 prior certificate was already trusted. */
6838 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006839
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006840 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6841 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6842 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006843
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006844 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6845 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6846 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006847
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006848 default:
6849 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6850 crt_parse_der_failed:
6851 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6852 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6853 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006854 }
6855
6856 i += n;
6857 }
6858
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006859 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006860 return( 0 );
6861}
6862
Hanno Becker4a55f632019-02-05 12:49:06 +00006863#if defined(MBEDTLS_SSL_SRV_C)
6864static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6865{
6866 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6867 return( -1 );
6868
6869#if defined(MBEDTLS_SSL_PROTO_SSL3)
6870 /*
6871 * Check if the client sent an empty certificate
6872 */
6873 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6874 {
6875 if( ssl->in_msglen == 2 &&
6876 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6877 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6878 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6879 {
6880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6881 return( 0 );
6882 }
6883
6884 return( -1 );
6885 }
6886#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6887
6888#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6889 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6890 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6891 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6892 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6893 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6894 {
6895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6896 return( 0 );
6897 }
6898
6899 return( -1 );
6900#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6901 MBEDTLS_SSL_PROTO_TLS1_2 */
6902}
6903#endif /* MBEDTLS_SSL_SRV_C */
6904
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006905/* Check if a certificate message is expected.
6906 * Return either
6907 * - SSL_CERTIFICATE_EXPECTED, or
6908 * - SSL_CERTIFICATE_SKIP
6909 * indicating whether a Certificate message is expected or not.
6910 */
6911#define SSL_CERTIFICATE_EXPECTED 0
6912#define SSL_CERTIFICATE_SKIP 1
6913static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6914 int authmode )
6915{
6916 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006917 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006918
6919 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6920 return( SSL_CERTIFICATE_SKIP );
6921
6922#if defined(MBEDTLS_SSL_SRV_C)
6923 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6924 {
6925 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6926 return( SSL_CERTIFICATE_SKIP );
6927
6928 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6929 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006930 ssl->session_negotiate->verify_result =
6931 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6932 return( SSL_CERTIFICATE_SKIP );
6933 }
6934 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006935#else
6936 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006937#endif /* MBEDTLS_SSL_SRV_C */
6938
6939 return( SSL_CERTIFICATE_EXPECTED );
6940}
6941
Hanno Becker68636192019-02-05 14:36:34 +00006942static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6943 int authmode,
6944 mbedtls_x509_crt *chain,
6945 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006946{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006947 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006948 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006949 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006950 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006951
Hanno Becker8927c832019-04-03 12:52:50 +01006952 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6953 void *p_vrfy;
6954
Hanno Becker68636192019-02-05 14:36:34 +00006955 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6956 return( 0 );
6957
Hanno Becker8927c832019-04-03 12:52:50 +01006958 if( ssl->f_vrfy != NULL )
6959 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006960 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006961 f_vrfy = ssl->f_vrfy;
6962 p_vrfy = ssl->p_vrfy;
6963 }
6964 else
6965 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006967 f_vrfy = ssl->conf->f_vrfy;
6968 p_vrfy = ssl->conf->p_vrfy;
6969 }
6970
Hanno Becker68636192019-02-05 14:36:34 +00006971 /*
6972 * Main check: verify certificate
6973 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006974#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6975 if( ssl->conf->f_ca_cb != NULL )
6976 {
6977 ((void) rs_ctx);
6978 have_ca_chain = 1;
6979
6980 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006981 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006982 chain,
6983 ssl->conf->f_ca_cb,
6984 ssl->conf->p_ca_cb,
6985 ssl->conf->cert_profile,
6986 ssl->hostname,
6987 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006988 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006989 }
6990 else
6991#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6992 {
6993 mbedtls_x509_crt *ca_chain;
6994 mbedtls_x509_crl *ca_crl;
6995
6996#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6997 if( ssl->handshake->sni_ca_chain != NULL )
6998 {
6999 ca_chain = ssl->handshake->sni_ca_chain;
7000 ca_crl = ssl->handshake->sni_ca_crl;
7001 }
7002 else
7003#endif
7004 {
7005 ca_chain = ssl->conf->ca_chain;
7006 ca_crl = ssl->conf->ca_crl;
7007 }
7008
7009 if( ca_chain != NULL )
7010 have_ca_chain = 1;
7011
7012 ret = mbedtls_x509_crt_verify_restartable(
7013 chain,
7014 ca_chain, ca_crl,
7015 ssl->conf->cert_profile,
7016 ssl->hostname,
7017 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007018 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007019 }
Hanno Becker68636192019-02-05 14:36:34 +00007020
7021 if( ret != 0 )
7022 {
7023 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7024 }
7025
7026#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7027 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7028 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7029#endif
7030
7031 /*
7032 * Secondary checks: always done, but change 'ret' only if it was 0
7033 */
7034
7035#if defined(MBEDTLS_ECP_C)
7036 {
7037 const mbedtls_pk_context *pk = &chain->pk;
7038
7039 /* If certificate uses an EC key, make sure the curve is OK */
7040 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7041 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7042 {
7043 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7044
7045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7046 if( ret == 0 )
7047 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7048 }
7049 }
7050#endif /* MBEDTLS_ECP_C */
7051
7052 if( mbedtls_ssl_check_cert_usage( chain,
7053 ciphersuite_info,
7054 ! ssl->conf->endpoint,
7055 &ssl->session_negotiate->verify_result ) != 0 )
7056 {
7057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7058 if( ret == 0 )
7059 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7060 }
7061
7062 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7063 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7064 * with details encoded in the verification flags. All other kinds
7065 * of error codes, including those from the user provided f_vrfy
7066 * functions, are treated as fatal and lead to a failure of
7067 * ssl_parse_certificate even if verification was optional. */
7068 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7069 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7070 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7071 {
7072 ret = 0;
7073 }
7074
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007075 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007076 {
7077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7078 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7079 }
7080
7081 if( ret != 0 )
7082 {
7083 uint8_t alert;
7084
7085 /* The certificate may have been rejected for several reasons.
7086 Pick one and send the corresponding alert. Which alert to send
7087 may be a subject of debate in some cases. */
7088 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7089 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7090 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7091 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7092 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7093 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7094 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7095 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7096 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7097 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7098 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7099 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7100 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7101 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7102 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7103 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7104 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7105 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7106 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7107 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7108 else
7109 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7110 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7111 alert );
7112 }
7113
7114#if defined(MBEDTLS_DEBUG_C)
7115 if( ssl->session_negotiate->verify_result != 0 )
7116 {
7117 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7118 ssl->session_negotiate->verify_result ) );
7119 }
7120 else
7121 {
7122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7123 }
7124#endif /* MBEDTLS_DEBUG_C */
7125
7126 return( ret );
7127}
7128
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007129#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7130static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7131 unsigned char *start, size_t len )
7132{
7133 int ret;
7134 /* Remember digest of the peer's end-CRT. */
7135 ssl->session_negotiate->peer_cert_digest =
7136 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7137 if( ssl->session_negotiate->peer_cert_digest == NULL )
7138 {
7139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7140 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7141 mbedtls_ssl_send_alert_message( ssl,
7142 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7143 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7144
7145 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7146 }
7147
7148 ret = mbedtls_md( mbedtls_md_info_from_type(
7149 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7150 start, len,
7151 ssl->session_negotiate->peer_cert_digest );
7152
7153 ssl->session_negotiate->peer_cert_digest_type =
7154 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7155 ssl->session_negotiate->peer_cert_digest_len =
7156 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7157
7158 return( ret );
7159}
7160
7161static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7162 unsigned char *start, size_t len )
7163{
7164 unsigned char *end = start + len;
7165 int ret;
7166
7167 /* Make a copy of the peer's raw public key. */
7168 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7169 ret = mbedtls_pk_parse_subpubkey( &start, end,
7170 &ssl->handshake->peer_pubkey );
7171 if( ret != 0 )
7172 {
7173 /* We should have parsed the public key before. */
7174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7175 }
7176
7177 return( 0 );
7178}
7179#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7180
Hanno Becker68636192019-02-05 14:36:34 +00007181int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7182{
7183 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007184 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007185#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7186 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7187 ? ssl->handshake->sni_authmode
7188 : ssl->conf->authmode;
7189#else
7190 const int authmode = ssl->conf->authmode;
7191#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007192 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007193 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007194
7195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7196
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007197 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7198 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007199 {
7200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007201 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007202 }
7203
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007204#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7205 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007206 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007207 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007208 chain = ssl->handshake->ecrs_peer_cert;
7209 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007210 goto crt_verify;
7211 }
7212#endif
7213
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007214 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007215 {
7216 /* mbedtls_ssl_read_record may have sent an alert already. We
7217 let it decide whether to alert. */
7218 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007219 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007220 }
7221
Hanno Becker4a55f632019-02-05 12:49:06 +00007222#if defined(MBEDTLS_SSL_SRV_C)
7223 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7224 {
7225 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007226
7227 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007228 ret = 0;
7229 else
7230 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007231
Hanno Becker6bdfab22019-02-05 13:11:17 +00007232 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007233 }
7234#endif /* MBEDTLS_SSL_SRV_C */
7235
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007236 /* Clear existing peer CRT structure in case we tried to
7237 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007238 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007239
7240 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7241 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007242 {
7243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7244 sizeof( mbedtls_x509_crt ) ) );
7245 mbedtls_ssl_send_alert_message( ssl,
7246 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7247 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007248
Hanno Becker3dad3112019-02-05 17:19:52 +00007249 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7250 goto exit;
7251 }
7252 mbedtls_x509_crt_init( chain );
7253
7254 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007255 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007256 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007257
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007258#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7259 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007260 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007261
7262crt_verify:
7263 if( ssl->handshake->ecrs_enabled)
7264 rs_ctx = &ssl->handshake->ecrs_ctx;
7265#endif
7266
Hanno Becker68636192019-02-05 14:36:34 +00007267 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007268 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007269 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007270 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007271
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007272#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007273 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007274 unsigned char *crt_start, *pk_start;
7275 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007276
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007277 /* We parse the CRT chain without copying, so
7278 * these pointers point into the input buffer,
7279 * and are hence still valid after freeing the
7280 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007281
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007282 crt_start = chain->raw.p;
7283 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007284
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007285 pk_start = chain->pk_raw.p;
7286 pk_len = chain->pk_raw.len;
7287
7288 /* Free the CRT structures before computing
7289 * digest and copying the peer's public key. */
7290 mbedtls_x509_crt_free( chain );
7291 mbedtls_free( chain );
7292 chain = NULL;
7293
7294 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007295 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007296 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007297
7298 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7299 if( ret != 0 )
7300 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007301 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007302#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7303 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007304 ssl->session_negotiate->peer_cert = chain;
7305 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007306#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007309
Hanno Becker6bdfab22019-02-05 13:11:17 +00007310exit:
7311
Hanno Becker3dad3112019-02-05 17:19:52 +00007312 if( ret == 0 )
7313 ssl->state++;
7314
7315#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7316 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7317 {
7318 ssl->handshake->ecrs_peer_cert = chain;
7319 chain = NULL;
7320 }
7321#endif
7322
7323 if( chain != NULL )
7324 {
7325 mbedtls_x509_crt_free( chain );
7326 mbedtls_free( chain );
7327 }
7328
Paul Bakker5121ce52009-01-03 21:22:43 +00007329 return( ret );
7330}
Hanno Becker21489932019-02-05 13:20:55 +00007331#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007334{
7335 int ret;
7336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007340 ssl->out_msglen = 1;
7341 ssl->out_msg[0] = 1;
7342
Paul Bakker5121ce52009-01-03 21:22:43 +00007343 ssl->state++;
7344
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007345 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007346 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007348 return( ret );
7349 }
7350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007352
7353 return( 0 );
7354}
7355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007357{
7358 int ret;
7359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007361
Hanno Becker327c93b2018-08-15 13:56:18 +01007362 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007365 return( ret );
7366 }
7367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007371 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7372 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007374 }
7375
Hanno Beckere678eaa2018-08-21 14:57:46 +01007376 /* CCS records are only accepted if they have length 1 and content '1',
7377 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007378
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007379 /*
7380 * Switch to our negotiated transform and session parameters for inbound
7381 * data.
7382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007384 ssl->transform_in = ssl->transform_negotiate;
7385 ssl->session_in = ssl->session_negotiate;
7386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007388 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007391 ssl_dtls_replay_reset( ssl );
7392#endif
7393
7394 /* Increment epoch */
7395 if( ++ssl->in_epoch == 0 )
7396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007398 /* This is highly unlikely to happen for legitimate reasons, so
7399 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007401 }
7402 }
7403 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007405 memset( ssl->in_ctr, 0, 8 );
7406
Hanno Becker79594fd2019-05-08 09:38:41 +01007407 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7410 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007412 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007415 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7416 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007418 }
7419 }
7420#endif
7421
Paul Bakker5121ce52009-01-03 21:22:43 +00007422 ssl->state++;
7423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007425
7426 return( 0 );
7427}
7428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7430 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007431{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007432 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7435 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7436 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007437 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007438 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007439#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7441#if defined(MBEDTLS_SHA512_C)
7442 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007443 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7444 else
7445#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446#if defined(MBEDTLS_SHA256_C)
7447 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007448 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007449 else
7450#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007454 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007455 }
Paul Bakker380da532012-04-18 16:10:25 +00007456}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7461 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007462 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7463 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007464#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7466#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007467#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007468 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007469 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7470#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007471 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007472#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007473#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007475#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007476 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007477 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007478#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007479 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007480#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007481#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007483}
7484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007485static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007486 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007487{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7489 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007490 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7491 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007492#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7494#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007495#if defined(MBEDTLS_USE_PSA_CRYPTO)
7496 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7497#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007498 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007499#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007500#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007502#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007503 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007504#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007505 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007506#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007507#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007509}
7510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7512 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7513static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007514 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007515{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007516 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7517 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007518}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007519#endif
Paul Bakker380da532012-04-18 16:10:25 +00007520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7522#if defined(MBEDTLS_SHA256_C)
7523static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007524 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007525{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007526#if defined(MBEDTLS_USE_PSA_CRYPTO)
7527 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7528#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007529 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007530#endif
Paul Bakker380da532012-04-18 16:10:25 +00007531}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007532#endif
Paul Bakker380da532012-04-18 16:10:25 +00007533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534#if defined(MBEDTLS_SHA512_C)
7535static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007536 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007537{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007538#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007539 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007540#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007541 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007542#endif
Paul Bakker380da532012-04-18 16:10:25 +00007543}
Paul Bakker769075d2012-11-24 11:26:46 +01007544#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007548static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007550{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007551 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007552 mbedtls_md5_context md5;
7553 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007554
Paul Bakker5121ce52009-01-03 21:22:43 +00007555 unsigned char padbuf[48];
7556 unsigned char md5sum[16];
7557 unsigned char sha1sum[20];
7558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007560 if( !session )
7561 session = ssl->session;
7562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007564
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007565 mbedtls_md5_init( &md5 );
7566 mbedtls_sha1_init( &sha1 );
7567
7568 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7569 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007570
7571 /*
7572 * SSLv3:
7573 * hash =
7574 * MD5( master + pad2 +
7575 * MD5( handshake + sender + master + pad1 ) )
7576 * + SHA1( master + pad2 +
7577 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007578 */
7579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007581 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7582 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007583#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007586 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7587 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007588#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007591 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007592
Paul Bakker1ef83d62012-04-11 12:09:53 +00007593 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007594
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007595 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7596 mbedtls_md5_update_ret( &md5, session->master, 48 );
7597 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7598 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007599
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007600 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7601 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7602 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7603 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007604
Paul Bakker1ef83d62012-04-11 12:09:53 +00007605 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007606
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007607 mbedtls_md5_starts_ret( &md5 );
7608 mbedtls_md5_update_ret( &md5, session->master, 48 );
7609 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7610 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7611 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007612
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007613 mbedtls_sha1_starts_ret( &sha1 );
7614 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7615 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7616 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7617 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007620
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007621 mbedtls_md5_free( &md5 );
7622 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007623
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007624 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7625 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7626 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007629}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007633static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007635{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007636 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007637 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007638 mbedtls_md5_context md5;
7639 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007640 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007643 if( !session )
7644 session = ssl->session;
7645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007647
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007648 mbedtls_md5_init( &md5 );
7649 mbedtls_sha1_init( &sha1 );
7650
7651 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7652 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007653
Paul Bakker1ef83d62012-04-11 12:09:53 +00007654 /*
7655 * TLSv1:
7656 * hash = PRF( master, finished_label,
7657 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7658 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007661 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7662 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007663#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007666 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7667 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007668#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007671 ? "client finished"
7672 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007673
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007674 mbedtls_md5_finish_ret( &md5, padbuf );
7675 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007676
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007677 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007678 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007681
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007682 mbedtls_md5_free( &md5 );
7683 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007684
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007685 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7692#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007693static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007695{
7696 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007697 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007698 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007699#if defined(MBEDTLS_USE_PSA_CRYPTO)
7700 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007701 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007702 psa_status_t status;
7703#else
7704 mbedtls_sha256_context sha256;
7705#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007708 if( !session )
7709 session = ssl->session;
7710
Andrzej Kurekeb342242019-01-29 09:14:33 -05007711 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7712 ? "client finished"
7713 : "server finished";
7714
7715#if defined(MBEDTLS_USE_PSA_CRYPTO)
7716 sha256_psa = psa_hash_operation_init();
7717
7718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7719
7720 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7721 if( status != PSA_SUCCESS )
7722 {
7723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7724 return;
7725 }
7726
7727 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7728 if( status != PSA_SUCCESS )
7729 {
7730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7731 return;
7732 }
7733 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7734#else
7735
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007736 mbedtls_sha256_init( &sha256 );
7737
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007739
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007740 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007741
7742 /*
7743 * TLSv1.2:
7744 * hash = PRF( master, finished_label,
7745 * Hash( handshake ) )[0.11]
7746 */
7747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748#if !defined(MBEDTLS_SHA256_ALT)
7749 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007750 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007751#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007752
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007753 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007754 mbedtls_sha256_free( &sha256 );
7755#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007756
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007757 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007758 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007761
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007762 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007765}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007769static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007771{
7772 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007773 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007774 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007775#if defined(MBEDTLS_USE_PSA_CRYPTO)
7776 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007777 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007778 psa_status_t status;
7779#else
7780 mbedtls_sha512_context sha512;
7781#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007783 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007784 if( !session )
7785 session = ssl->session;
7786
Andrzej Kurekeb342242019-01-29 09:14:33 -05007787 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7788 ? "client finished"
7789 : "server finished";
7790
7791#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007792 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007793
7794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7795
Andrzej Kurek972fba52019-01-30 03:29:12 -05007796 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007797 if( status != PSA_SUCCESS )
7798 {
7799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7800 return;
7801 }
7802
Andrzej Kurek972fba52019-01-30 03:29:12 -05007803 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007804 if( status != PSA_SUCCESS )
7805 {
7806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7807 return;
7808 }
7809 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7810#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007811 mbedtls_sha512_init( &sha512 );
7812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007814
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007815 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007816
7817 /*
7818 * TLSv1.2:
7819 * hash = PRF( master, finished_label,
7820 * Hash( handshake ) )[0.11]
7821 */
7822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007824 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7825 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007826#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007827
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007828 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007829 mbedtls_sha512_free( &sha512 );
7830#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007831
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007832 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007833 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007836
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007837 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841#endif /* MBEDTLS_SHA512_C */
7842#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007845{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007847
7848 /*
7849 * Free our handshake params
7850 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007851 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007853 ssl->handshake = NULL;
7854
7855 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007856 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007857 */
7858 if( ssl->transform )
7859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 mbedtls_ssl_transform_free( ssl->transform );
7861 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007862 }
7863 ssl->transform = ssl->transform_negotiate;
7864 ssl->transform_negotiate = NULL;
7865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007867}
7868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007870{
7871 int resume = ssl->handshake->resume;
7872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875#if defined(MBEDTLS_SSL_RENEGOTIATION)
7876 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007879 ssl->renego_records_seen = 0;
7880 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007881#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007882
7883 /*
7884 * Free the previous session and switch in the current one
7885 */
Paul Bakker0a597072012-09-25 21:55:46 +00007886 if( ssl->session )
7887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007889 /* RFC 7366 3.1: keep the EtM state */
7890 ssl->session_negotiate->encrypt_then_mac =
7891 ssl->session->encrypt_then_mac;
7892#endif
7893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894 mbedtls_ssl_session_free( ssl->session );
7895 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007896 }
7897 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007898 ssl->session_negotiate = NULL;
7899
Paul Bakker0a597072012-09-25 21:55:46 +00007900 /*
7901 * Add cache entry
7902 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007903 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007904 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007905 resume == 0 )
7906 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007907 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007909 }
Paul Bakker0a597072012-09-25 21:55:46 +00007910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007912 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007913 ssl->handshake->flight != NULL )
7914 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007915 /* Cancel handshake timer */
7916 ssl_set_timer( ssl, 0 );
7917
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007918 /* Keep last flight around in case we need to resend it:
7919 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007921 }
7922 else
7923#endif
7924 ssl_handshake_wrapup_free_hs_transform( ssl );
7925
Paul Bakker48916f92012-09-16 19:57:18 +00007926 ssl->state++;
7927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007929}
7930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007932{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007933 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007936
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007937 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007938
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007939 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007940
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007941 /*
7942 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7943 * may define some other value. Currently (early 2016), no defined
7944 * ciphersuite does this (and this is unlikely to change as activity has
7945 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007950 ssl->verify_data_len = hash_len;
7951 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007952#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007953
Paul Bakker5121ce52009-01-03 21:22:43 +00007954 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7956 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007957
7958 /*
7959 * In case of session resuming, invert the client and server
7960 * ChangeCipherSpec messages order.
7961 */
Paul Bakker0a597072012-09-25 21:55:46 +00007962 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007965 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007967#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007969 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007971#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007972 }
7973 else
7974 ssl->state++;
7975
Paul Bakker48916f92012-09-16 19:57:18 +00007976 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007977 * Switch to our negotiated transform and session parameters for outbound
7978 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007983 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007984 {
7985 unsigned char i;
7986
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007987 /* Remember current epoch settings for resending */
7988 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007989 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007990
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007991 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007992 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007993
7994 /* Increment epoch */
7995 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007996 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007997 break;
7998
7999 /* The loop goes to its end iff the counter is wrapping */
8000 if( i == 0 )
8001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8003 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008004 }
8005 }
8006 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008008 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008009
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008010 ssl->transform_out = ssl->transform_negotiate;
8011 ssl->session_out = ssl->session_negotiate;
8012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8014 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8019 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008020 }
8021 }
8022#endif
8023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008025 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008027#endif
8028
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008029 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008030 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008032 return( ret );
8033 }
8034
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008035#if defined(MBEDTLS_SSL_PROTO_DTLS)
8036 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8037 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8038 {
8039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8040 return( ret );
8041 }
8042#endif
8043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008045
8046 return( 0 );
8047}
8048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008049#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008050#define SSL_MAX_HASH_LEN 36
8051#else
8052#define SSL_MAX_HASH_LEN 12
8053#endif
8054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008056{
Paul Bakker23986e52011-04-24 08:57:21 +00008057 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008058 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008059 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008062
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008063 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008064
Hanno Becker327c93b2018-08-15 13:56:18 +01008065 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008068 return( ret );
8069 }
8070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008074 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8075 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008077 }
8078
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008079 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080#if defined(MBEDTLS_SSL_PROTO_SSL3)
8081 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008082 hash_len = 36;
8083 else
8084#endif
8085 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8088 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008091 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8092 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008094 }
8095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008097 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008100 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8101 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008102 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008103 }
8104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008106 ssl->verify_data_len = hash_len;
8107 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008108#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008109
Paul Bakker0a597072012-09-25 21:55:46 +00008110 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008113 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008115#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008117 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008119#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008120 }
8121 else
8122 ssl->state++;
8123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008124#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008125 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008127#endif
8128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008130
8131 return( 0 );
8132}
8133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8139 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8140 mbedtls_md5_init( &handshake->fin_md5 );
8141 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008142 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8143 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8146#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008147#if defined(MBEDTLS_USE_PSA_CRYPTO)
8148 handshake->fin_sha256_psa = psa_hash_operation_init();
8149 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8150#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008152 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008153#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008154#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008156#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008157 handshake->fin_sha384_psa = psa_hash_operation_init();
8158 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008159#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008161 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008162#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008165
8166 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008167
8168#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8169 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8170 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8171#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173#if defined(MBEDTLS_DHM_C)
8174 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176#if defined(MBEDTLS_ECDH_C)
8177 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008178#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008179#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008180 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008181#if defined(MBEDTLS_SSL_CLI_C)
8182 handshake->ecjpake_cache = NULL;
8183 handshake->ecjpake_cache_len = 0;
8184#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008185#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008186
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008187#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008188 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008189#endif
8190
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008191#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8192 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8193#endif
Hanno Becker75173122019-02-06 16:18:31 +00008194
8195#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8196 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8197 mbedtls_pk_init( &handshake->peer_pubkey );
8198#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008199}
8200
Hanno Beckera18d1322018-01-03 14:27:32 +00008201void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008202{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8206 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008207
Hanno Beckerd56ed242018-01-03 15:32:51 +00008208#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 mbedtls_md_init( &transform->md_ctx_enc );
8210 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008211#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008212}
8213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008215{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008217}
8218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008220{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008221 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008222 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008224 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008226 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008227 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008228
8229 /*
8230 * Either the pointers are now NULL or cleared properly and can be freed.
8231 * Now allocate missing structures.
8232 */
8233 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008234 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008235 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008236 }
Paul Bakker48916f92012-09-16 19:57:18 +00008237
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008238 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008239 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008240 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008241 }
Paul Bakker48916f92012-09-16 19:57:18 +00008242
Paul Bakker82788fb2014-10-20 13:59:19 +02008243 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008244 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008245 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008246 }
Paul Bakker48916f92012-09-16 19:57:18 +00008247
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008248 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008249 if( ssl->handshake == NULL ||
8250 ssl->transform_negotiate == NULL ||
8251 ssl->session_negotiate == NULL )
8252 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255 mbedtls_free( ssl->handshake );
8256 mbedtls_free( ssl->transform_negotiate );
8257 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008258
8259 ssl->handshake = NULL;
8260 ssl->transform_negotiate = NULL;
8261 ssl->session_negotiate = NULL;
8262
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008263 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008264 }
8265
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008266 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008268 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008269 ssl_handshake_params_init( ssl->handshake );
8270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008272 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8273 {
8274 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008275
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008276 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8277 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8278 else
8279 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008280
8281 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008282 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008283#endif
8284
Paul Bakker48916f92012-09-16 19:57:18 +00008285 return( 0 );
8286}
8287
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008288#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008289/* Dummy cookie callbacks for defaults */
8290static int ssl_cookie_write_dummy( void *ctx,
8291 unsigned char **p, unsigned char *end,
8292 const unsigned char *cli_id, size_t cli_id_len )
8293{
8294 ((void) ctx);
8295 ((void) p);
8296 ((void) end);
8297 ((void) cli_id);
8298 ((void) cli_id_len);
8299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008301}
8302
8303static int ssl_cookie_check_dummy( void *ctx,
8304 const unsigned char *cookie, size_t cookie_len,
8305 const unsigned char *cli_id, size_t cli_id_len )
8306{
8307 ((void) ctx);
8308 ((void) cookie);
8309 ((void) cookie_len);
8310 ((void) cli_id);
8311 ((void) cli_id_len);
8312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008314}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008315#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008316
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008317/* Once ssl->out_hdr as the address of the beginning of the
8318 * next outgoing record is set, deduce the other pointers.
8319 *
8320 * Note: For TLS, we save the implicit record sequence number
8321 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8322 * and the caller has to make sure there's space for this.
8323 */
8324
8325static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8326 mbedtls_ssl_transform *transform )
8327{
8328#if defined(MBEDTLS_SSL_PROTO_DTLS)
8329 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8330 {
8331 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008332#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008333 ssl->out_cid = ssl->out_ctr + 8;
8334 ssl->out_len = ssl->out_cid;
8335 if( transform != NULL )
8336 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008337#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008338 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008339#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008340 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008341 }
8342 else
8343#endif
8344 {
8345 ssl->out_ctr = ssl->out_hdr - 8;
8346 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008347#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008348 ssl->out_cid = ssl->out_len;
8349#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008350 ssl->out_iv = ssl->out_hdr + 5;
8351 }
8352
8353 /* Adjust out_msg to make space for explicit IV, if used. */
8354 if( transform != NULL &&
8355 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8356 {
8357 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8358 }
8359 else
8360 ssl->out_msg = ssl->out_iv;
8361}
8362
8363/* Once ssl->in_hdr as the address of the beginning of the
8364 * next incoming record is set, deduce the other pointers.
8365 *
8366 * Note: For TLS, we save the implicit record sequence number
8367 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8368 * and the caller has to make sure there's space for this.
8369 */
8370
Hanno Becker79594fd2019-05-08 09:38:41 +01008371static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008372{
Hanno Becker79594fd2019-05-08 09:38:41 +01008373 /* This function sets the pointers to match the case
8374 * of unprotected TLS/DTLS records, with both ssl->in_iv
8375 * and ssl->in_msg pointing to the beginning of the record
8376 * content.
8377 *
8378 * When decrypting a protected record, ssl->in_msg
8379 * will be shifted to point to the beginning of the
8380 * record plaintext.
8381 */
8382
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008383#if defined(MBEDTLS_SSL_PROTO_DTLS)
8384 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8385 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008386 /* This sets the header pointers to match records
8387 * without CID. When we receive a record containing
8388 * a CID, the fields are shifted accordingly in
8389 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008390 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008391#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008392 ssl->in_cid = ssl->in_ctr + 8;
8393 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008394#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008395 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008396#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008397 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008398 }
8399 else
8400#endif
8401 {
8402 ssl->in_ctr = ssl->in_hdr - 8;
8403 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008404#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008405 ssl->in_cid = ssl->in_len;
8406#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008407 ssl->in_iv = ssl->in_hdr + 5;
8408 }
8409
Hanno Becker79594fd2019-05-08 09:38:41 +01008410 /* This will be adjusted at record decryption time. */
8411 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008412}
8413
Paul Bakker5121ce52009-01-03 21:22:43 +00008414/*
8415 * Initialize an SSL context
8416 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008417void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8418{
8419 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8420}
8421
8422/*
8423 * Setup an SSL context
8424 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008425
8426static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8427{
8428 /* Set the incoming and outgoing record pointers. */
8429#if defined(MBEDTLS_SSL_PROTO_DTLS)
8430 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8431 {
8432 ssl->out_hdr = ssl->out_buf;
8433 ssl->in_hdr = ssl->in_buf;
8434 }
8435 else
8436#endif /* MBEDTLS_SSL_PROTO_DTLS */
8437 {
8438 ssl->out_hdr = ssl->out_buf + 8;
8439 ssl->in_hdr = ssl->in_buf + 8;
8440 }
8441
8442 /* Derive other internal pointers. */
8443 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008444 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008445}
8446
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008447int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008448 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008449{
Paul Bakker48916f92012-09-16 19:57:18 +00008450 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008451
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008452 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008453
8454 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008455 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008456 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008457
8458 /* Set to NULL in case of an error condition */
8459 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008460
Angus Grattond8213d02016-05-25 20:56:48 +10008461 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8462 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008463 {
Angus Grattond8213d02016-05-25 20:56:48 +10008464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008465 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008466 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008467 }
8468
8469 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8470 if( ssl->out_buf == NULL )
8471 {
8472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008473 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008474 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008475 }
8476
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008477 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008478
Paul Bakker48916f92012-09-16 19:57:18 +00008479 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008480 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008481
8482 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008483
8484error:
8485 mbedtls_free( ssl->in_buf );
8486 mbedtls_free( ssl->out_buf );
8487
8488 ssl->conf = NULL;
8489
8490 ssl->in_buf = NULL;
8491 ssl->out_buf = NULL;
8492
8493 ssl->in_hdr = NULL;
8494 ssl->in_ctr = NULL;
8495 ssl->in_len = NULL;
8496 ssl->in_iv = NULL;
8497 ssl->in_msg = NULL;
8498
8499 ssl->out_hdr = NULL;
8500 ssl->out_ctr = NULL;
8501 ssl->out_len = NULL;
8502 ssl->out_iv = NULL;
8503 ssl->out_msg = NULL;
8504
k-stachowiak9f7798e2018-07-31 16:52:32 +02008505 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008506}
8507
8508/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008509 * Reset an initialized and used SSL context for re-use while retaining
8510 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008511 *
8512 * If partial is non-zero, keep data in the input buffer and client ID.
8513 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008514 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008515static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008516{
Paul Bakker48916f92012-09-16 19:57:18 +00008517 int ret;
8518
Hanno Becker7e772132018-08-10 12:38:21 +01008519#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8520 !defined(MBEDTLS_SSL_SRV_C)
8521 ((void) partial);
8522#endif
8523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008524 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008525
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008526 /* Cancel any possibly running timer */
8527 ssl_set_timer( ssl, 0 );
8528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529#if defined(MBEDTLS_SSL_RENEGOTIATION)
8530 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008531 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008532
8533 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008534 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8535 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008537 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008538
Paul Bakker7eb013f2011-10-06 12:37:39 +00008539 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008540 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008541
8542 ssl->in_msgtype = 0;
8543 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008545 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008546 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008547#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008548#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008549 ssl_dtls_replay_reset( ssl );
8550#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008551
8552 ssl->in_hslen = 0;
8553 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008554
8555 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008556
8557 ssl->out_msgtype = 0;
8558 ssl->out_msglen = 0;
8559 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008560#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8561 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008562 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008563#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008564
Hanno Becker19859472018-08-06 09:40:20 +01008565 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8566
Paul Bakker48916f92012-09-16 19:57:18 +00008567 ssl->transform_in = NULL;
8568 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008569
Hanno Becker78640902018-08-13 16:35:15 +01008570 ssl->session_in = NULL;
8571 ssl->session_out = NULL;
8572
Angus Grattond8213d02016-05-25 20:56:48 +10008573 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008574
8575#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008576 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008577#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8578 {
8579 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008580 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008581 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008583#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8584 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8587 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8590 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008591 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008592 }
8593#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008594
Paul Bakker48916f92012-09-16 19:57:18 +00008595 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597 mbedtls_ssl_transform_free( ssl->transform );
8598 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008599 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008600 }
Paul Bakker48916f92012-09-16 19:57:18 +00008601
Paul Bakkerc0463502013-02-14 11:19:38 +01008602 if( ssl->session )
8603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604 mbedtls_ssl_session_free( ssl->session );
8605 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008606 ssl->session = NULL;
8607 }
8608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008610 ssl->alpn_chosen = NULL;
8611#endif
8612
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008613#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008614#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008615 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008616#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008617 {
8618 mbedtls_free( ssl->cli_id );
8619 ssl->cli_id = NULL;
8620 ssl->cli_id_len = 0;
8621 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008622#endif
8623
Paul Bakker48916f92012-09-16 19:57:18 +00008624 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8625 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008626
8627 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008628}
8629
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008630/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008631 * Reset an initialized and used SSL context for re-use while retaining
8632 * all application-set variables, function pointers and data.
8633 */
8634int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8635{
8636 return( ssl_session_reset_int( ssl, 0 ) );
8637}
8638
8639/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008640 * SSL set accessors
8641 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008642void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008643{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008644 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008645}
8646
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008647void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008648{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008649 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008650}
8651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008652#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008653void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008654{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008655 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008656}
8657#endif
8658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008660void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008661{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008662 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008663}
8664#endif
8665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008666#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008667
Hanno Becker1841b0a2018-08-24 11:13:57 +01008668void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8669 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008670{
8671 ssl->disable_datagram_packing = !allow_packing;
8672}
8673
8674void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8675 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008676{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008677 conf->hs_timeout_min = min;
8678 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008679}
8680#endif
8681
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008682void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008683{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008684 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008685}
8686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008688void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008689 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008690 void *p_vrfy )
8691{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008692 conf->f_vrfy = f_vrfy;
8693 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008696
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008697void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008698 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008699 void *p_rng )
8700{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008701 conf->f_rng = f_rng;
8702 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008703}
8704
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008705void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008706 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008707 void *p_dbg )
8708{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008709 conf->f_dbg = f_dbg;
8710 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008711}
8712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008713void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008714 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008715 mbedtls_ssl_send_t *f_send,
8716 mbedtls_ssl_recv_t *f_recv,
8717 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008718{
8719 ssl->p_bio = p_bio;
8720 ssl->f_send = f_send;
8721 ssl->f_recv = f_recv;
8722 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008723}
8724
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008725#if defined(MBEDTLS_SSL_PROTO_DTLS)
8726void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8727{
8728 ssl->mtu = mtu;
8729}
8730#endif
8731
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008732void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008733{
8734 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008735}
8736
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008737void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8738 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008739 mbedtls_ssl_set_timer_t *f_set_timer,
8740 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008741{
8742 ssl->p_timer = p_timer;
8743 ssl->f_set_timer = f_set_timer;
8744 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008745
8746 /* Make sure we start with no timer running */
8747 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008748}
8749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008751void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008752 void *p_cache,
8753 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8754 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008755{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008756 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008757 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008758 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008762#if defined(MBEDTLS_SSL_CLI_C)
8763int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008764{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008765 int ret;
8766
8767 if( ssl == NULL ||
8768 session == NULL ||
8769 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008770 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008773 }
8774
Hanno Becker52055ae2019-02-06 14:30:46 +00008775 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8776 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008777 return( ret );
8778
Paul Bakker0a597072012-09-25 21:55:46 +00008779 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008780
8781 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008782}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008784
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008785void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008786 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008787{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008788 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8789 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8790 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8791 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008792}
8793
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008794void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008795 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008796 int major, int minor )
8797{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008799 return;
8800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008802 return;
8803
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008804 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008805}
8806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008808void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008809 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008810{
8811 conf->cert_profile = profile;
8812}
8813
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008814/* Append a new keycert entry to a (possibly empty) list */
8815static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8816 mbedtls_x509_crt *cert,
8817 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008818{
niisato8ee24222018-06-25 19:05:48 +09008819 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008820
niisato8ee24222018-06-25 19:05:48 +09008821 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8822 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008823 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008824
niisato8ee24222018-06-25 19:05:48 +09008825 new_cert->cert = cert;
8826 new_cert->key = key;
8827 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008828
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008829 /* Update head is the list was null, else add to the end */
8830 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008831 {
niisato8ee24222018-06-25 19:05:48 +09008832 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008833 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008834 else
8835 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008836 mbedtls_ssl_key_cert *cur = *head;
8837 while( cur->next != NULL )
8838 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008839 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008840 }
8841
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008842 return( 0 );
8843}
8844
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008845int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008846 mbedtls_x509_crt *own_cert,
8847 mbedtls_pk_context *pk_key )
8848{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008849 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008850}
8851
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008852void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008853 mbedtls_x509_crt *ca_chain,
8854 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008855{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008856 conf->ca_chain = ca_chain;
8857 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008858
8859#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8860 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8861 * cannot be used together. */
8862 conf->f_ca_cb = NULL;
8863 conf->p_ca_cb = NULL;
8864#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008865}
Hanno Becker5adaad92019-03-27 16:54:37 +00008866
8867#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8868void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008869 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008870 void *p_ca_cb )
8871{
8872 conf->f_ca_cb = f_ca_cb;
8873 conf->p_ca_cb = p_ca_cb;
8874
8875 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8876 * cannot be used together. */
8877 conf->ca_chain = NULL;
8878 conf->ca_crl = NULL;
8879}
8880#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008882
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008883#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8884int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8885 mbedtls_x509_crt *own_cert,
8886 mbedtls_pk_context *pk_key )
8887{
8888 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8889 own_cert, pk_key ) );
8890}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008891
8892void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8893 mbedtls_x509_crt *ca_chain,
8894 mbedtls_x509_crl *ca_crl )
8895{
8896 ssl->handshake->sni_ca_chain = ca_chain;
8897 ssl->handshake->sni_ca_crl = ca_crl;
8898}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008899
8900void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8901 int authmode )
8902{
8903 ssl->handshake->sni_authmode = authmode;
8904}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008905#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8906
Hanno Becker8927c832019-04-03 12:52:50 +01008907#if defined(MBEDTLS_X509_CRT_PARSE_C)
8908void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8909 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8910 void *p_vrfy )
8911{
8912 ssl->f_vrfy = f_vrfy;
8913 ssl->p_vrfy = p_vrfy;
8914}
8915#endif
8916
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008917#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008918/*
8919 * Set EC J-PAKE password for current handshake
8920 */
8921int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8922 const unsigned char *pw,
8923 size_t pw_len )
8924{
8925 mbedtls_ecjpake_role role;
8926
Janos Follath8eb64132016-06-03 15:40:57 +01008927 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008928 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8929
8930 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8931 role = MBEDTLS_ECJPAKE_SERVER;
8932 else
8933 role = MBEDTLS_ECJPAKE_CLIENT;
8934
8935 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8936 role,
8937 MBEDTLS_MD_SHA256,
8938 MBEDTLS_ECP_DP_SECP256R1,
8939 pw, pw_len ) );
8940}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008941#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008944
8945static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8946{
8947 /* Remove reference to existing PSK, if any. */
8948#if defined(MBEDTLS_USE_PSA_CRYPTO)
8949 if( conf->psk_opaque != 0 )
8950 {
8951 /* The maintenance of the PSK key slot is the
8952 * user's responsibility. */
8953 conf->psk_opaque = 0;
8954 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008955 /* This and the following branch should never
8956 * be taken simultaenously as we maintain the
8957 * invariant that raw and opaque PSKs are never
8958 * configured simultaneously. As a safeguard,
8959 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008960#endif /* MBEDTLS_USE_PSA_CRYPTO */
8961 if( conf->psk != NULL )
8962 {
8963 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8964
8965 mbedtls_free( conf->psk );
8966 conf->psk = NULL;
8967 conf->psk_len = 0;
8968 }
8969
8970 /* Remove reference to PSK identity, if any. */
8971 if( conf->psk_identity != NULL )
8972 {
8973 mbedtls_free( conf->psk_identity );
8974 conf->psk_identity = NULL;
8975 conf->psk_identity_len = 0;
8976 }
8977}
8978
Hanno Becker7390c712018-11-15 13:33:04 +00008979/* This function assumes that PSK identity in the SSL config is unset.
8980 * It checks that the provided identity is well-formed and attempts
8981 * to make a copy of it in the SSL config.
8982 * On failure, the PSK identity in the config remains unset. */
8983static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8984 unsigned char const *psk_identity,
8985 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008986{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008987 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008988 if( psk_identity == NULL ||
8989 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008990 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008991 {
8992 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8993 }
8994
Hanno Becker7390c712018-11-15 13:33:04 +00008995 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8996 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008997 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008998
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008999 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009000 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02009001
9002 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02009003}
9004
Hanno Becker7390c712018-11-15 13:33:04 +00009005int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9006 const unsigned char *psk, size_t psk_len,
9007 const unsigned char *psk_identity, size_t psk_identity_len )
9008{
9009 int ret;
9010 /* Remove opaque/raw PSK + PSK Identity */
9011 ssl_conf_remove_psk( conf );
9012
9013 /* Check and set raw PSK */
9014 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9016 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9017 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9018 conf->psk_len = psk_len;
9019 memcpy( conf->psk, psk, conf->psk_len );
9020
9021 /* Check and set PSK Identity */
9022 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9023 if( ret != 0 )
9024 ssl_conf_remove_psk( conf );
9025
9026 return( ret );
9027}
9028
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009029static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9030{
9031#if defined(MBEDTLS_USE_PSA_CRYPTO)
9032 if( ssl->handshake->psk_opaque != 0 )
9033 {
9034 ssl->handshake->psk_opaque = 0;
9035 }
9036 else
9037#endif /* MBEDTLS_USE_PSA_CRYPTO */
9038 if( ssl->handshake->psk != NULL )
9039 {
9040 mbedtls_platform_zeroize( ssl->handshake->psk,
9041 ssl->handshake->psk_len );
9042 mbedtls_free( ssl->handshake->psk );
9043 ssl->handshake->psk_len = 0;
9044 }
9045}
9046
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009047int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9048 const unsigned char *psk, size_t psk_len )
9049{
9050 if( psk == NULL || ssl->handshake == NULL )
9051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9052
9053 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9055
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009056 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009057
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009058 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009059 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009060
9061 ssl->handshake->psk_len = psk_len;
9062 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9063
9064 return( 0 );
9065}
9066
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009067#if defined(MBEDTLS_USE_PSA_CRYPTO)
9068int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009069 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009070 const unsigned char *psk_identity,
9071 size_t psk_identity_len )
9072{
Hanno Becker7390c712018-11-15 13:33:04 +00009073 int ret;
9074 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009075 ssl_conf_remove_psk( conf );
9076
Hanno Becker7390c712018-11-15 13:33:04 +00009077 /* Check and set opaque PSK */
9078 if( psk_slot == 0 )
9079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009080 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009081
9082 /* Check and set PSK Identity */
9083 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9084 psk_identity_len );
9085 if( ret != 0 )
9086 ssl_conf_remove_psk( conf );
9087
9088 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009089}
9090
9091int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009092 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009093{
9094 if( psk_slot == 0 || ssl->handshake == NULL )
9095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9096
9097 ssl_remove_psk( ssl );
9098 ssl->handshake->psk_opaque = psk_slot;
9099 return( 0 );
9100}
9101#endif /* MBEDTLS_USE_PSA_CRYPTO */
9102
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009103void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009105 size_t),
9106 void *p_psk )
9107{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009108 conf->f_psk = f_psk;
9109 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009112
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009113#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009114
9115#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009116int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009117{
9118 int ret;
9119
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009120 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9121 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9122 {
9123 mbedtls_mpi_free( &conf->dhm_P );
9124 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009125 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009126 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009127
9128 return( 0 );
9129}
Hanno Becker470a8c42017-10-04 15:28:46 +01009130#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009131
Hanno Beckera90658f2017-10-04 15:29:08 +01009132int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9133 const unsigned char *dhm_P, size_t P_len,
9134 const unsigned char *dhm_G, size_t G_len )
9135{
9136 int ret;
9137
9138 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9139 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9140 {
9141 mbedtls_mpi_free( &conf->dhm_P );
9142 mbedtls_mpi_free( &conf->dhm_G );
9143 return( ret );
9144 }
9145
9146 return( 0 );
9147}
Paul Bakker5121ce52009-01-03 21:22:43 +00009148
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009149int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009150{
9151 int ret;
9152
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009153 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9154 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9155 {
9156 mbedtls_mpi_free( &conf->dhm_P );
9157 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009158 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009159 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009160
9161 return( 0 );
9162}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009163#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009164
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009165#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9166/*
9167 * Set the minimum length for Diffie-Hellman parameters
9168 */
9169void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9170 unsigned int bitlen )
9171{
9172 conf->dhm_min_bitlen = bitlen;
9173}
9174#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9175
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009176#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009177/*
9178 * Set allowed/preferred hashes for handshake signatures
9179 */
9180void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9181 const int *hashes )
9182{
9183 conf->sig_hashes = hashes;
9184}
Hanno Becker947194e2017-04-07 13:25:49 +01009185#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009186
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009187#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009188/*
9189 * Set the allowed elliptic curves
9190 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009191void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009192 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009193{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009194 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009195}
Hanno Becker947194e2017-04-07 13:25:49 +01009196#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009197
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009198#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009200{
Hanno Becker947194e2017-04-07 13:25:49 +01009201 /* Initialize to suppress unnecessary compiler warning */
9202 size_t hostname_len = 0;
9203
9204 /* Check if new hostname is valid before
9205 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009206 if( hostname != NULL )
9207 {
9208 hostname_len = strlen( hostname );
9209
9210 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9212 }
9213
9214 /* Now it's clear that we will overwrite the old hostname,
9215 * so we can free it safely */
9216
9217 if( ssl->hostname != NULL )
9218 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009219 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009220 mbedtls_free( ssl->hostname );
9221 }
9222
9223 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009224
Paul Bakker5121ce52009-01-03 21:22:43 +00009225 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009226 {
9227 ssl->hostname = NULL;
9228 }
9229 else
9230 {
9231 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009232 if( ssl->hostname == NULL )
9233 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009234
Hanno Becker947194e2017-04-07 13:25:49 +01009235 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009236
Hanno Becker947194e2017-04-07 13:25:49 +01009237 ssl->hostname[hostname_len] = '\0';
9238 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009239
9240 return( 0 );
9241}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009242#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009243
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009244#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009245void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009247 const unsigned char *, size_t),
9248 void *p_sni )
9249{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009250 conf->f_sni = f_sni;
9251 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009253#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009256int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009257{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009258 size_t cur_len, tot_len;
9259 const char **p;
9260
9261 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009262 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9263 * MUST NOT be truncated."
9264 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009265 */
9266 tot_len = 0;
9267 for( p = protos; *p != NULL; p++ )
9268 {
9269 cur_len = strlen( *p );
9270 tot_len += cur_len;
9271
9272 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009273 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009274 }
9275
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009276 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009277
9278 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009279}
9280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009282{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009283 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009284}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009286
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009287void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009288{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009289 conf->max_major_ver = major;
9290 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009291}
9292
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009293void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009294{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009295 conf->min_major_ver = major;
9296 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009297}
9298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009300void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009301{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009302 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009303}
9304#endif
9305
Janos Follath088ce432017-04-10 12:42:31 +01009306#if defined(MBEDTLS_SSL_SRV_C)
9307void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9308 char cert_req_ca_list )
9309{
9310 conf->cert_req_ca_list = cert_req_ca_list;
9311}
9312#endif
9313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009314#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009315void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009316{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009317 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009318}
9319#endif
9320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009322void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009323{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009324 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009325}
9326#endif
9327
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009328#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009329void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009330{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009331 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009332}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009333#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009336int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009337{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009339 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009342 }
9343
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009344 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009345
9346 return( 0 );
9347}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009351void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009352{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009353 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009358void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009359{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009360 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009361}
9362#endif
9363
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009364void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009365{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009366 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009367}
9368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009370void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009371{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009372 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009373}
9374
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009375void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009376{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009377 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009378}
9379
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009380void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009381 const unsigned char period[8] )
9382{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009383 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009388#if defined(MBEDTLS_SSL_CLI_C)
9389void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009390{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009391 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009392}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009393#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009394
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009395#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009396void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9397 mbedtls_ssl_ticket_write_t *f_ticket_write,
9398 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9399 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009400{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009401 conf->f_ticket_write = f_ticket_write;
9402 conf->f_ticket_parse = f_ticket_parse;
9403 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009404}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009405#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009407
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009408#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9409void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9410 mbedtls_ssl_export_keys_t *f_export_keys,
9411 void *p_export_keys )
9412{
9413 conf->f_export_keys = f_export_keys;
9414 conf->p_export_keys = p_export_keys;
9415}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009416
9417void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9418 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9419 void *p_export_keys )
9420{
9421 conf->f_export_keys_ext = f_export_keys_ext;
9422 conf->p_export_keys = p_export_keys;
9423}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009424#endif
9425
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009426#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009427void mbedtls_ssl_conf_async_private_cb(
9428 mbedtls_ssl_config *conf,
9429 mbedtls_ssl_async_sign_t *f_async_sign,
9430 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9431 mbedtls_ssl_async_resume_t *f_async_resume,
9432 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009433 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009434{
9435 conf->f_async_sign_start = f_async_sign;
9436 conf->f_async_decrypt_start = f_async_decrypt;
9437 conf->f_async_resume = f_async_resume;
9438 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009439 conf->p_async_config_data = async_config_data;
9440}
9441
Gilles Peskine8f97af72018-04-26 11:46:10 +02009442void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9443{
9444 return( conf->p_async_config_data );
9445}
9446
Gilles Peskine1febfef2018-04-30 11:54:39 +02009447void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009448{
9449 if( ssl->handshake == NULL )
9450 return( NULL );
9451 else
9452 return( ssl->handshake->user_async_ctx );
9453}
9454
Gilles Peskine1febfef2018-04-30 11:54:39 +02009455void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009456 void *ctx )
9457{
9458 if( ssl->handshake != NULL )
9459 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009460}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009461#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009462
Paul Bakker5121ce52009-01-03 21:22:43 +00009463/*
9464 * SSL get accessors
9465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009467{
9468 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9469}
9470
Hanno Becker8b170a02017-10-10 11:51:19 +01009471int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9472{
9473 /*
9474 * Case A: We're currently holding back
9475 * a message for further processing.
9476 */
9477
9478 if( ssl->keep_current_message == 1 )
9479 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009480 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009481 return( 1 );
9482 }
9483
9484 /*
9485 * Case B: Further records are pending in the current datagram.
9486 */
9487
9488#if defined(MBEDTLS_SSL_PROTO_DTLS)
9489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9490 ssl->in_left > ssl->next_record_offset )
9491 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009493 return( 1 );
9494 }
9495#endif /* MBEDTLS_SSL_PROTO_DTLS */
9496
9497 /*
9498 * Case C: A handshake message is being processed.
9499 */
9500
Hanno Becker8b170a02017-10-10 11:51:19 +01009501 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9502 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009504 return( 1 );
9505 }
9506
9507 /*
9508 * Case D: An application data message is being processed
9509 */
9510 if( ssl->in_offt != NULL )
9511 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009513 return( 1 );
9514 }
9515
9516 /*
9517 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009518 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009519 * we implement support for multiple alerts in single records.
9520 */
9521
9522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9523 return( 0 );
9524}
9525
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009526uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009527{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009528 if( ssl->session != NULL )
9529 return( ssl->session->verify_result );
9530
9531 if( ssl->session_negotiate != NULL )
9532 return( ssl->session_negotiate->verify_result );
9533
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009534 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009535}
9536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009538{
Paul Bakker926c8e42013-03-06 10:23:34 +01009539 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009540 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009543}
9544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009546{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009548 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009549 {
9550 switch( ssl->minor_ver )
9551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009553 return( "DTLSv1.0" );
9554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009556 return( "DTLSv1.2" );
9557
9558 default:
9559 return( "unknown (DTLS)" );
9560 }
9561 }
9562#endif
9563
Paul Bakker43ca69c2011-01-15 17:35:19 +00009564 switch( ssl->minor_ver )
9565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009567 return( "SSLv3.0" );
9568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009570 return( "TLSv1.0" );
9571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009573 return( "TLSv1.1" );
9574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009576 return( "TLSv1.2" );
9577
Paul Bakker43ca69c2011-01-15 17:35:19 +00009578 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009579 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009580 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009581}
9582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009584{
Hanno Becker3136ede2018-08-17 15:28:19 +01009585 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009587 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009588
Hanno Becker5903de42019-05-03 14:46:38 +01009589 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9590
Hanno Becker78640902018-08-13 16:35:15 +01009591 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009592 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594#if defined(MBEDTLS_ZLIB_SUPPORT)
9595 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9596 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009597#endif
9598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009599 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601 case MBEDTLS_MODE_GCM:
9602 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009603 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009605 transform_expansion = transform->minlen;
9606 break;
9607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009609
9610 block_size = mbedtls_cipher_get_block_size(
9611 &transform->cipher_ctx_enc );
9612
Hanno Becker3136ede2018-08-17 15:28:19 +01009613 /* Expansion due to the addition of the MAC. */
9614 transform_expansion += transform->maclen;
9615
9616 /* Expansion due to the addition of CBC padding;
9617 * Theoretically up to 256 bytes, but we never use
9618 * more than the block size of the underlying cipher. */
9619 transform_expansion += block_size;
9620
9621 /* For TLS 1.1 or higher, an explicit IV is added
9622 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009623#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9624 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009625 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009626#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009627
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009628 break;
9629
9630 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009633 }
9634
Hanno Beckera0e20d02019-05-15 14:03:01 +01009635#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009636 if( transform->out_cid_len != 0 )
9637 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009638#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009639
Hanno Becker5903de42019-05-03 14:46:38 +01009640 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009641}
9642
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009643#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9644size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9645{
9646 size_t max_len;
9647
9648 /*
9649 * Assume mfl_code is correct since it was checked when set
9650 */
Angus Grattond8213d02016-05-25 20:56:48 +10009651 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009652
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009653 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009654 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009655 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009656 {
Angus Grattond8213d02016-05-25 20:56:48 +10009657 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009658 }
9659
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009660 /* During a handshake, use the value being negotiated */
9661 if( ssl->session_negotiate != NULL &&
9662 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9663 {
9664 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9665 }
9666
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009667 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009668}
9669#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9670
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009671#if defined(MBEDTLS_SSL_PROTO_DTLS)
9672static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9673{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009674 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9675 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9676 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9677 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9678 return ( 0 );
9679
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009680 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9681 return( ssl->mtu );
9682
9683 if( ssl->mtu == 0 )
9684 return( ssl->handshake->mtu );
9685
9686 return( ssl->mtu < ssl->handshake->mtu ?
9687 ssl->mtu : ssl->handshake->mtu );
9688}
9689#endif /* MBEDTLS_SSL_PROTO_DTLS */
9690
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009691int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9692{
9693 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9694
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009695#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9696 !defined(MBEDTLS_SSL_PROTO_DTLS)
9697 (void) ssl;
9698#endif
9699
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009700#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9701 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9702
9703 if( max_len > mfl )
9704 max_len = mfl;
9705#endif
9706
9707#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009708 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009709 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009710 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009711 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9712 const size_t overhead = (size_t) ret;
9713
9714 if( ret < 0 )
9715 return( ret );
9716
9717 if( mtu <= overhead )
9718 {
9719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9720 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9721 }
9722
9723 if( max_len > mtu - overhead )
9724 max_len = mtu - overhead;
9725 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009726#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009727
Hanno Becker0defedb2018-08-10 12:35:02 +01009728#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9729 !defined(MBEDTLS_SSL_PROTO_DTLS)
9730 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009731#endif
9732
9733 return( (int) max_len );
9734}
9735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736#if defined(MBEDTLS_X509_CRT_PARSE_C)
9737const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009738{
9739 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009740 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009741
Hanno Beckere6824572019-02-07 13:18:46 +00009742#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009743 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009744#else
9745 return( NULL );
9746#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009747}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009751int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9752 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009753{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009754 if( ssl == NULL ||
9755 dst == NULL ||
9756 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009757 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009760 }
9761
Hanno Becker52055ae2019-02-06 14:30:46 +00009762 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009763}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009765
Paul Bakker5121ce52009-01-03 21:22:43 +00009766/*
Paul Bakker1961b702013-01-25 14:49:24 +01009767 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009770{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009772
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009773 if( ssl == NULL || ssl->conf == NULL )
9774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009777 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009781 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009782 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009783#endif
9784
Paul Bakker1961b702013-01-25 14:49:24 +01009785 return( ret );
9786}
9787
9788/*
9789 * Perform the SSL handshake
9790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009792{
9793 int ret = 0;
9794
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009795 if( ssl == NULL || ssl->conf == NULL )
9796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009800 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009803
9804 if( ret != 0 )
9805 break;
9806 }
9807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009809
9810 return( ret );
9811}
9812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813#if defined(MBEDTLS_SSL_RENEGOTIATION)
9814#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009815/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009816 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009819{
9820 int ret;
9821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009823
9824 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9826 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009827
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009828 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009829 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009831 return( ret );
9832 }
9833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009835
9836 return( 0 );
9837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009839
9840/*
9841 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842 * - any side: calling mbedtls_ssl_renegotiate(),
9843 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9844 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009845 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009846 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009848 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009850{
9851 int ret;
9852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009854
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009855 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9856 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009857
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009858 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9859 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009861 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009863 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009864 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009865 ssl->handshake->out_msg_seq = 1;
9866 else
9867 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009868 }
9869#endif
9870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9872 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009876 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009877 return( ret );
9878 }
9879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009881
9882 return( 0 );
9883}
9884
9885/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009886 * Renegotiate current connection on client,
9887 * or request renegotiation on server
9888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009889int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009890{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009892
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009893 if( ssl == NULL || ssl->conf == NULL )
9894 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009896#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009897 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009898 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009903 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009904
9905 /* Did we already try/start sending HelloRequest? */
9906 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009908
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009909 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009914 /*
9915 * On client, either start the renegotiation process or,
9916 * if already in progress, continue the handshake
9917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009918 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9921 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009922
9923 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009925 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009926 return( ret );
9927 }
9928 }
9929 else
9930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009933 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009934 return( ret );
9935 }
9936 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009938
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009939 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009940}
9941
9942/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009943 * Check record counters and renegotiate if they're above the limit.
9944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009946{
Andres AG2196c7f2016-12-15 17:01:16 +00009947 size_t ep_len = ssl_ep_len( ssl );
9948 int in_ctr_cmp;
9949 int out_ctr_cmp;
9950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009951 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9952 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009953 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009954 {
9955 return( 0 );
9956 }
9957
Andres AG2196c7f2016-12-15 17:01:16 +00009958 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9959 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009960 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009961 ssl->conf->renego_period + ep_len, 8 - ep_len );
9962
9963 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009964 {
9965 return( 0 );
9966 }
9967
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009970}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009972
9973/*
9974 * Receive application data decrypted from the SSL layer
9975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009977{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009978 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009979 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009980
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009981 if( ssl == NULL || ssl->conf == NULL )
9982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009987 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009989 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009990 return( ret );
9991
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009992 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009993 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009994 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009995 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009996 return( ret );
9997 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009998 }
9999#endif
10000
Hanno Becker4a810fb2017-05-24 16:27:30 +010010001 /*
10002 * Check if renegotiation is necessary and/or handshake is
10003 * in process. If yes, perform/continue, and fall through
10004 * if an unexpected packet is received while the client
10005 * is waiting for the ServerHello.
10006 *
10007 * (There is no equivalent to the last condition on
10008 * the server-side as it is not treated as within
10009 * a handshake while waiting for the ClientHello
10010 * after a renegotiation request.)
10011 */
10012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010013#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010014 ret = ssl_check_ctr_renegotiate( ssl );
10015 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10016 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010018 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010019 return( ret );
10020 }
10021#endif
10022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010025 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010026 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10027 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010029 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010030 return( ret );
10031 }
10032 }
10033
Hanno Beckere41158b2017-10-23 13:30:32 +010010034 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010035 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010036 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010037 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010038 if( ssl->f_get_timer != NULL &&
10039 ssl->f_get_timer( ssl->p_timer ) == -1 )
10040 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010041 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010042 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010043
Hanno Becker327c93b2018-08-15 13:56:18 +010010044 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010045 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010046 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10047 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010048
Hanno Becker4a810fb2017-05-24 16:27:30 +010010049 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10050 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010051 }
10052
10053 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010055 {
10056 /*
10057 * OpenSSL sends empty messages to randomize the IV
10058 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010059 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010062 return( 0 );
10063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010065 return( ret );
10066 }
10067 }
10068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010069 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010072
Hanno Becker4a810fb2017-05-24 16:27:30 +010010073 /*
10074 * - For client-side, expect SERVER_HELLO_REQUEST.
10075 * - For server-side, expect CLIENT_HELLO.
10076 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10077 */
10078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010079#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010080 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010082 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010085
10086 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010088 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010089 {
10090 continue;
10091 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010092#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010094 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010095#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010096
Hanno Becker4a810fb2017-05-24 16:27:30 +010010097#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010098 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010102
10103 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010104#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010105 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010106 {
10107 continue;
10108 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010109#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010110 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010111 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010112#endif /* MBEDTLS_SSL_SRV_C */
10113
Hanno Becker21df7f92017-10-17 11:03:26 +010010114#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010115 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010116 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10117 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10118 ssl->conf->allow_legacy_renegotiation ==
10119 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10120 {
10121 /*
10122 * Accept renegotiation request
10123 */
Paul Bakker48916f92012-09-16 19:57:18 +000010124
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010125 /* DTLS clients need to know renego is server-initiated */
10126#if defined(MBEDTLS_SSL_PROTO_DTLS)
10127 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10128 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10129 {
10130 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10131 }
10132#endif
10133 ret = ssl_start_renegotiation( ssl );
10134 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10135 ret != 0 )
10136 {
10137 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10138 return( ret );
10139 }
10140 }
10141 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010142#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010143 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010144 /*
10145 * Refuse renegotiation
10146 */
10147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010150#if defined(MBEDTLS_SSL_PROTO_SSL3)
10151 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010152 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010153 /* SSLv3 does not have a "no_renegotiation" warning, so
10154 we send a fatal alert and abort the connection. */
10155 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10156 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10157 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010158 }
10159 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010160#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10161#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10162 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10163 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010165 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10166 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10167 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010168 {
10169 return( ret );
10170 }
Paul Bakker48916f92012-09-16 19:57:18 +000010171 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010172 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010173#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10174 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010178 }
Paul Bakker48916f92012-09-16 19:57:18 +000010179 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010180
Hanno Becker90333da2017-10-10 11:27:13 +010010181 /* At this point, we don't know whether the renegotiation has been
10182 * completed or not. The cases to consider are the following:
10183 * 1) The renegotiation is complete. In this case, no new record
10184 * has been read yet.
10185 * 2) The renegotiation is incomplete because the client received
10186 * an application data record while awaiting the ServerHello.
10187 * 3) The renegotiation is incomplete because the client received
10188 * a non-handshake, non-application data message while awaiting
10189 * the ServerHello.
10190 * In each of these case, looping will be the proper action:
10191 * - For 1), the next iteration will read a new record and check
10192 * if it's application data.
10193 * - For 2), the loop condition isn't satisfied as application data
10194 * is present, hence continue is the same as break
10195 * - For 3), the loop condition is satisfied and read_record
10196 * will re-deliver the message that was held back by the client
10197 * when expecting the ServerHello.
10198 */
10199 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010200 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010201#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010203 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010204 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010205 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010206 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010209 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010211 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010212 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010213 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10217 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010220 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010221 }
10222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10226 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010227 }
10228
10229 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010230
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010231 /* We're going to return something now, cancel timer,
10232 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010234 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010235
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010237 /* If we requested renego but received AppData, resend HelloRequest.
10238 * Do it now, after setting in_offt, to avoid taking this branch
10239 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010241 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010243 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010244 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010246 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010247 return( ret );
10248 }
10249 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010251#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010252 }
10253
10254 n = ( len < ssl->in_msglen )
10255 ? len : ssl->in_msglen;
10256
10257 memcpy( buf, ssl->in_offt, n );
10258 ssl->in_msglen -= n;
10259
10260 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010261 {
10262 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010263 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010264 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010265 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010266 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010267 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010268 /* more data available */
10269 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010270 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010273
Paul Bakker23986e52011-04-24 08:57:21 +000010274 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010275}
10276
10277/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010278 * Send application data to be encrypted by the SSL layer, taking care of max
10279 * fragment length and buffer size.
10280 *
10281 * According to RFC 5246 Section 6.2.1:
10282 *
10283 * Zero-length fragments of Application data MAY be sent as they are
10284 * potentially useful as a traffic analysis countermeasure.
10285 *
10286 * Therefore, it is possible that the input message length is 0 and the
10287 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010288 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010289static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010290 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010291{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010292 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10293 const size_t max_len = (size_t) ret;
10294
10295 if( ret < 0 )
10296 {
10297 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10298 return( ret );
10299 }
10300
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010301 if( len > max_len )
10302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010307 "maximum fragment length: %d > %d",
10308 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010310 }
10311 else
10312#endif
10313 len = max_len;
10314 }
Paul Bakker887bd502011-06-08 13:10:54 +000010315
Paul Bakker5121ce52009-01-03 21:22:43 +000010316 if( ssl->out_left != 0 )
10317 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010318 /*
10319 * The user has previously tried to send the data and
10320 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10321 * written. In this case, we expect the high-level write function
10322 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010327 return( ret );
10328 }
10329 }
Paul Bakker887bd502011-06-08 13:10:54 +000010330 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010331 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010332 /*
10333 * The user is trying to send a message the first time, so we need to
10334 * copy the data into the internal buffers and setup the data structure
10335 * to keep track of partial writes
10336 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010337 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010338 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010339 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010340
Hanno Becker67bc7c32018-08-06 11:33:50 +010010341 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010344 return( ret );
10345 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010346 }
10347
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010348 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010349}
10350
10351/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010352 * Write application data, doing 1/n-1 splitting if necessary.
10353 *
10354 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010355 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010356 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010359static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010360 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010361{
10362 int ret;
10363
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010364 if( ssl->conf->cbc_record_splitting ==
10365 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010366 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10368 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10369 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010370 {
10371 return( ssl_write_real( ssl, buf, len ) );
10372 }
10373
10374 if( ssl->split_done == 0 )
10375 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010376 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010377 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010378 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010379 }
10380
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010381 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10382 return( ret );
10383 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010384
10385 return( ret + 1 );
10386}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010387#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010388
10389/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010390 * Write application data (public-facing wrapper)
10391 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010392int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010393{
10394 int ret;
10395
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010397
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010398 if( ssl == NULL || ssl->conf == NULL )
10399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10400
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010401#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010402 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10403 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010404 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010405 return( ret );
10406 }
10407#endif
10408
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010409 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010410 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010411 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010412 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010414 return( ret );
10415 }
10416 }
10417
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010418#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010419 ret = ssl_write_split( ssl, buf, len );
10420#else
10421 ret = ssl_write_real( ssl, buf, len );
10422#endif
10423
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010425
10426 return( ret );
10427}
10428
10429/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010430 * Notify the peer that the connection is being closed
10431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010432int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010433{
10434 int ret;
10435
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010436 if( ssl == NULL || ssl->conf == NULL )
10437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010440
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010441 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010442 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010446 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10447 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10448 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010451 return( ret );
10452 }
10453 }
10454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010456
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010457 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010458}
10459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010460void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010461{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010462 if( transform == NULL )
10463 return;
10464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010465#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010466 deflateEnd( &transform->ctx_deflate );
10467 inflateEnd( &transform->ctx_inflate );
10468#endif
10469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010470 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10471 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010472
Hanno Beckerd56ed242018-01-03 15:32:51 +000010473#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010474 mbedtls_md_free( &transform->md_ctx_enc );
10475 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010476#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010477
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010478 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010479}
10480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010481#if defined(MBEDTLS_X509_CRT_PARSE_C)
10482static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010484 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010485
10486 while( cur != NULL )
10487 {
10488 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010489 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010490 cur = next;
10491 }
10492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010493#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010494
Hanno Becker0271f962018-08-16 13:23:47 +010010495#if defined(MBEDTLS_SSL_PROTO_DTLS)
10496
10497static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10498{
10499 unsigned offset;
10500 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10501
10502 if( hs == NULL )
10503 return;
10504
Hanno Becker283f5ef2018-08-24 09:34:47 +010010505 ssl_free_buffered_record( ssl );
10506
Hanno Becker0271f962018-08-16 13:23:47 +010010507 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010508 ssl_buffering_free_slot( ssl, offset );
10509}
10510
10511static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10512 uint8_t slot )
10513{
10514 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10515 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010516
10517 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10518 return;
10519
Hanno Beckere605b192018-08-21 15:59:07 +010010520 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010521 {
Hanno Beckere605b192018-08-21 15:59:07 +010010522 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010523 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010524 mbedtls_free( hs_buf->data );
10525 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010526 }
10527}
10528
10529#endif /* MBEDTLS_SSL_PROTO_DTLS */
10530
Gilles Peskine9b562d52018-04-25 20:32:43 +020010531void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010532{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010533 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10534
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010535 if( handshake == NULL )
10536 return;
10537
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010538#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10539 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10540 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010541 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010542 handshake->async_in_progress = 0;
10543 }
10544#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10545
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010546#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10547 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10548 mbedtls_md5_free( &handshake->fin_md5 );
10549 mbedtls_sha1_free( &handshake->fin_sha1 );
10550#endif
10551#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10552#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010553#if defined(MBEDTLS_USE_PSA_CRYPTO)
10554 psa_hash_abort( &handshake->fin_sha256_psa );
10555#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010556 mbedtls_sha256_free( &handshake->fin_sha256 );
10557#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010558#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010559#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010560#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010561 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010562#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010563 mbedtls_sha512_free( &handshake->fin_sha512 );
10564#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010565#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010566#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010568#if defined(MBEDTLS_DHM_C)
10569 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010570#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010571#if defined(MBEDTLS_ECDH_C)
10572 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010573#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010574#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010575 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010576#if defined(MBEDTLS_SSL_CLI_C)
10577 mbedtls_free( handshake->ecjpake_cache );
10578 handshake->ecjpake_cache = NULL;
10579 handshake->ecjpake_cache_len = 0;
10580#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010581#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010582
Janos Follath4ae5c292016-02-10 11:27:43 +000010583#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10584 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010585 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010586 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010587#endif
10588
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010589#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10590 if( handshake->psk != NULL )
10591 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010592 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010593 mbedtls_free( handshake->psk );
10594 }
10595#endif
10596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10598 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010599 /*
10600 * Free only the linked list wrapper, not the keys themselves
10601 * since the belong to the SNI callback
10602 */
10603 if( handshake->sni_key_cert != NULL )
10604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010605 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010606
10607 while( cur != NULL )
10608 {
10609 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010610 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010611 cur = next;
10612 }
10613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010614#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010615
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010616#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010617 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010618 if( handshake->ecrs_peer_cert != NULL )
10619 {
10620 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10621 mbedtls_free( handshake->ecrs_peer_cert );
10622 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010623#endif
10624
Hanno Becker75173122019-02-06 16:18:31 +000010625#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10626 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10627 mbedtls_pk_free( &handshake->peer_pubkey );
10628#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630#if defined(MBEDTLS_SSL_PROTO_DTLS)
10631 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010632 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010633 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010634#endif
10635
Hanno Becker4a63ed42019-01-08 11:39:35 +000010636#if defined(MBEDTLS_ECDH_C) && \
10637 defined(MBEDTLS_USE_PSA_CRYPTO)
10638 psa_destroy_key( handshake->ecdh_psa_privkey );
10639#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10640
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010641 mbedtls_platform_zeroize( handshake,
10642 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010643}
10644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010645void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010646{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010647 if( session == NULL )
10648 return;
10649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010650#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010651 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010652#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010653
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010654#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010656#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010657
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010658 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010659}
10660
Paul Bakker5121ce52009-01-03 21:22:43 +000010661/*
10662 * Free an SSL context
10663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010665{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010666 if( ssl == NULL )
10667 return;
10668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010670
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010671 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010672 {
Angus Grattond8213d02016-05-25 20:56:48 +100010673 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010674 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010675 }
10676
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010677 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010678 {
Angus Grattond8213d02016-05-25 20:56:48 +100010679 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010680 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010681 }
10682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010684 if( ssl->compress_buf != NULL )
10685 {
Angus Grattond8213d02016-05-25 20:56:48 +100010686 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010688 }
10689#endif
10690
Paul Bakker48916f92012-09-16 19:57:18 +000010691 if( ssl->transform )
10692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010693 mbedtls_ssl_transform_free( ssl->transform );
10694 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010695 }
10696
10697 if( ssl->handshake )
10698 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010699 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010700 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10701 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703 mbedtls_free( ssl->handshake );
10704 mbedtls_free( ssl->transform_negotiate );
10705 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010706 }
10707
Paul Bakkerc0463502013-02-14 11:19:38 +010010708 if( ssl->session )
10709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010710 mbedtls_ssl_session_free( ssl->session );
10711 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010712 }
10713
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010714#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010715 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010716 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010717 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010719 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010720#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010722#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10723 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10726 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010727 }
10728#endif
10729
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010730#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010731 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010732#endif
10733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010735
Paul Bakker86f04f42013-02-14 11:20:09 +010010736 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010737 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010738}
10739
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010740/*
10741 * Initialze mbedtls_ssl_config
10742 */
10743void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10744{
10745 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10746}
10747
Simon Butcherc97b6972015-12-27 23:48:17 +000010748#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010749static int ssl_preset_default_hashes[] = {
10750#if defined(MBEDTLS_SHA512_C)
10751 MBEDTLS_MD_SHA512,
10752 MBEDTLS_MD_SHA384,
10753#endif
10754#if defined(MBEDTLS_SHA256_C)
10755 MBEDTLS_MD_SHA256,
10756 MBEDTLS_MD_SHA224,
10757#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010758#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010759 MBEDTLS_MD_SHA1,
10760#endif
10761 MBEDTLS_MD_NONE
10762};
Simon Butcherc97b6972015-12-27 23:48:17 +000010763#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010764
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010765static int ssl_preset_suiteb_ciphersuites[] = {
10766 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10767 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10768 0
10769};
10770
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010771#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010772static int ssl_preset_suiteb_hashes[] = {
10773 MBEDTLS_MD_SHA256,
10774 MBEDTLS_MD_SHA384,
10775 MBEDTLS_MD_NONE
10776};
10777#endif
10778
10779#if defined(MBEDTLS_ECP_C)
10780static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010781#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010782 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010783#endif
10784#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010785 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010786#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010787 MBEDTLS_ECP_DP_NONE
10788};
10789#endif
10790
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010791/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010792 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010793 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010794int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010795 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010796{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010797#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010798 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010799#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010800
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010801 /* Use the functions here so that they are covered in tests,
10802 * but otherwise access member directly for efficiency */
10803 mbedtls_ssl_conf_endpoint( conf, endpoint );
10804 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010805
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010806 /*
10807 * Things that are common to all presets
10808 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010809#if defined(MBEDTLS_SSL_CLI_C)
10810 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10811 {
10812 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10813#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10814 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10815#endif
10816 }
10817#endif
10818
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010819#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010820 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010821#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010822
10823#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10824 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10825#endif
10826
10827#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10828 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10829#endif
10830
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010831#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10832 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10833#endif
10834
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010835#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010836 conf->f_cookie_write = ssl_cookie_write_dummy;
10837 conf->f_cookie_check = ssl_cookie_check_dummy;
10838#endif
10839
10840#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10841 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10842#endif
10843
Janos Follath088ce432017-04-10 12:42:31 +010010844#if defined(MBEDTLS_SSL_SRV_C)
10845 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10846#endif
10847
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010848#if defined(MBEDTLS_SSL_PROTO_DTLS)
10849 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10850 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10851#endif
10852
10853#if defined(MBEDTLS_SSL_RENEGOTIATION)
10854 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010855 memset( conf->renego_period, 0x00, 2 );
10856 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010857#endif
10858
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010859#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10860 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10861 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010862 const unsigned char dhm_p[] =
10863 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10864 const unsigned char dhm_g[] =
10865 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10866
Hanno Beckera90658f2017-10-04 15:29:08 +010010867 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10868 dhm_p, sizeof( dhm_p ),
10869 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010870 {
10871 return( ret );
10872 }
10873 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010874#endif
10875
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010876 /*
10877 * Preset-specific defaults
10878 */
10879 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010880 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010881 /*
10882 * NSA Suite B
10883 */
10884 case MBEDTLS_SSL_PRESET_SUITEB:
10885 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10886 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10887 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10888 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10889
10890 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10891 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10892 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10893 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10894 ssl_preset_suiteb_ciphersuites;
10895
10896#if defined(MBEDTLS_X509_CRT_PARSE_C)
10897 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010898#endif
10899
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010900#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010901 conf->sig_hashes = ssl_preset_suiteb_hashes;
10902#endif
10903
10904#if defined(MBEDTLS_ECP_C)
10905 conf->curve_list = ssl_preset_suiteb_curves;
10906#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010907 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010908
10909 /*
10910 * Default
10911 */
10912 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010913 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10914 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10915 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10916 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10917 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10918 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10919 MBEDTLS_SSL_MIN_MINOR_VERSION :
10920 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010921 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10922 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10923
10924#if defined(MBEDTLS_SSL_PROTO_DTLS)
10925 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10926 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10927#endif
10928
10929 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10930 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10931 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10932 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10933 mbedtls_ssl_list_ciphersuites();
10934
10935#if defined(MBEDTLS_X509_CRT_PARSE_C)
10936 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10937#endif
10938
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010939#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010940 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010941#endif
10942
10943#if defined(MBEDTLS_ECP_C)
10944 conf->curve_list = mbedtls_ecp_grp_id_list();
10945#endif
10946
10947#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10948 conf->dhm_min_bitlen = 1024;
10949#endif
10950 }
10951
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010952 return( 0 );
10953}
10954
10955/*
10956 * Free mbedtls_ssl_config
10957 */
10958void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10959{
10960#if defined(MBEDTLS_DHM_C)
10961 mbedtls_mpi_free( &conf->dhm_P );
10962 mbedtls_mpi_free( &conf->dhm_G );
10963#endif
10964
10965#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10966 if( conf->psk != NULL )
10967 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010968 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010969 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010970 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010971 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010972 }
10973
10974 if( conf->psk_identity != NULL )
10975 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010976 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010977 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010978 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010979 conf->psk_identity_len = 0;
10980 }
10981#endif
10982
10983#if defined(MBEDTLS_X509_CRT_PARSE_C)
10984 ssl_key_cert_free( conf->key_cert );
10985#endif
10986
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010987 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010988}
10989
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010990#if defined(MBEDTLS_PK_C) && \
10991 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010992/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010993 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010995unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010996{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010997#if defined(MBEDTLS_RSA_C)
10998 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10999 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011000#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011001#if defined(MBEDTLS_ECDSA_C)
11002 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
11003 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011004#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011005 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011006}
11007
Hanno Becker7e5437a2017-04-28 17:15:26 +010011008unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11009{
11010 switch( type ) {
11011 case MBEDTLS_PK_RSA:
11012 return( MBEDTLS_SSL_SIG_RSA );
11013 case MBEDTLS_PK_ECDSA:
11014 case MBEDTLS_PK_ECKEY:
11015 return( MBEDTLS_SSL_SIG_ECDSA );
11016 default:
11017 return( MBEDTLS_SSL_SIG_ANON );
11018 }
11019}
11020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011021mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011022{
11023 switch( sig )
11024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025#if defined(MBEDTLS_RSA_C)
11026 case MBEDTLS_SSL_SIG_RSA:
11027 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011028#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011029#if defined(MBEDTLS_ECDSA_C)
11030 case MBEDTLS_SSL_SIG_ECDSA:
11031 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011032#endif
11033 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011034 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011035 }
11036}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011037#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011038
Hanno Becker7e5437a2017-04-28 17:15:26 +010011039#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11040 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11041
11042/* Find an entry in a signature-hash set matching a given hash algorithm. */
11043mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11044 mbedtls_pk_type_t sig_alg )
11045{
11046 switch( sig_alg )
11047 {
11048 case MBEDTLS_PK_RSA:
11049 return( set->rsa );
11050 case MBEDTLS_PK_ECDSA:
11051 return( set->ecdsa );
11052 default:
11053 return( MBEDTLS_MD_NONE );
11054 }
11055}
11056
11057/* Add a signature-hash-pair to a signature-hash set */
11058void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11059 mbedtls_pk_type_t sig_alg,
11060 mbedtls_md_type_t md_alg )
11061{
11062 switch( sig_alg )
11063 {
11064 case MBEDTLS_PK_RSA:
11065 if( set->rsa == MBEDTLS_MD_NONE )
11066 set->rsa = md_alg;
11067 break;
11068
11069 case MBEDTLS_PK_ECDSA:
11070 if( set->ecdsa == MBEDTLS_MD_NONE )
11071 set->ecdsa = md_alg;
11072 break;
11073
11074 default:
11075 break;
11076 }
11077}
11078
11079/* Allow exactly one hash algorithm for each signature. */
11080void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11081 mbedtls_md_type_t md_alg )
11082{
11083 set->rsa = md_alg;
11084 set->ecdsa = md_alg;
11085}
11086
11087#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11088 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11089
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011090/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011091 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011093mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011094{
11095 switch( hash )
11096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011097#if defined(MBEDTLS_MD5_C)
11098 case MBEDTLS_SSL_HASH_MD5:
11099 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011100#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011101#if defined(MBEDTLS_SHA1_C)
11102 case MBEDTLS_SSL_HASH_SHA1:
11103 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011104#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011105#if defined(MBEDTLS_SHA256_C)
11106 case MBEDTLS_SSL_HASH_SHA224:
11107 return( MBEDTLS_MD_SHA224 );
11108 case MBEDTLS_SSL_HASH_SHA256:
11109 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011111#if defined(MBEDTLS_SHA512_C)
11112 case MBEDTLS_SSL_HASH_SHA384:
11113 return( MBEDTLS_MD_SHA384 );
11114 case MBEDTLS_SSL_HASH_SHA512:
11115 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011116#endif
11117 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011118 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011119 }
11120}
11121
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011122/*
11123 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11124 */
11125unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11126{
11127 switch( md )
11128 {
11129#if defined(MBEDTLS_MD5_C)
11130 case MBEDTLS_MD_MD5:
11131 return( MBEDTLS_SSL_HASH_MD5 );
11132#endif
11133#if defined(MBEDTLS_SHA1_C)
11134 case MBEDTLS_MD_SHA1:
11135 return( MBEDTLS_SSL_HASH_SHA1 );
11136#endif
11137#if defined(MBEDTLS_SHA256_C)
11138 case MBEDTLS_MD_SHA224:
11139 return( MBEDTLS_SSL_HASH_SHA224 );
11140 case MBEDTLS_MD_SHA256:
11141 return( MBEDTLS_SSL_HASH_SHA256 );
11142#endif
11143#if defined(MBEDTLS_SHA512_C)
11144 case MBEDTLS_MD_SHA384:
11145 return( MBEDTLS_SSL_HASH_SHA384 );
11146 case MBEDTLS_MD_SHA512:
11147 return( MBEDTLS_SSL_HASH_SHA512 );
11148#endif
11149 default:
11150 return( MBEDTLS_SSL_HASH_NONE );
11151 }
11152}
11153
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011154#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011155/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011156 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011157 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011158 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011159int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011161 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011162
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011163 if( ssl->conf->curve_list == NULL )
11164 return( -1 );
11165
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011166 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011167 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011168 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011169
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011170 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011171}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011172#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011173
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011174#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011175/*
11176 * Check if a hash proposed by the peer is in our list.
11177 * Return 0 if we're willing to use it, -1 otherwise.
11178 */
11179int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11180 mbedtls_md_type_t md )
11181{
11182 const int *cur;
11183
11184 if( ssl->conf->sig_hashes == NULL )
11185 return( -1 );
11186
11187 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11188 if( *cur == (int) md )
11189 return( 0 );
11190
11191 return( -1 );
11192}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011193#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011195#if defined(MBEDTLS_X509_CRT_PARSE_C)
11196int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11197 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011198 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011199 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011200{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011201 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011202#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011203 int usage = 0;
11204#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011205#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011206 const char *ext_oid;
11207 size_t ext_len;
11208#endif
11209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011210#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11211 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011212 ((void) cert);
11213 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011214 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011215#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011217#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11218 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011219 {
11220 /* Server part of the key exchange */
11221 switch( ciphersuite->key_exchange )
11222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011223 case MBEDTLS_KEY_EXCHANGE_RSA:
11224 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011225 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011226 break;
11227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011228 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11229 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11230 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11231 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011232 break;
11233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011234 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11235 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011236 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011237 break;
11238
11239 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011240 case MBEDTLS_KEY_EXCHANGE_NONE:
11241 case MBEDTLS_KEY_EXCHANGE_PSK:
11242 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11243 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011244 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011245 usage = 0;
11246 }
11247 }
11248 else
11249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011250 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11251 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011252 }
11253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011254 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011255 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011256 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011257 ret = -1;
11258 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011259#else
11260 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011261#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011263#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11264 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011266 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11267 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011268 }
11269 else
11270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011271 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11272 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011273 }
11274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011275 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011276 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011277 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011278 ret = -1;
11279 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011280#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011281
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011282 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011283}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011284#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011285
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011286/*
11287 * Convert version numbers to/from wire format
11288 * and, for DTLS, to/from TLS equivalent.
11289 *
11290 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011291 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011292 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11293 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011295void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011296 unsigned char ver[2] )
11297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011298#if defined(MBEDTLS_SSL_PROTO_DTLS)
11299 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011301 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011302 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11303
11304 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11305 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11306 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011307 else
11308#else
11309 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011310#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011311 {
11312 ver[0] = (unsigned char) major;
11313 ver[1] = (unsigned char) minor;
11314 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011315}
11316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011317void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011318 const unsigned char ver[2] )
11319{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011320#if defined(MBEDTLS_SSL_PROTO_DTLS)
11321 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011322 {
11323 *major = 255 - ver[0] + 2;
11324 *minor = 255 - ver[1] + 1;
11325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011326 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011327 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11328 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011329 else
11330#else
11331 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011332#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011333 {
11334 *major = ver[0];
11335 *minor = ver[1];
11336 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011337}
11338
Simon Butcher99000142016-10-13 17:21:01 +010011339int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11340{
11341#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11342 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11343 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11344
11345 switch( md )
11346 {
11347#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11348#if defined(MBEDTLS_MD5_C)
11349 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011350 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011351#endif
11352#if defined(MBEDTLS_SHA1_C)
11353 case MBEDTLS_SSL_HASH_SHA1:
11354 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11355 break;
11356#endif
11357#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11358#if defined(MBEDTLS_SHA512_C)
11359 case MBEDTLS_SSL_HASH_SHA384:
11360 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11361 break;
11362#endif
11363#if defined(MBEDTLS_SHA256_C)
11364 case MBEDTLS_SSL_HASH_SHA256:
11365 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11366 break;
11367#endif
11368 default:
11369 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11370 }
11371
11372 return 0;
11373#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11374 (void) ssl;
11375 (void) md;
11376
11377 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11378#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11379}
11380
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011381#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11382 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11383int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11384 unsigned char *output,
11385 unsigned char *data, size_t data_len )
11386{
11387 int ret = 0;
11388 mbedtls_md5_context mbedtls_md5;
11389 mbedtls_sha1_context mbedtls_sha1;
11390
11391 mbedtls_md5_init( &mbedtls_md5 );
11392 mbedtls_sha1_init( &mbedtls_sha1 );
11393
11394 /*
11395 * digitally-signed struct {
11396 * opaque md5_hash[16];
11397 * opaque sha_hash[20];
11398 * };
11399 *
11400 * md5_hash
11401 * MD5(ClientHello.random + ServerHello.random
11402 * + ServerParams);
11403 * sha_hash
11404 * SHA(ClientHello.random + ServerHello.random
11405 * + ServerParams);
11406 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011407 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011408 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011410 goto exit;
11411 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011412 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011413 ssl->handshake->randbytes, 64 ) ) != 0 )
11414 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011416 goto exit;
11417 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011418 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011419 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011421 goto exit;
11422 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011423 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011424 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011426 goto exit;
11427 }
11428
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011429 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011430 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011432 goto exit;
11433 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011434 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011435 ssl->handshake->randbytes, 64 ) ) != 0 )
11436 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011438 goto exit;
11439 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011440 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011441 data_len ) ) != 0 )
11442 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011444 goto exit;
11445 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011446 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011447 output + 16 ) ) != 0 )
11448 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011449 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011450 goto exit;
11451 }
11452
11453exit:
11454 mbedtls_md5_free( &mbedtls_md5 );
11455 mbedtls_sha1_free( &mbedtls_sha1 );
11456
11457 if( ret != 0 )
11458 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11459 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11460
11461 return( ret );
11462
11463}
11464#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11465 MBEDTLS_SSL_PROTO_TLS1_1 */
11466
11467#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11468 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011469
11470#if defined(MBEDTLS_USE_PSA_CRYPTO)
11471int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11472 unsigned char *hash, size_t *hashlen,
11473 unsigned char *data, size_t data_len,
11474 mbedtls_md_type_t md_alg )
11475{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011476 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011477 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011478 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11479
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011480 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011481
11482 if( ( status = psa_hash_setup( &hash_operation,
11483 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011484 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011485 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011486 goto exit;
11487 }
11488
Andrzej Kurek814feff2019-01-14 04:35:19 -050011489 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11490 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011491 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011492 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011493 goto exit;
11494 }
11495
Andrzej Kurek814feff2019-01-14 04:35:19 -050011496 if( ( status = psa_hash_update( &hash_operation,
11497 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011498 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011499 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011500 goto exit;
11501 }
11502
Andrzej Kurek814feff2019-01-14 04:35:19 -050011503 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11504 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011505 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011506 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011507 goto exit;
11508 }
11509
11510exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011511 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011512 {
11513 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11514 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011515 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011516 {
11517 case PSA_ERROR_NOT_SUPPORTED:
11518 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011519 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011520 case PSA_ERROR_BUFFER_TOO_SMALL:
11521 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11522 case PSA_ERROR_INSUFFICIENT_MEMORY:
11523 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11524 default:
11525 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11526 }
11527 }
11528 return( 0 );
11529}
11530
11531#else
11532
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011533int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011534 unsigned char *hash, size_t *hashlen,
11535 unsigned char *data, size_t data_len,
11536 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011537{
11538 int ret = 0;
11539 mbedtls_md_context_t ctx;
11540 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011541 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011542
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011543 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011544
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011545 mbedtls_md_init( &ctx );
11546
11547 /*
11548 * digitally-signed struct {
11549 * opaque client_random[32];
11550 * opaque server_random[32];
11551 * ServerDHParams params;
11552 * };
11553 */
11554 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11555 {
11556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11557 goto exit;
11558 }
11559 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11560 {
11561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11562 goto exit;
11563 }
11564 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11565 {
11566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11567 goto exit;
11568 }
11569 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11570 {
11571 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11572 goto exit;
11573 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011574 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011575 {
11576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11577 goto exit;
11578 }
11579
11580exit:
11581 mbedtls_md_free( &ctx );
11582
11583 if( ret != 0 )
11584 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11585 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11586
11587 return( ret );
11588}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011589#endif /* MBEDTLS_USE_PSA_CRYPTO */
11590
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011591#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11592 MBEDTLS_SSL_PROTO_TLS1_2 */
11593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011594#endif /* MBEDTLS_SSL_TLS_C */