blob: 6563d9ade49df87988842599428831177712d7e3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Hanno Becker2a43f6f2018-08-10 11:12:52 +010062static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010063static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010064
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010070 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010071#else
72 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010073#endif
74 return( 0 );
75}
76
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077/*
78 * Start a timer.
79 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020083 if( ssl->f_set_timer == NULL )
84 return;
85
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
87 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088}
89
90/*
91 * Return -1 is timer is expired, 0 if it isn't.
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020094{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020095 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020096 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020097
98 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020099 {
100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200102 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103
104 return( 0 );
105}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200106
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100107static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
108 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100109static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100110
Hanno Beckercfe45792019-07-03 16:13:00 +0100111#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +0100112static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
113 unsigned char *buf,
114 size_t len,
115 mbedtls_record *rec );
116
Hanno Beckercfe45792019-07-03 16:13:00 +0100117int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
118 unsigned char *buf,
119 size_t buflen )
120{
Hanno Becker54229812019-07-12 14:40:00 +0100121 int ret = 0;
122 mbedtls_record rec;
123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
124 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
125
126 /* We don't support record checking in TLS because
127 * (a) there doesn't seem to be a usecase for it, and
128 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
129 * and we'd need to backup the transform here.
130 */
131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
132 {
133 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
134 goto exit;
135 }
136#if defined(MBEDTLS_SSL_PROTO_DTLS)
137 else
138 {
139 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
140 if( ret != 0 )
141 {
142 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
143 goto exit;
144 }
145
146 if( ssl->transform_in != NULL )
147 {
148 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
149 if( ret != 0 )
150 {
151 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
152 goto exit;
153 }
154 }
155 }
156#endif /* MBEDTLS_SSL_PROTO_DTLS */
157
158exit:
159 /* On success, we have decrypted the buffer in-place, so make
160 * sure we don't leak any plaintext data. */
161 mbedtls_platform_zeroize( buf, buflen );
162
163 /* For the purpose of this API, treat messages with unexpected CID
164 * as well as such from future epochs as unexpected. */
165 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
166 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
167 {
168 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
169 }
170
171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
172 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100173}
174#endif /* MBEDTLS_SSL_RECORD_CHECKING */
175
Hanno Becker67bc7c32018-08-06 11:33:50 +0100176#define SSL_DONT_FORCE_FLUSH 0
177#define SSL_FORCE_FLUSH 1
178
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200179#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100180
Hanno Beckera0e20d02019-05-15 14:03:01 +0100181#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100182/* Top-level Connection ID API */
183
Hanno Becker8367ccc2019-05-14 11:30:10 +0100184int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
185 size_t len,
186 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100187{
188 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
190
Hanno Becker611ac772019-05-14 11:45:26 +0100191 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
192 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
193 {
194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
195 }
196
197 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100198 conf->cid_len = len;
199 return( 0 );
200}
201
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100202int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
203 int enable,
204 unsigned char const *own_cid,
205 size_t own_cid_len )
206{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100207 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
209
Hanno Beckerca092242019-04-25 16:01:49 +0100210 ssl->negotiate_cid = enable;
211 if( enable == MBEDTLS_SSL_CID_DISABLED )
212 {
213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
214 return( 0 );
215 }
216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100217 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100218
Hanno Beckerad4a1372019-05-03 13:06:44 +0100219 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100220 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
222 (unsigned) own_cid_len,
223 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
225 }
226
227 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100228 /* Truncation is not an issue here because
229 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
230 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100231
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100232 return( 0 );
233}
234
235int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
236 int *enabled,
237 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
238 size_t *peer_cid_len )
239{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100240 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100241
Hanno Becker76a79ab2019-05-03 14:38:32 +0100242 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
243 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
244 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100246 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100247
Hanno Beckerc5f24222019-05-03 12:54:52 +0100248 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
249 * were used, but client and server requested the empty CID.
250 * This is indistinguishable from not using the CID extension
251 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100252 if( ssl->transform_in->in_cid_len == 0 &&
253 ssl->transform_in->out_cid_len == 0 )
254 {
255 return( 0 );
256 }
257
Hanno Becker615ef172019-05-22 16:50:35 +0100258 if( peer_cid_len != NULL )
259 {
260 *peer_cid_len = ssl->transform_in->out_cid_len;
261 if( peer_cid != NULL )
262 {
263 memcpy( peer_cid, ssl->transform_in->out_cid,
264 ssl->transform_in->out_cid_len );
265 }
266 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100267
268 *enabled = MBEDTLS_SSL_CID_ENABLED;
269
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100270 return( 0 );
271}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100272#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100273
Hanno Beckerd5847772018-08-28 10:09:23 +0100274/* Forward declarations for functions related to message buffering. */
275static void ssl_buffering_free( mbedtls_ssl_context *ssl );
276static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
277 uint8_t slot );
278static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
279static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
280static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
281static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100282static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
283 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100284static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100285
Hanno Beckera67dee22018-08-22 10:05:20 +0100286static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100287static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100288{
Hanno Becker11682cc2018-08-22 14:41:02 +0100289 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100290
291 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100292 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100293
294 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
295}
296
Hanno Becker67bc7c32018-08-06 11:33:50 +0100297static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
298{
Hanno Becker11682cc2018-08-22 14:41:02 +0100299 size_t const bytes_written = ssl->out_left;
300 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100301
302 /* Double-check that the write-index hasn't gone
303 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100304 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100305 {
306 /* Should never happen... */
307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
308 }
309
310 return( (int) ( mtu - bytes_written ) );
311}
312
313static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
314{
315 int ret;
316 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400317 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100318
319#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
320 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
321
322 if( max_len > mfl )
323 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100324
325 /* By the standard (RFC 6066 Sect. 4), the MFL extension
326 * only limits the maximum record payload size, so in theory
327 * we would be allowed to pack multiple records of payload size
328 * MFL into a single datagram. However, this would mean that there's
329 * no way to explicitly communicate MTU restrictions to the peer.
330 *
331 * The following reduction of max_len makes sure that we never
332 * write datagrams larger than MFL + Record Expansion Overhead.
333 */
334 if( max_len <= ssl->out_left )
335 return( 0 );
336
337 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100338#endif
339
340 ret = ssl_get_remaining_space_in_datagram( ssl );
341 if( ret < 0 )
342 return( ret );
343 remaining = (size_t) ret;
344
345 ret = mbedtls_ssl_get_record_expansion( ssl );
346 if( ret < 0 )
347 return( ret );
348 expansion = (size_t) ret;
349
350 if( remaining <= expansion )
351 return( 0 );
352
353 remaining -= expansion;
354 if( remaining >= max_len )
355 remaining = max_len;
356
357 return( (int) remaining );
358}
359
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200360/*
361 * Double the retransmit timeout value, within the allowed range,
362 * returning -1 if the maximum value has already been reached.
363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200365{
366 uint32_t new_timeout;
367
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200368 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200369 return( -1 );
370
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200371 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
372 * in the following way: after the initial transmission and a first
373 * retransmission, back off to a temporary estimated MTU of 508 bytes.
374 * This value is guaranteed to be deliverable (if not guaranteed to be
375 * delivered) of any compliant IPv4 (and IPv6) network, and should work
376 * on most non-IP stacks too. */
377 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400378 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200379 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
381 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200382
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200383 new_timeout = 2 * ssl->handshake->retransmit_timeout;
384
385 /* Avoid arithmetic overflow and range overflow */
386 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200387 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200388 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200389 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200390 }
391
392 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200394 ssl->handshake->retransmit_timeout ) );
395
396 return( 0 );
397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200400{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200401 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200403 ssl->handshake->retransmit_timeout ) );
404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200408/*
409 * Convert max_fragment_length codes to length.
410 * RFC 6066 says:
411 * enum{
412 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
413 * } MaxFragmentLength;
414 * and we add 0 -> extension unused
415 */
Angus Grattond8213d02016-05-25 20:56:48 +1000416static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200417{
Angus Grattond8213d02016-05-25 20:56:48 +1000418 switch( mfl )
419 {
420 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
421 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
422 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
423 return 512;
424 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
425 return 1024;
426 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
427 return 2048;
428 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
429 return 4096;
430 default:
431 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
432 }
433}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200435
Hanno Becker52055ae2019-02-06 14:30:46 +0000436int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
437 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200438{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ssl_session_free( dst );
440 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000443
444#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200445 if( src->peer_cert != NULL )
446 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200447 int ret;
448
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200449 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200450 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200451 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200456 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200459 dst->peer_cert = NULL;
460 return( ret );
461 }
462 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000463#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000464 if( src->peer_cert_digest != NULL )
465 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000466 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000467 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000468 if( dst->peer_cert_digest == NULL )
469 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
470
471 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
472 src->peer_cert_digest_len );
473 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000474 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000475 }
476#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200479
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200480#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200481 if( src->ticket != NULL )
482 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200483 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200484 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200485 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200486
487 memcpy( dst->ticket, src->ticket, src->ticket_len );
488 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200489#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200490
491 return( 0 );
492}
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
495int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200496 const unsigned char *key_enc, const unsigned char *key_dec,
497 size_t keylen,
498 const unsigned char *iv_enc, const unsigned char *iv_dec,
499 size_t ivlen,
500 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200501 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
503int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
504int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
505int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
506int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
507#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000508
Paul Bakker5121ce52009-01-03 21:22:43 +0000509/*
510 * Key material generation
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200513static int ssl3_prf( const unsigned char *secret, size_t slen,
514 const char *label,
515 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000516 unsigned char *dstbuf, size_t dlen )
517{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100518 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000519 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200520 mbedtls_md5_context md5;
521 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000522 unsigned char padding[16];
523 unsigned char sha1sum[20];
524 ((void)label);
525
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200526 mbedtls_md5_init( &md5 );
527 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200528
Paul Bakker5f70b252012-09-13 14:23:06 +0000529 /*
530 * SSLv3:
531 * block =
532 * MD5( secret + SHA1( 'A' + secret + random ) ) +
533 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
534 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
535 * ...
536 */
537 for( i = 0; i < dlen / 16; i++ )
538 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200539 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000540
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100541 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100542 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100543 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100544 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100545 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100546 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100547 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100548 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100549 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100550 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000551
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100552 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100553 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100554 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100555 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100556 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100557 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100558 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100559 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000560 }
561
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100562exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200563 mbedtls_md5_free( &md5 );
564 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000565
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500566 mbedtls_platform_zeroize( padding, sizeof( padding ) );
567 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000568
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100569 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200574static int tls1_prf( const unsigned char *secret, size_t slen,
575 const char *label,
576 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000577 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578{
Paul Bakker23986e52011-04-24 08:57:21 +0000579 size_t nb, hs;
580 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200581 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300582 unsigned char *tmp;
583 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 const mbedtls_md_info_t *md_info;
586 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100587 int ret;
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Ron Eldor3b350852019-05-07 18:31:49 +0300591 tmp_len = 20 + strlen( label ) + rlen;
592 tmp = mbedtls_calloc( 1, tmp_len );
593 if( tmp == NULL )
594 {
595 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
596 goto exit;
597 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 hs = ( slen + 1 ) / 2;
600 S1 = secret;
601 S2 = secret + slen - hs;
602
603 nb = strlen( label );
604 memcpy( tmp + 20, label, nb );
605 memcpy( tmp + 20 + nb, random, rlen );
606 nb += rlen;
607
608 /*
609 * First compute P_md5(secret,label+random)[0..dlen]
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300612 {
613 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
614 goto exit;
615 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300618 {
619 goto exit;
620 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
623 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
624 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626 for( i = 0; i < dlen; i += 16 )
627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_md_hmac_reset ( &md_ctx );
629 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
630 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
634 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
637
638 for( j = 0; j < k; j++ )
639 dstbuf[i + j] = h_i[j];
640 }
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100643
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 /*
645 * XOR out with P_sha1(secret,label+random)[0..dlen]
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300648 {
649 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
650 goto exit;
651 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300654 {
655 goto exit;
656 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
659 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
660 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
662 for( i = 0; i < dlen; i += 20 )
663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_md_hmac_reset ( &md_ctx );
665 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
666 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_hmac_reset ( &md_ctx );
669 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
670 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
673
674 for( j = 0; j < k; j++ )
675 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
676 }
677
Ron Eldor3b350852019-05-07 18:31:49 +0300678exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100680
Ron Eldor3b350852019-05-07 18:31:49 +0300681 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500682 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Ron Eldor3b350852019-05-07 18:31:49 +0300684 mbedtls_free( tmp );
685 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500690#if defined(MBEDTLS_USE_PSA_CRYPTO)
691static int tls_prf_generic( mbedtls_md_type_t md_type,
692 const unsigned char *secret, size_t slen,
693 const char *label,
694 const unsigned char *random, size_t rlen,
695 unsigned char *dstbuf, size_t dlen )
696{
697 psa_status_t status;
698 psa_algorithm_t alg;
Janos Follath53b8ec22019-08-08 10:28:27 +0100699 psa_key_attributes_t key_attributes;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500700 psa_key_handle_t master_slot;
Janos Follathda6ac012019-08-16 13:47:29 +0100701 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100702 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500703
Andrzej Kurekc929a822019-01-14 03:51:11 -0500704 if( md_type == MBEDTLS_MD_SHA384 )
705 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
706 else
707 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
708
Janos Follath53b8ec22019-08-08 10:28:27 +0100709 key_attributes = psa_key_attributes_init();
710 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
711 psa_set_key_algorithm( &key_attributes, alg );
712 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500713
Janos Follath53b8ec22019-08-08 10:28:27 +0100714 status = psa_import_key( &key_attributes, secret, slen, &master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500715 if( status != PSA_SUCCESS )
716 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
717
Janos Follathda6ac012019-08-16 13:47:29 +0100718 status = psa_key_derivation( &derivation,
Andrzej Kurekc929a822019-01-14 03:51:11 -0500719 master_slot, alg,
720 random, rlen,
721 (unsigned char const *) label,
722 (size_t) strlen( label ),
723 dlen );
724 if( status != PSA_SUCCESS )
725 {
Janos Follathda6ac012019-08-16 13:47:29 +0100726 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500727 psa_destroy_key( master_slot );
728 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
729 }
730
Janos Follathda6ac012019-08-16 13:47:29 +0100731 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500732 if( status != PSA_SUCCESS )
733 {
Janos Follathda6ac012019-08-16 13:47:29 +0100734 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500735 psa_destroy_key( master_slot );
736 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
737 }
738
Janos Follathda6ac012019-08-16 13:47:29 +0100739 status = psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500740 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500741 {
742 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500743 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500744 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500745
746 status = psa_destroy_key( master_slot );
747 if( status != PSA_SUCCESS )
748 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
749
Andrzej Kurek33171262019-01-15 03:25:18 -0500750 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500751}
752
753#else /* MBEDTLS_USE_PSA_CRYPTO */
754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100756 const unsigned char *secret, size_t slen,
757 const char *label,
758 const unsigned char *random, size_t rlen,
759 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000760{
761 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100762 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300763 unsigned char *tmp;
764 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
Ron Eldor3b350852019-05-07 18:31:49 +0300777 tmp_len = md_len + strlen( label ) + rlen;
778 tmp = mbedtls_calloc( 1, tmp_len );
779 if( tmp == NULL )
780 {
781 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
782 goto exit;
783 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000784
785 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100786 memcpy( tmp + md_len, label, nb );
787 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000788 nb += rlen;
789
790 /*
791 * Compute P_<hash>(secret, label + random)[0..dlen]
792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300794 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
797 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
798 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100799
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100800 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_md_hmac_reset ( &md_ctx );
803 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
804 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_hmac_reset ( &md_ctx );
807 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
808 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000809
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100810 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000811
812 for( j = 0; j < k; j++ )
813 dstbuf[i + j] = h_i[j];
814 }
815
Ron Eldor3b350852019-05-07 18:31:49 +0300816exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100818
Ron Eldor3b350852019-05-07 18:31:49 +0300819 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500820 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000821
Ron Eldor3b350852019-05-07 18:31:49 +0300822 mbedtls_free( tmp );
823
824 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000825}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500826#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100828static int tls_prf_sha256( const unsigned char *secret, size_t slen,
829 const char *label,
830 const unsigned char *random, size_t rlen,
831 unsigned char *dstbuf, size_t dlen )
832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100834 label, random, rlen, dstbuf, dlen ) );
835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200839static int tls_prf_sha384( const unsigned char *secret, size_t slen,
840 const char *label,
841 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000842 unsigned char *dstbuf, size_t dlen )
843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100845 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_SHA512_C */
848#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
853 defined(MBEDTLS_SSL_PROTO_TLS1_1)
854static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#endif
Paul Bakker380da532012-04-18 16:10:25 +0000856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200858static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200860#endif
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200863static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200865#endif
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
868#if defined(MBEDTLS_SHA256_C)
869static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200870static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_SHA512_C)
875static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200876static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000880
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100881#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100882 defined(MBEDTLS_USE_PSA_CRYPTO)
883static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
884{
885 if( ssl->conf->f_psk != NULL )
886 {
887 /* If we've used a callback to select the PSK,
888 * the static configuration is irrelevant. */
889 if( ssl->handshake->psk_opaque != 0 )
890 return( 1 );
891
892 return( 0 );
893 }
894
895 if( ssl->conf->psk_opaque != 0 )
896 return( 1 );
897
898 return( 0 );
899}
900#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100901 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100902
Ron Eldorcf280092019-05-14 20:19:13 +0300903#if defined(MBEDTLS_SSL_EXPORT_KEYS)
904static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
905{
906#if defined(MBEDTLS_SSL_PROTO_SSL3)
907 if( tls_prf == ssl3_prf )
908 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300909 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300910 }
911 else
912#endif
913#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
914 if( tls_prf == tls1_prf )
915 {
916 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
917 }
918 else
919#endif
920#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
921#if defined(MBEDTLS_SHA512_C)
922 if( tls_prf == tls_prf_sha384 )
923 {
924 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
925 }
926 else
927#endif
928#if defined(MBEDTLS_SHA256_C)
929 if( tls_prf == tls_prf_sha256 )
930 {
931 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
932 }
933 else
934#endif
935#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
936 return( MBEDTLS_SSL_TLS_PRF_NONE );
937}
938#endif /* MBEDTLS_SSL_EXPORT_KEYS */
939
Ron Eldor51d3ab52019-05-12 14:54:30 +0300940int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
941 const unsigned char *secret, size_t slen,
942 const char *label,
943 const unsigned char *random, size_t rlen,
944 unsigned char *dstbuf, size_t dlen )
945{
946 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
947
948 switch( prf )
949 {
950#if defined(MBEDTLS_SSL_PROTO_SSL3)
951 case MBEDTLS_SSL_TLS_PRF_SSL3:
952 tls_prf = ssl3_prf;
953 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300954#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300955#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
956 case MBEDTLS_SSL_TLS_PRF_TLS1:
957 tls_prf = tls1_prf;
958 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300959#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
960
961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300962#if defined(MBEDTLS_SHA512_C)
963 case MBEDTLS_SSL_TLS_PRF_SHA384:
964 tls_prf = tls_prf_sha384;
965 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300966#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300967#if defined(MBEDTLS_SHA256_C)
968 case MBEDTLS_SSL_TLS_PRF_SHA256:
969 tls_prf = tls_prf_sha256;
970 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300971#endif /* MBEDTLS_SHA256_C */
972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300973 default:
974 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
975 }
976
977 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
978}
979
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200980/*
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200981 * Populate a transform structure with session keys and all the other
982 * necessary information.
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200983 *
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200984 * Parameters:
985 * - [in/out]: transform: structure to populate
986 * [in] must be just initialised with mbedtls_ssl_transform_init()
987 * [out] fully populate, ready for use by mbedtls_ssl_{en,de}crypt_buf()
988 * - [in] session: used members: encrypt_then_max, master, compression
989 * - [in] handshake: used members: prf, ciphersuite_info, randbytes
990 * - [in]: ssl: used members: minor_ver, conf->endpoint
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200991 */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200992static int ssl_populate_transform( mbedtls_ssl_transform *transform,
993 const mbedtls_ssl_session *session,
994 const mbedtls_ssl_handshake_params *handshake,
995 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000996{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200997 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000998#if defined(MBEDTLS_USE_PSA_CRYPTO)
999 int psa_fallthrough;
1000#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001001 unsigned char keyblk[256];
1002 unsigned char *key1;
1003 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001004 unsigned char *mac_enc;
1005 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001006 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001007 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001008 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001009 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 const mbedtls_cipher_info_t *cipher_info;
1011 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001012
Jaeden Amero2de07f12019-06-05 13:32:08 +01001013#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1014 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001015 transform->encrypt_then_mac = session->encrypt_then_mac;
1016#endif
1017 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001018
1019 ciphersuite_info = handshake->ciphersuite_info;
1020 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001021 if( cipher_info == NULL )
1022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001024 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001026 }
1027
Hanno Beckere694c3e2017-12-27 21:34:08 +00001028 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001029 if( md_info == NULL )
1030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001032 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001034 }
1035
Hanno Beckera0e20d02019-05-15 14:03:01 +01001036#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001037 /* Copy own and peer's CID if the use of the CID
1038 * extension has been negotiated. */
1039 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1040 {
1041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001042
Hanno Becker05154c32019-05-03 15:23:51 +01001043 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001044 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001045 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001046 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001047
1048 transform->out_cid_len = ssl->handshake->peer_cid_len;
1049 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1050 ssl->handshake->peer_cid_len );
1051 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1052 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001053 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001054#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001055
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 * SSLv3:
1058 * key block =
1059 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1060 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1061 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1062 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1063 * ...
1064 *
1065 * TLSv1:
1066 * key block = PRF( master, "key expansion", randbytes )
1067 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001068 ret = handshake->tls_prf( session->master, 48, "key expansion",
1069 handshake->randbytes, 64, keyblk, 256 );
1070 if( ret != 0 )
1071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001073 return( ret );
1074 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1077 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1078 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1079 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1080 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 /*
1083 * Determine the appropriate key, IV and MAC length.
1084 */
Paul Bakker68884e32013-01-07 18:20:04 +01001085
Hanno Becker88aaf652017-12-27 08:17:40 +00001086 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001087
Hanno Becker8031d062018-01-03 15:32:31 +00001088#if defined(MBEDTLS_GCM_C) || \
1089 defined(MBEDTLS_CCM_C) || \
1090 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001092 cipher_info->mode == MBEDTLS_MODE_CCM ||
1093 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001094 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001095 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001096
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001097 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001098 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001099 transform->taglen =
1100 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001101
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001102 /* All modes haves 96-bit IVs;
1103 * GCM and CCM has 4 implicit and 8 explicit bytes
1104 * ChachaPoly has all 12 bytes implicit
1105 */
Paul Bakker68884e32013-01-07 18:20:04 +01001106 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001107 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1108 transform->fixed_ivlen = 12;
1109 else
1110 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001111
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001112 /* Minimum length of encrypted record */
1113 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001114 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001115 }
1116 else
Hanno Becker8031d062018-01-03 15:32:31 +00001117#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1118#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1119 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1120 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001121 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001122 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1124 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001127 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001128 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001129
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001130 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001131 mac_key_len = mbedtls_md_get_size( md_info );
1132 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001135 /*
1136 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1137 * (rfc 6066 page 13 or rfc 2104 section 4),
1138 * so we only need to adjust the length here.
1139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001143
1144#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1145 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001146 * HMAC implementation which also truncates the key
1147 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001148 mac_key_len = transform->maclen;
1149#endif
1150 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001152
1153 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001154 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001156 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001158 transform->minlen = transform->maclen;
1159 else
Paul Bakker68884e32013-01-07 18:20:04 +01001160 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001161 /*
1162 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001163 * 1. if EtM is in use: one block plus MAC
1164 * otherwise: * first multiple of blocklen greater than maclen
1165 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1168 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001169 {
1170 transform->minlen = transform->maclen
1171 + cipher_info->block_size;
1172 }
1173 else
1174#endif
1175 {
1176 transform->minlen = transform->maclen
1177 + cipher_info->block_size
1178 - transform->maclen % cipher_info->block_size;
1179 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1182 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1183 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001184 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001185 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1188 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1189 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001190 {
1191 transform->minlen += transform->ivlen;
1192 }
1193 else
1194#endif
1195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001197 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1198 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001199 }
Paul Bakker68884e32013-01-07 18:20:04 +01001200 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 }
Hanno Becker8031d062018-01-03 15:32:31 +00001202 else
1203#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1204 {
1205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001208
Hanno Becker88aaf652017-12-27 08:17:40 +00001209 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1210 (unsigned) keylen,
1211 (unsigned) transform->minlen,
1212 (unsigned) transform->ivlen,
1213 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
1215 /*
1216 * Finally setup the cipher contexts, IVs and MAC secrets.
1217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001219 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001221 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001222 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001223
Paul Bakker68884e32013-01-07 18:20:04 +01001224 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001225 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001226
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001227 /*
1228 * This is not used in TLS v1.1.
1229 */
Paul Bakker48916f92012-09-16 19:57:18 +00001230 iv_copy_len = ( transform->fixed_ivlen ) ?
1231 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001232 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1233 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001234 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 }
1236 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#endif /* MBEDTLS_SSL_CLI_C */
1238#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001239 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001241 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001242 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Hanno Becker81c7b182017-11-09 18:39:33 +00001244 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001245 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001247 /*
1248 * This is not used in TLS v1.1.
1249 */
Paul Bakker48916f92012-09-16 19:57:18 +00001250 iv_copy_len = ( transform->fixed_ivlen ) ?
1251 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001252 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1253 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001254 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001256 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001260 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1261 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001262 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Hanno Beckerd56ed242018-01-03 15:32:51 +00001264#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_SSL_PROTO_SSL3)
1266 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001267 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001268 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001271 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1272 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001273 }
1274
Hanno Becker81c7b182017-11-09 18:39:33 +00001275 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1276 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001277 }
1278 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1280#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1281 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1282 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001283 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001284 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1285 For AEAD-based ciphersuites, there is nothing to do here. */
1286 if( mac_key_len != 0 )
1287 {
1288 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1289 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1290 }
Paul Bakker68884e32013-01-07 18:20:04 +01001291 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001292 else
1293#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001296 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1297 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001298 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001299#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1302 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001303 {
1304 int ret = 0;
1305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001307
Hanno Becker88aaf652017-12-27 08:17:40 +00001308 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001309 transform->iv_enc, transform->iv_dec,
1310 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001311 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001312 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001315 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1316 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001317 }
1318 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001319#else
1320 ((void) mac_dec);
1321 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001323
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001324#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1325 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001326 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001327 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1328 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001329 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001330 iv_copy_len );
1331 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001332
1333 if( ssl->conf->f_export_keys_ext != NULL )
1334 {
1335 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1336 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001337 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001338 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001339 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001340 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001341 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001342 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001343#endif
1344
Hanno Beckerf704bef2018-11-16 15:21:18 +00001345#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001346
1347 /* Only use PSA-based ciphers for TLS-1.2.
1348 * That's relevant at least for TLS-1.0, where
1349 * we assume that mbedtls_cipher_crypt() updates
1350 * the structure field for the IV, which the PSA-based
1351 * implementation currently doesn't. */
1352#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1353 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001354 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001355 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001356 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001357 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1358 {
1359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001360 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001361 }
1362
1363 if( ret == 0 )
1364 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001365 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001366 psa_fallthrough = 0;
1367 }
1368 else
1369 {
1370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1371 psa_fallthrough = 1;
1372 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001373 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001374 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001375 psa_fallthrough = 1;
1376#else
1377 psa_fallthrough = 1;
1378#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001379
Hanno Beckercb1cc802018-11-17 22:27:38 +00001380 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001381#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001382 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001383 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001384 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001385 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001386 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001387 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001388
Hanno Beckerf704bef2018-11-16 15:21:18 +00001389#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001390 /* Only use PSA-based ciphers for TLS-1.2.
1391 * That's relevant at least for TLS-1.0, where
1392 * we assume that mbedtls_cipher_crypt() updates
1393 * the structure field for the IV, which the PSA-based
1394 * implementation currently doesn't. */
1395#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1396 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001397 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001398 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001399 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001400 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1401 {
1402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001403 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001404 }
1405
1406 if( ret == 0 )
1407 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001408 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001409 psa_fallthrough = 0;
1410 }
1411 else
1412 {
1413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1414 psa_fallthrough = 1;
1415 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001416 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001417 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001418 psa_fallthrough = 1;
1419#else
1420 psa_fallthrough = 1;
1421#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001422
Hanno Beckercb1cc802018-11-17 22:27:38 +00001423 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001424#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001425 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001426 cipher_info ) ) != 0 )
1427 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001429 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001430 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001433 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001437 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001438 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001441 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001445 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001446 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_CIPHER_MODE_CBC)
1449 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1452 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001455 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001456 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1459 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001462 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001463 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001466
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001468 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001473
Paul Bakker48916f92012-09-16 19:57:18 +00001474 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1475 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001476
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001477 if( deflateInit( &transform->ctx_deflate,
1478 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001479 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001482 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1483 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001484 }
1485 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001487
Ron Eldore6992702019-05-07 18:27:13 +03001488end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001489 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Ron Eldore6992702019-05-07 18:27:13 +03001490 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001491}
1492
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001493/*
1494 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
1495 *
1496 * Inputs:
1497 * - SSL/TLS minor version
1498 * - hash associated with the ciphersuite (only used by TLS 1.2)
1499 *
1500 * Ouputs:
1501 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1502 */
1503static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1504 int minor_ver,
1505 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001506{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001507#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1508 (void) hash;
1509#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001510
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001511#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001512 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001513 {
1514 handshake->tls_prf = ssl3_prf;
1515 handshake->calc_verify = ssl_calc_verify_ssl;
1516 handshake->calc_finished = ssl_calc_finished_ssl;
1517 }
1518 else
1519#endif
1520#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001521 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001522 {
1523 handshake->tls_prf = tls1_prf;
1524 handshake->calc_verify = ssl_calc_verify_tls;
1525 handshake->calc_finished = ssl_calc_finished_tls;
1526 }
1527 else
1528#endif
1529#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1530#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001531 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1532 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001533 {
1534 handshake->tls_prf = tls_prf_sha384;
1535 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1536 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1537 }
1538 else
1539#endif
1540#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001541 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001542 {
1543 handshake->tls_prf = tls_prf_sha256;
1544 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1545 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1546 }
1547 else
1548#endif
1549#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1550 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1552 }
1553
1554 return( 0 );
1555}
1556
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001557/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001558 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001559 *
1560 * Parameters:
1561 * [in/out] handshake
1562 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001563 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001564 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001565 * [out] master
1566 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1567 * debug: conf->f_dbg, conf->p_dbg
1568 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001569 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001570 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001571static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001572 unsigned char *master,
Manuel Pégourié-Gonnard0d56aaa2019-05-03 09:58:33 +02001573 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001574{
1575 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001576
1577 /* cf. RFC 5246, Section 8.1:
1578 * "The master secret is always exactly 48 bytes in length." */
1579 size_t const master_secret_len = 48;
1580
1581#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1582 unsigned char session_hash[48];
1583#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1584
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001585 /* The label for the KDF used for key expansion.
1586 * This is either "master secret" or "extended master secret"
1587 * depending on whether the Extended Master Secret extension
1588 * is used. */
1589 char const *lbl = "master secret";
1590
1591 /* The salt for the KDF used for key expansion.
1592 * - If the Extended Master Secret extension is not used,
1593 * this is ClientHello.Random + ServerHello.Random
1594 * (see Sect. 8.1 in RFC 5246).
1595 * - If the Extended Master Secret extension is used,
1596 * this is the transcript of the handshake so far.
1597 * (see Sect. 4 in RFC 7627). */
1598 unsigned char const *salt = handshake->randbytes;
1599 size_t salt_len = 64;
1600
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001601#if !defined(MBEDTLS_DEBUG_C) && \
1602 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1603 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1604 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
1605 (void) ssl;
1606#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001607
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001608 if( handshake->resume != 0 )
1609 {
1610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001611 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001612 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001613
1614#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001615 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001616 {
1617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001618
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001619 lbl = "extended master secret";
1620 salt = session_hash;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001621 handshake->calc_verify( ssl, session_hash, &salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001622
1623 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
1624 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001625#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1626
1627#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001628 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1629 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001630 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001631 ssl_use_opaque_psk( ssl ) == 1 )
1632 {
1633 /* Perform PSK-to-MS expansion in a single step. */
1634 psa_status_t status;
1635 psa_algorithm_t alg;
1636 psa_key_handle_t psk;
1637 psa_key_derivation_operation_t derivation =
1638 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001639 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001640
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001642
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001643 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001644 if( handshake->psk_opaque != 0 )
1645 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001646
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001647 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001648 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001649 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001650 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1651
1652 status = psa_key_derivation( &derivation, psk, alg,
1653 salt, salt_len,
1654 (unsigned char const *) lbl,
1655 (size_t) strlen( lbl ),
1656 master_secret_len );
1657 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001658 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001659 psa_key_derivation_abort( &derivation );
1660 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001661 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001662
1663 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001664 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001665 master_secret_len );
1666 if( status != PSA_SUCCESS )
1667 {
1668 psa_key_derivation_abort( &derivation );
1669 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1670 }
1671
1672 status = psa_key_derivation_abort( &derivation );
1673 if( status != PSA_SUCCESS )
1674 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1675 }
1676 else
1677#endif
1678 {
1679 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1680 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001681 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001682 master_secret_len );
1683 if( ret != 0 )
1684 {
1685 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1686 return( ret );
1687 }
1688
1689 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1690 handshake->premaster,
1691 handshake->pmslen );
1692
1693 mbedtls_platform_zeroize( handshake->premaster,
1694 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001695 }
1696
1697 return( 0 );
1698}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001699
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001700int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1701{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001702 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001703 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1704 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001705
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1707
1708 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001709 ret = ssl_set_handshake_prfs( ssl->handshake,
1710 ssl->minor_ver,
1711 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001712 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001713 {
1714 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001715 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001716 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001717
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001718 /* Compute master secret if needed */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001719 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001720 ssl->session_negotiate->master,
1721 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001722 if( ret != 0 )
1723 {
1724 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1725 return( ret );
1726 }
1727
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001728 /* Swap the client and server random values:
1729 * - MS derivation wanted client+server (RFC 5246 8.1)
1730 * - key derivation wants server+client (RFC 5246 6.3) */
1731 {
1732 unsigned char tmp[64];
1733 memcpy( tmp, ssl->handshake->randbytes, 64 );
1734 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1735 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1736 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1737 }
1738
1739 /* Populate transform structure */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001740 ret = ssl_populate_transform( ssl->transform_negotiate,
1741 ssl->session_negotiate,
1742 ssl->handshake,
1743 ssl );
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001744 if( ret != 0 )
1745 {
1746 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1747 return( ret );
1748 }
1749
1750 /* We no longer need Server/ClientHello.random values */
1751 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1752 sizeof( ssl->handshake->randbytes ) );
1753
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001754 /* Allocate compression buffer */
1755#if defined(MBEDTLS_ZLIB_SUPPORT)
1756 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1757 ssl->compress_buf == NULL )
1758 {
1759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1760 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1761 if( ssl->compress_buf == NULL )
1762 {
1763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
1764 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
1765 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1766 }
1767 }
1768#endif
1769
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1771
1772 return( 0 );
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001773}
1774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001776void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1777 unsigned char hash[36],
1778 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001779{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001780 mbedtls_md5_context md5;
1781 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 unsigned char pad_1[48];
1783 unsigned char pad_2[48];
1784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001787 mbedtls_md5_init( &md5 );
1788 mbedtls_sha1_init( &sha1 );
1789
1790 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1791 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Paul Bakker380da532012-04-18 16:10:25 +00001793 memset( pad_1, 0x36, 48 );
1794 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001796 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1797 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1798 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001799
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001800 mbedtls_md5_starts_ret( &md5 );
1801 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1802 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1803 mbedtls_md5_update_ret( &md5, hash, 16 );
1804 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001805
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001806 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1807 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1808 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001809
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001810 mbedtls_sha1_starts_ret( &sha1 );
1811 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1812 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1813 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1814 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001815
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001816 *hlen = 36;
1817
1818 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001820
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001821 mbedtls_md5_free( &md5 );
1822 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001823
Paul Bakker380da532012-04-18 16:10:25 +00001824 return;
1825}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001829void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1830 unsigned char hash[36],
1831 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001832{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001833 mbedtls_md5_context md5;
1834 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001837
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001838 mbedtls_md5_init( &md5 );
1839 mbedtls_sha1_init( &sha1 );
1840
1841 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1842 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001843
Andrzej Kurekeb342242019-01-29 09:14:33 -05001844 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001845 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001846
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001847 *hlen = 36;
1848
1849 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001851
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001852 mbedtls_md5_free( &md5 );
1853 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001854
Paul Bakker380da532012-04-18 16:10:25 +00001855 return;
1856}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1860#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001861void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1862 unsigned char hash[32],
1863 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001864{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001865#if defined(MBEDTLS_USE_PSA_CRYPTO)
1866 size_t hash_size;
1867 psa_status_t status;
1868 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1869
1870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1871 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1872 if( status != PSA_SUCCESS )
1873 {
1874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1875 return;
1876 }
1877
1878 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1879 if( status != PSA_SUCCESS )
1880 {
1881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1882 return;
1883 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001884
1885 *hlen = 32;
1886 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1888#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001889 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001890
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001891 mbedtls_sha256_init( &sha256 );
1892
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001894
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001895 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001896 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001897
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001898 *hlen = 32;
1899
1900 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001902
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001903 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001904#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001905 return;
1906}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001910void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1911 unsigned char hash[48],
1912 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001913{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001914#if defined(MBEDTLS_USE_PSA_CRYPTO)
1915 size_t hash_size;
1916 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001917 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001918
1919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001920 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001921 if( status != PSA_SUCCESS )
1922 {
1923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1924 return;
1925 }
1926
Andrzej Kurek972fba52019-01-30 03:29:12 -05001927 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001928 if( status != PSA_SUCCESS )
1929 {
1930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1931 return;
1932 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001933
1934 *hlen = 48;
1935 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1937#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001938 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001939
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001940 mbedtls_sha512_init( &sha512 );
1941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001943
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001944 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001945 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001946
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001947 *hlen = 48;
1948
1949 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001952 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001953#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 return;
1955}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#endif /* MBEDTLS_SHA512_C */
1957#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1960int 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 +02001961{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001962 unsigned char *p = ssl->handshake->premaster;
1963 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001964 const unsigned char *psk = ssl->conf->psk;
1965 size_t psk_len = ssl->conf->psk_len;
1966
1967 /* If the psk callback was called, use its result */
1968 if( ssl->handshake->psk != NULL )
1969 {
1970 psk = ssl->handshake->psk;
1971 psk_len = ssl->handshake->psk_len;
1972 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001973
1974 /*
1975 * PMS = struct {
1976 * opaque other_secret<0..2^16-1>;
1977 * opaque psk<0..2^16-1>;
1978 * };
1979 * with "other_secret" depending on the particular key exchange
1980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1982 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001983 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001984 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001986
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001987 *(p++) = (unsigned char)( psk_len >> 8 );
1988 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001989
1990 if( end < p || (size_t)( end - p ) < psk_len )
1991 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1992
1993 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001994 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001995 }
1996 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1998#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1999 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002000 {
2001 /*
2002 * other_secret already set by the ClientKeyExchange message,
2003 * and is 48 bytes long
2004 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002005 if( end - p < 2 )
2006 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2007
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002008 *p++ = 0;
2009 *p++ = 48;
2010 p += 48;
2011 }
2012 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2014#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2015 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002016 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002017 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002018 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002019
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002020 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002022 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002023 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002026 return( ret );
2027 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002028 *(p++) = (unsigned char)( len >> 8 );
2029 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002030 p += len;
2031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002033 }
2034 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2036#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2037 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002038 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002039 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002040 size_t zlen;
2041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002043 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002044 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002047 return( ret );
2048 }
2049
2050 *(p++) = (unsigned char)( zlen >> 8 );
2051 *(p++) = (unsigned char)( zlen );
2052 p += zlen;
2053
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002054 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2055 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002056 }
2057 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2061 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002062 }
2063
2064 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002065 if( end - p < 2 )
2066 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002067
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002068 *(p++) = (unsigned char)( psk_len >> 8 );
2069 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002070
2071 if( end < p || (size_t)( end - p ) < psk_len )
2072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2073
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002074 memcpy( p, psk, psk_len );
2075 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002076
2077 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2078
2079 return( 0 );
2080}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002084/*
2085 * SSLv3.0 MAC functions
2086 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002087#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002088static void ssl_mac( mbedtls_md_context_t *md_ctx,
2089 const unsigned char *secret,
2090 const unsigned char *buf, size_t len,
2091 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002092 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002093{
2094 unsigned char header[11];
2095 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002096 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2098 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002099
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002100 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002102 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002103 else
Paul Bakker68884e32013-01-07 18:20:04 +01002104 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002105
2106 memcpy( header, ctr, 8 );
2107 header[ 8] = (unsigned char) type;
2108 header[ 9] = (unsigned char)( len >> 8 );
2109 header[10] = (unsigned char)( len );
2110
Paul Bakker68884e32013-01-07 18:20:04 +01002111 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 mbedtls_md_starts( md_ctx );
2113 mbedtls_md_update( md_ctx, secret, md_size );
2114 mbedtls_md_update( md_ctx, padding, padlen );
2115 mbedtls_md_update( md_ctx, header, 11 );
2116 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002117 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002118
Paul Bakker68884e32013-01-07 18:20:04 +01002119 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 mbedtls_md_starts( md_ctx );
2121 mbedtls_md_update( md_ctx, secret, md_size );
2122 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002123 mbedtls_md_update( md_ctx, out, md_size );
2124 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002127
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002128/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002129 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002130#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002131 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2132 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2133 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2134/* This function makes sure every byte in the memory region is accessed
2135 * (in ascending addresses order) */
2136static void ssl_read_memory( unsigned char *p, size_t len )
2137{
2138 unsigned char acc = 0;
2139 volatile unsigned char force;
2140
2141 for( ; len != 0; p++, len-- )
2142 acc ^= *p;
2143
2144 force = acc;
2145 (void) force;
2146}
2147#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2148
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002149/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002151 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002152
Hanno Beckera0e20d02019-05-15 14:03:01 +01002153#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002154/* This functions transforms a DTLS plaintext fragment and a record content
2155 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002156 *
2157 * struct {
2158 * opaque content[DTLSPlaintext.length];
2159 * ContentType real_type;
2160 * uint8 zeros[length_of_padding];
2161 * } DTLSInnerPlaintext;
2162 *
2163 * Input:
2164 * - `content`: The beginning of the buffer holding the
2165 * plaintext to be wrapped.
2166 * - `*content_size`: The length of the plaintext in Bytes.
2167 * - `max_len`: The number of Bytes available starting from
2168 * `content`. This must be `>= *content_size`.
2169 * - `rec_type`: The desired record content type.
2170 *
2171 * Output:
2172 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2173 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2174 *
2175 * Returns:
2176 * - `0` on success.
2177 * - A negative error code if `max_len` didn't offer enough space
2178 * for the expansion.
2179 */
2180static int ssl_cid_build_inner_plaintext( unsigned char *content,
2181 size_t *content_size,
2182 size_t remaining,
2183 uint8_t rec_type )
2184{
2185 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002186 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2187 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2188 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002189
2190 /* Write real content type */
2191 if( remaining == 0 )
2192 return( -1 );
2193 content[ len ] = rec_type;
2194 len++;
2195 remaining--;
2196
2197 if( remaining < pad )
2198 return( -1 );
2199 memset( content + len, 0, pad );
2200 len += pad;
2201 remaining -= pad;
2202
2203 *content_size = len;
2204 return( 0 );
2205}
2206
Hanno Becker07dc97d2019-05-20 15:08:01 +01002207/* This function parses a DTLSInnerPlaintext structure.
2208 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002209static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2210 size_t *content_size,
2211 uint8_t *rec_type )
2212{
2213 size_t remaining = *content_size;
2214
2215 /* Determine length of padding by skipping zeroes from the back. */
2216 do
2217 {
2218 if( remaining == 0 )
2219 return( -1 );
2220 remaining--;
2221 } while( content[ remaining ] == 0 );
2222
2223 *content_size = remaining;
2224 *rec_type = content[ remaining ];
2225
2226 return( 0 );
2227}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002228#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002229
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002230/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002231 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002232static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002233 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002234 mbedtls_record *rec )
2235{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002236 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002237 *
2238 * additional_data = seq_num + TLSCompressed.type +
2239 * TLSCompressed.version + TLSCompressed.length;
2240 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002241 * For the CID extension, this is extended as follows
2242 * (quoting draft-ietf-tls-dtls-connection-id-05,
2243 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002244 *
2245 * additional_data = seq_num + DTLSPlaintext.type +
2246 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002247 * cid +
2248 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002249 * length_of_DTLSInnerPlaintext;
2250 */
2251
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002252 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2253 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002254 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002255
Hanno Beckera0e20d02019-05-15 14:03:01 +01002256#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002257 if( rec->cid_len != 0 )
2258 {
2259 memcpy( add_data + 11, rec->cid, rec->cid_len );
2260 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2261 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2262 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2263 *add_data_len = 13 + 1 + rec->cid_len;
2264 }
2265 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002266#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002267 {
2268 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2269 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2270 *add_data_len = 13;
2271 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002272}
2273
Hanno Beckera18d1322018-01-03 14:27:32 +00002274int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2275 mbedtls_ssl_transform *transform,
2276 mbedtls_record *rec,
2277 int (*f_rng)(void *, unsigned char *, size_t),
2278 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002279{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002281 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002282 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002283 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002284 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002285 size_t post_avail;
2286
2287 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002288#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002289 ((void) ssl);
2290#endif
2291
2292 /* The PRNG is used for dynamic IV generation that's used
2293 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2294#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2295 ( defined(MBEDTLS_AES_C) || \
2296 defined(MBEDTLS_ARIA_C) || \
2297 defined(MBEDTLS_CAMELLIA_C) ) && \
2298 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2299 ((void) f_rng);
2300 ((void) p_rng);
2301#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002304
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002305 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002306 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2309 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002310 if( rec == NULL
2311 || rec->buf == NULL
2312 || rec->buf_len < rec->data_offset
2313 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002314#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002315 || rec->cid_len != 0
2316#endif
2317 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002318 {
2319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002321 }
2322
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002323 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002324 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002326 data, rec->data_len );
2327
2328 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2329
2330 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2331 {
2332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2333 (unsigned) rec->data_len,
2334 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2336 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002337
Hanno Beckera0e20d02019-05-15 14:03:01 +01002338#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002339 /*
2340 * Add CID information
2341 */
2342 rec->cid_len = transform->out_cid_len;
2343 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2344 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002345
2346 if( rec->cid_len != 0 )
2347 {
2348 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002349 * Wrap plaintext into DTLSInnerPlaintext structure.
2350 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002351 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002352 * Note that this changes `rec->data_len`, and hence
2353 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002354 */
2355 if( ssl_cid_build_inner_plaintext( data,
2356 &rec->data_len,
2357 post_avail,
2358 rec->type ) != 0 )
2359 {
2360 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2361 }
2362
2363 rec->type = MBEDTLS_SSL_MSG_CID;
2364 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002365#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002366
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002367 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2368
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002370 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002371 */
Hanno Becker52344c22018-01-03 15:24:20 +00002372#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 if( mode == MBEDTLS_MODE_STREAM ||
2374 ( mode == MBEDTLS_MODE_CBC
2375#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002376 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002377#endif
2378 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002380 if( post_avail < transform->maclen )
2381 {
2382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2383 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2384 }
2385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002387 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002388 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002389 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002390 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2391 data, rec->data_len, rec->ctr, rec->type, mac );
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2397 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002398 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002399 {
Hanno Becker992b6872017-11-09 18:57:39 +00002400 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2401
Hanno Beckercab87e62019-04-29 13:52:53 +01002402 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002403
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002404 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002405 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002406 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2407 data, rec->data_len );
2408 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2409 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2410
2411 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002412 }
2413 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002414#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002418 }
2419
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002420 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2421 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002422
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002423 rec->data_len += transform->maclen;
2424 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002425 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002426 }
Hanno Becker52344c22018-01-03 15:24:20 +00002427#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002429 /*
2430 * Encrypt
2431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2433 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002435 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002436 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002438 "including %d bytes of padding",
2439 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002441 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2442 transform->iv_enc, transform->ivlen,
2443 data, rec->data_len,
2444 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002447 return( ret );
2448 }
2449
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002450 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002454 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 }
Paul Bakker68884e32013-01-07 18:20:04 +01002456 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002458
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002459#if defined(MBEDTLS_GCM_C) || \
2460 defined(MBEDTLS_CCM_C) || \
2461 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002463 mode == MBEDTLS_MODE_CCM ||
2464 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002465 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002466 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002467 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002468 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002469
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002470 /* Check that there's space for both the authentication tag
2471 * and the explicit IV before and after the record content. */
2472 if( post_avail < transform->taglen ||
2473 rec->data_offset < explicit_iv_len )
2474 {
2475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2476 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2477 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002478
Paul Bakker68884e32013-01-07 18:20:04 +01002479 /*
2480 * Generate IV
2481 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002482 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2483 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002484 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002485 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002486 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2487 explicit_iv_len );
2488 /* Prefix record content with explicit IV. */
2489 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002490 }
2491 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2492 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002493 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002494 unsigned char i;
2495
2496 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2497
2498 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002499 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002500 }
2501 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002502 {
2503 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002506 }
2507
Hanno Beckercab87e62019-04-29 13:52:53 +01002508 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002509
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002510 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2511 iv, transform->ivlen );
2512 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002513 data - explicit_iv_len, explicit_iv_len );
2514 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002515 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002517 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002518 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002519
Paul Bakker68884e32013-01-07 18:20:04 +01002520 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002521 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002522 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002523
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002524 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002525 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002526 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002527 data, rec->data_len, /* source */
2528 data, &rec->data_len, /* destination */
2529 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002532 return( ret );
2533 }
2534
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002535 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2536 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002537
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002538 rec->data_len += transform->taglen + explicit_iv_len;
2539 rec->data_offset -= explicit_iv_len;
2540 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002541 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2545#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002546 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002549 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002550 size_t padlen, i;
2551 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002552
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002553 /* Currently we're always using minimal padding
2554 * (up to 255 bytes would be allowed). */
2555 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2556 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 padlen = 0;
2558
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002559 /* Check there's enough space in the buffer for the padding. */
2560 if( post_avail < padlen + 1 )
2561 {
2562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2563 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2564 }
2565
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002567 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002569 rec->data_len += padlen + 1;
2570 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002573 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002574 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2575 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002576 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002577 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002578 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002579 if( f_rng == NULL )
2580 {
2581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2582 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2583 }
2584
2585 if( rec->data_offset < transform->ivlen )
2586 {
2587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2588 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2589 }
2590
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002591 /*
2592 * Generate IV
2593 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002594 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002595 if( ret != 0 )
2596 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002597
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002598 memcpy( data - transform->ivlen, transform->iv_enc,
2599 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002600
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002601 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002605 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002606 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002607 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002608
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002609 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2610 transform->iv_enc,
2611 transform->ivlen,
2612 data, rec->data_len,
2613 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002616 return( ret );
2617 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002618
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002619 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2622 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002623 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002626 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002627 {
2628 /*
2629 * Save IV in SSL3 and TLS1
2630 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002631 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2632 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002634 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002635#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002636 {
2637 data -= transform->ivlen;
2638 rec->data_offset -= transform->ivlen;
2639 rec->data_len += transform->ivlen;
2640 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002643 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002644 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002645 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2646
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002647 /*
2648 * MAC(MAC_write_key, seq_num +
2649 * TLSCipherText.type +
2650 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002651 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002652 * IV + // except for TLS 1.0
2653 * ENC(content + padding + padding_length));
2654 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002655
2656 if( post_avail < transform->maclen)
2657 {
2658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2659 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2660 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002661
Hanno Beckercab87e62019-04-29 13:52:53 +01002662 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002665 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002666 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002667
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002668 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002669 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002670 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2671 data, rec->data_len );
2672 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2673 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002674
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002675 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002676
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002677 rec->data_len += transform->maclen;
2678 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002679 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002680 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002683 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002685 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2688 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002689 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002691 /* Make extra sure authentication was performed, exactly once */
2692 if( auth_done != 1 )
2693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2695 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002696 }
2697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
2700 return( 0 );
2701}
2702
Hanno Becker605949f2019-07-12 08:23:59 +01002703int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002704 mbedtls_ssl_transform *transform,
2705 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002706{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002707 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002709 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002710#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002711 size_t padlen = 0, correct = 1;
2712#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002713 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002714 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002715 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002716
Hanno Beckera18d1322018-01-03 14:27:32 +00002717#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002718 ((void) ssl);
2719#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002722 if( rec == NULL ||
2723 rec->buf == NULL ||
2724 rec->buf_len < rec->data_offset ||
2725 rec->buf_len - rec->data_offset < rec->data_len )
2726 {
2727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002729 }
2730
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002731 data = rec->buf + rec->data_offset;
2732 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
Hanno Beckera0e20d02019-05-15 14:03:01 +01002734#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002735 /*
2736 * Match record's CID with incoming CID.
2737 */
Hanno Becker938489a2019-05-08 13:02:22 +01002738 if( rec->cid_len != transform->in_cid_len ||
2739 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2740 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002741 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002742 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002743#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2746 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002747 {
2748 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002749 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2750 transform->iv_dec,
2751 transform->ivlen,
2752 data, rec->data_len,
2753 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002756 return( ret );
2757 }
2758
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002759 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2762 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002763 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 }
Paul Bakker68884e32013-01-07 18:20:04 +01002765 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002767#if defined(MBEDTLS_GCM_C) || \
2768 defined(MBEDTLS_CCM_C) || \
2769 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002771 mode == MBEDTLS_MODE_CCM ||
2772 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002773 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002774 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002775 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002776
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002777 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002778 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002779 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002780
2781 /* Check that there's enough space for the explicit IV
2782 * (at the beginning of the record) and the MAC (at the
2783 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002784 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002787 "+ taglen (%d)", rec->data_len,
2788 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002790 }
Paul Bakker68884e32013-01-07 18:20:04 +01002791
Hanno Beckerd96a6522019-07-10 13:55:25 +01002792#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002793 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2794 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002795 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002796
Hanno Beckerd96a6522019-07-10 13:55:25 +01002797 /* Fixed */
2798 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2799 /* Explicit */
2800 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002801 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002802 else
2803#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2804#if defined(MBEDTLS_CHACHAPOLY_C)
2805 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002806 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002807 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002808 unsigned char i;
2809
2810 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2811
2812 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002813 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002814 }
2815 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002816#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002817 {
2818 /* Reminder if we ever add an AEAD mode with a different size */
2819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2820 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2821 }
2822
Hanno Beckerd96a6522019-07-10 13:55:25 +01002823 /* Group changes to data, data_len, and add_data, because
2824 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002825 data += explicit_iv_len;
2826 rec->data_offset += explicit_iv_len;
2827 rec->data_len -= explicit_iv_len + transform->taglen;
2828
Hanno Beckercab87e62019-04-29 13:52:53 +01002829 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002830 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002831 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002832
Hanno Beckerd96a6522019-07-10 13:55:25 +01002833 /* Because of the check above, we know that there are
2834 * explicit_iv_len Bytes preceeding data, and taglen
2835 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002836 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002837 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002838
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002839 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002840 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002841 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002842
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002843 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002844 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002845 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002846 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2847 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002848 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002849 data, rec->data_len,
2850 data, &olen,
2851 data + rec->data_len,
2852 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2857 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002858
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002859 return( ret );
2860 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002861 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002862
Hanno Beckerd96a6522019-07-10 13:55:25 +01002863 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002864 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2867 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002868 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002869 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2872#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002873 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002875 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002876 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002877
Paul Bakker5121ce52009-01-03 21:22:43 +00002878 /*
Paul Bakker45829992013-01-03 14:52:21 +01002879 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002882 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2883 {
2884 /* The ciphertext is prefixed with the CBC IV. */
2885 minlen += transform->ivlen;
2886 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002887#endif
Paul Bakker45829992013-01-03 14:52:21 +01002888
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002889 /* Size considerations:
2890 *
2891 * - The CBC cipher text must not be empty and hence
2892 * at least of size transform->ivlen.
2893 *
2894 * Together with the potential IV-prefix, this explains
2895 * the first of the two checks below.
2896 *
2897 * - The record must contain a MAC, either in plain or
2898 * encrypted, depending on whether Encrypt-then-MAC
2899 * is used or not.
2900 * - If it is, the message contains the IV-prefix,
2901 * the CBC ciphertext, and the MAC.
2902 * - If it is not, the padded plaintext, and hence
2903 * the CBC ciphertext, has at least length maclen + 1
2904 * because there is at least the padding length byte.
2905 *
2906 * As the CBC ciphertext is not empty, both cases give the
2907 * lower bound minlen + maclen + 1 on the record size, which
2908 * we test for in the second check below.
2909 */
2910 if( rec->data_len < minlen + transform->ivlen ||
2911 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002914 "+ 1 ) ( + expl IV )", rec->data_len,
2915 transform->ivlen,
2916 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002918 }
2919
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002920 /*
2921 * Authenticate before decrypt if enabled
2922 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002924 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002925 {
Hanno Becker992b6872017-11-09 18:57:39 +00002926 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002929
Hanno Beckerd96a6522019-07-10 13:55:25 +01002930 /* Update data_len in tandem with add_data.
2931 *
2932 * The subtraction is safe because of the previous check
2933 * data_len >= minlen + maclen + 1.
2934 *
2935 * Afterwards, we know that data + data_len is followed by at
2936 * least maclen Bytes, which justifies the call to
2937 * mbedtls_ssl_safer_memcmp() below.
2938 *
2939 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002940 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002941 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002942
Hanno Beckerd96a6522019-07-10 13:55:25 +01002943 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002944 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2945 add_data_len );
2946 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2947 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002948 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2949 data, rec->data_len );
2950 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2951 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002952
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002953 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2954 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002955 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002956 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002957
Hanno Beckerd96a6522019-07-10 13:55:25 +01002958 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002959 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2960 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002964 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002965 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002966 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002968
2969 /*
2970 * Check length sanity
2971 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002972
2973 /* We know from above that data_len > minlen >= 0,
2974 * so the following check in particular implies that
2975 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002976 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002979 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002981 }
2982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002984 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002985 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002986 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002987 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002988 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002989 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002990 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002991
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002992 data += transform->ivlen;
2993 rec->data_offset += transform->ivlen;
2994 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002995 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002997
Hanno Beckerd96a6522019-07-10 13:55:25 +01002998 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2999
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003000 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3001 transform->iv_dec, transform->ivlen,
3002 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003005 return( ret );
3006 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003007
Hanno Beckerd96a6522019-07-10 13:55:25 +01003008 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003009 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3012 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003013 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003016 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003017 {
3018 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01003019 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3020 * records is equivalent to CBC decryption of the concatenation
3021 * of the records; in other words, IVs are maintained across
3022 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003023 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003024 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3025 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003028
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003029 /* Safe since data_len >= minlen + maclen + 1, so after having
3030 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003031 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3032 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003033 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003034
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003035 if( auth_done == 1 )
3036 {
3037 correct *= ( rec->data_len >= padlen + 1 );
3038 padlen *= ( rec->data_len >= padlen + 1 );
3039 }
3040 else
Paul Bakker45829992013-01-03 14:52:21 +01003041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003043 if( rec->data_len < transform->maclen + padlen + 1 )
3044 {
3045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3046 rec->data_len,
3047 transform->maclen,
3048 padlen + 1 ) );
3049 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003050#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003051
3052 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3053 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003054 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003056 padlen++;
3057
3058 /* Regardless of the validity of the padding,
3059 * we have data_len >= padlen here. */
3060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003062 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003063 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003064 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066#if defined(MBEDTLS_SSL_DEBUG_ALL)
3067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003068 "should be no more than %d",
3069 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003070#endif
Paul Bakker45829992013-01-03 14:52:21 +01003071 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003072 }
3073 }
3074 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3076#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3077 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003078 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003079 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003080 /* The padding check involves a series of up to 256
3081 * consecutive memory reads at the end of the record
3082 * plaintext buffer. In order to hide the length and
3083 * validity of the padding, always perform exactly
3084 * `min(256,plaintext_len)` reads (but take into account
3085 * only the last `padlen` bytes for the padding check). */
3086 size_t pad_count = 0;
3087 size_t real_count = 0;
3088 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003089
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003090 /* Index of first padding byte; it has been ensured above
3091 * that the subtraction is safe. */
3092 size_t const padding_idx = rec->data_len - padlen;
3093 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3094 size_t const start_idx = rec->data_len - num_checks;
3095 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003096
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003097 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003098 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003099 real_count |= ( idx >= padding_idx );
3100 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003101 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003102 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003105 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003107#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003108 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003110 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3112 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3115 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003116 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003117
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003118 /* If the padding was found to be invalid, padlen == 0
3119 * and the subtraction is safe. If the padding was found valid,
3120 * padlen hasn't been changed and the previous assertion
3121 * data_len >= padlen still holds. */
3122 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003123 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003124 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003126 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3129 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003130 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003131
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003132#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003134 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003135#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003136
3137 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003138 * Authenticate if not done yet.
3139 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003140 */
Hanno Becker52344c22018-01-03 15:24:20 +00003141#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003142 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003143 {
Hanno Becker992b6872017-11-09 18:57:39 +00003144 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003145
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003146 /* If the initial value of padlen was such that
3147 * data_len < maclen + padlen + 1, then padlen
3148 * got reset to 1, and the initial check
3149 * data_len >= minlen + maclen + 1
3150 * guarantees that at this point we still
3151 * have at least data_len >= maclen.
3152 *
3153 * If the initial value of padlen was such that
3154 * data_len >= maclen + padlen + 1, then we have
3155 * subtracted either padlen + 1 (if the padding was correct)
3156 * or 0 (if the padding was incorrect) since then,
3157 * hence data_len >= maclen in any case.
3158 */
3159 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003160 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003163 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003164 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003165 ssl_mac( &transform->md_ctx_dec,
3166 transform->mac_dec,
3167 data, rec->data_len,
3168 rec->ctr, rec->type,
3169 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003170 }
3171 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3173#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3174 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003175 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003176 {
3177 /*
3178 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003179 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003180 *
3181 * Known timing attacks:
3182 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3183 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003184 * To compensate for different timings for the MAC calculation
3185 * depending on how much padding was removed (which is determined
3186 * by padlen), process extra_run more blocks through the hash
3187 * function.
3188 *
3189 * The formula in the paper is
3190 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3191 * where L1 is the size of the header plus the decrypted message
3192 * plus CBC padding and L2 is the size of the header plus the
3193 * decrypted message. This is for an underlying hash function
3194 * with 64-byte blocks.
3195 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3196 * correctly. We round down instead of up, so -56 is the correct
3197 * value for our calculations instead of -55.
3198 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003199 * Repeat the formula rather than defining a block_size variable.
3200 * This avoids requiring division by a variable at runtime
3201 * (which would be marginally less efficient and would require
3202 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003203 */
3204 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003205 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003206
3207 /*
3208 * The next two sizes are the minimum and maximum values of
3209 * in_msglen over all padlen values.
3210 *
3211 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003212 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003213 *
3214 * Note that max_len + maclen is never more than the buffer
3215 * length, as we previously did in_msglen -= maclen too.
3216 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003217 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003218 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3219
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003220 memset( tmp, 0, sizeof( tmp ) );
3221
3222 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003223 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003224#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3225 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003226 case MBEDTLS_MD_MD5:
3227 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003228 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003229 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003230 extra_run =
3231 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3232 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003233 break;
3234#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003235#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003236 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003237 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003238 extra_run =
3239 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3240 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003241 break;
3242#endif
3243 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3246 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003247
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003248 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003249
Hanno Beckercab87e62019-04-29 13:52:53 +01003250 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3251 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003252 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3253 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003254 /* Make sure we access everything even when padlen > 0. This
3255 * makes the synchronisation requirements for just-in-time
3256 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003257 ssl_read_memory( data + rec->data_len, padlen );
3258 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003259
3260 /* Call mbedtls_md_process at least once due to cache attacks
3261 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003262 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003263 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003264
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003265 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003266
3267 /* Make sure we access all the memory that could contain the MAC,
3268 * before we check it in the next code block. This makes the
3269 * synchronisation requirements for just-in-time Prime+Probe
3270 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003271 ssl_read_memory( data + min_len,
3272 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003273 }
3274 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3276 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3279 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003280 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003281
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003282#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003283 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3284 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003285#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003286
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003287 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3288 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290#if defined(MBEDTLS_SSL_DEBUG_ALL)
3291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003292#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003293 correct = 0;
3294 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003295 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003296 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003297
3298 /*
3299 * Finally check the correct flag
3300 */
3301 if( correct == 0 )
3302 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003303#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003304
3305 /* Make extra sure authentication was performed, exactly once */
3306 if( auth_done != 1 )
3307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3309 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003311
Hanno Beckera0e20d02019-05-15 14:03:01 +01003312#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003313 if( rec->cid_len != 0 )
3314 {
3315 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3316 &rec->type );
3317 if( ret != 0 )
3318 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3319 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003320#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003323
3324 return( 0 );
3325}
3326
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003327#undef MAC_NONE
3328#undef MAC_PLAINTEXT
3329#undef MAC_CIPHERTEXT
3330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003332/*
3333 * Compression/decompression functions
3334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003336{
3337 int ret;
3338 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003339 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003340 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003341 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003344
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003345 if( len_pre == 0 )
3346 return( 0 );
3347
Paul Bakker2770fbd2012-07-03 13:30:23 +00003348 memcpy( msg_pre, ssl->out_msg, len_pre );
3349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003351 ssl->out_msglen ) );
3352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003354 ssl->out_msg, ssl->out_msglen );
3355
Paul Bakker48916f92012-09-16 19:57:18 +00003356 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3357 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3358 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003359 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003360
Paul Bakker48916f92012-09-16 19:57:18 +00003361 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003362 if( ret != Z_OK )
3363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3365 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003366 }
3367
Angus Grattond8213d02016-05-25 20:56:48 +10003368 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003369 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003372 ssl->out_msglen ) );
3373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003375 ssl->out_msg, ssl->out_msglen );
3376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003378
3379 return( 0 );
3380}
3381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003383{
3384 int ret;
3385 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003386 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003387 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003388 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003391
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003392 if( len_pre == 0 )
3393 return( 0 );
3394
Paul Bakker2770fbd2012-07-03 13:30:23 +00003395 memcpy( msg_pre, ssl->in_msg, len_pre );
3396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003398 ssl->in_msglen ) );
3399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003401 ssl->in_msg, ssl->in_msglen );
3402
Paul Bakker48916f92012-09-16 19:57:18 +00003403 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3404 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3405 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003406 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003407 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003408
Paul Bakker48916f92012-09-16 19:57:18 +00003409 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003410 if( ret != Z_OK )
3411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3413 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003414 }
3415
Angus Grattond8213d02016-05-25 20:56:48 +10003416 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003417 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003420 ssl->in_msglen ) );
3421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003423 ssl->in_msg, ssl->in_msglen );
3424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003426
3427 return( 0 );
3428}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3432static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434#if defined(MBEDTLS_SSL_PROTO_DTLS)
3435static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003436{
3437 /* If renegotiation is not enforced, retransmit until we would reach max
3438 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003439 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003440 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003441 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003442 unsigned char doublings = 1;
3443
3444 while( ratio != 0 )
3445 {
3446 ++doublings;
3447 ratio >>= 1;
3448 }
3449
3450 if( ++ssl->renego_records_seen > doublings )
3451 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003453 return( 0 );
3454 }
3455 }
3456
3457 return( ssl_write_hello_request( ssl ) );
3458}
3459#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003461
Paul Bakker5121ce52009-01-03 21:22:43 +00003462/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003463 * Fill the input message buffer by appending data to it.
3464 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003465 *
3466 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3467 * available (from this read and/or a previous one). Otherwise, an error code
3468 * is returned (possibly EOF or WANT_READ).
3469 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003470 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3471 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3472 * since we always read a whole datagram at once.
3473 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003474 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003475 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003478{
Paul Bakker23986e52011-04-24 08:57:21 +00003479 int ret;
3480 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003483
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003484 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003487 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003489 }
3490
Angus Grattond8213d02016-05-25 20:56:48 +10003491 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3494 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003495 }
3496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003498 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003499 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003500 uint32_t timeout;
3501
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003502 /* Just to be sure */
3503 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3504 {
3505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3506 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3508 }
3509
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003510 /*
3511 * The point is, we need to always read a full datagram at once, so we
3512 * sometimes read more then requested, and handle the additional data.
3513 * It could be the rest of the current record (while fetching the
3514 * header) and/or some other records in the same datagram.
3515 */
3516
3517 /*
3518 * Move to the next record in the already read datagram if applicable
3519 */
3520 if( ssl->next_record_offset != 0 )
3521 {
3522 if( ssl->in_left < ssl->next_record_offset )
3523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003526 }
3527
3528 ssl->in_left -= ssl->next_record_offset;
3529
3530 if( ssl->in_left != 0 )
3531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003533 ssl->next_record_offset ) );
3534 memmove( ssl->in_hdr,
3535 ssl->in_hdr + ssl->next_record_offset,
3536 ssl->in_left );
3537 }
3538
3539 ssl->next_record_offset = 0;
3540 }
3541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003543 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003544
3545 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003546 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003547 */
3548 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003551 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003552 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003553
3554 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003555 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003556 * are not at the beginning of a new record, the caller did something
3557 * wrong.
3558 */
3559 if( ssl->in_left != 0 )
3560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003563 }
3564
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003565 /*
3566 * Don't even try to read if time's out already.
3567 * This avoids by-passing the timer when repeatedly receiving messages
3568 * that will end up being dropped.
3569 */
3570 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003571 {
3572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003573 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003574 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003575 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003576 {
Angus Grattond8213d02016-05-25 20:56:48 +10003577 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003580 timeout = ssl->handshake->retransmit_timeout;
3581 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003582 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003585
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003586 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003587 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3588 timeout );
3589 else
3590 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003593
3594 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003596 }
3597
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003598 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003601 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003604 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003605 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003608 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003609 }
3610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003614 return( ret );
3615 }
3616
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003617 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003618 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003620 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003622 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003623 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003626 return( ret );
3627 }
3628
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003629 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003630 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003632 }
3633
Paul Bakker5121ce52009-01-03 21:22:43 +00003634 if( ret < 0 )
3635 return( ret );
3636
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003637 ssl->in_left = ret;
3638 }
3639 else
3640#endif
3641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003643 ssl->in_left, nb_want ) );
3644
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003645 while( ssl->in_left < nb_want )
3646 {
3647 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003648
3649 if( ssl_check_timer( ssl ) != 0 )
3650 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3651 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003652 {
3653 if( ssl->f_recv_timeout != NULL )
3654 {
3655 ret = ssl->f_recv_timeout( ssl->p_bio,
3656 ssl->in_hdr + ssl->in_left, len,
3657 ssl->conf->read_timeout );
3658 }
3659 else
3660 {
3661 ret = ssl->f_recv( ssl->p_bio,
3662 ssl->in_hdr + ssl->in_left, len );
3663 }
3664 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003667 ssl->in_left, nb_want ) );
3668 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003669
3670 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003672
3673 if( ret < 0 )
3674 return( ret );
3675
mohammad160352aecb92018-03-28 23:41:40 -07003676 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003677 {
Darryl Green11999bb2018-03-13 15:22:58 +00003678 MBEDTLS_SSL_DEBUG_MSG( 1,
3679 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003680 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3682 }
3683
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003684 ssl->in_left += ret;
3685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003686 }
3687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003689
3690 return( 0 );
3691}
3692
3693/*
3694 * Flush any data not yet written
3695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003697{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003698 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003699 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003702
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003703 if( ssl->f_send == NULL )
3704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003706 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003708 }
3709
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003710 /* Avoid incrementing counter if data is flushed */
3711 if( ssl->out_left == 0 )
3712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003714 return( 0 );
3715 }
3716
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 while( ssl->out_left > 0 )
3718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003720 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003721
Hanno Becker2b1e3542018-08-06 11:19:13 +01003722 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003723 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003726
3727 if( ret <= 0 )
3728 return( ret );
3729
mohammad160352aecb92018-03-28 23:41:40 -07003730 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003731 {
Darryl Green11999bb2018-03-13 15:22:58 +00003732 MBEDTLS_SSL_DEBUG_MSG( 1,
3733 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003734 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003735 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3736 }
3737
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 ssl->out_left -= ret;
3739 }
3740
Hanno Becker2b1e3542018-08-06 11:19:13 +01003741#if defined(MBEDTLS_SSL_PROTO_DTLS)
3742 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003743 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003744 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003745 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003746 else
3747#endif
3748 {
3749 ssl->out_hdr = ssl->out_buf + 8;
3750 }
3751 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003754
3755 return( 0 );
3756}
3757
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003758/*
3759 * Functions to handle the DTLS retransmission state machine
3760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003762/*
3763 * Append current handshake message to current outgoing flight
3764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3769 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3770 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003771
3772 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003773 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003774 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003777 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003778 }
3779
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003780 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003781 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003784 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003785 }
3786
3787 /* Copy current handshake message with headers */
3788 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3789 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003790 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003791 msg->next = NULL;
3792
3793 /* Append to the current flight */
3794 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003795 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003796 else
3797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003798 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003799 while( cur->next != NULL )
3800 cur = cur->next;
3801 cur->next = msg;
3802 }
3803
Hanno Becker3b235902018-08-06 09:54:53 +01003804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003805 return( 0 );
3806}
3807
3808/*
3809 * Free the current flight of handshake messages
3810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003812{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 mbedtls_ssl_flight_item *cur = flight;
3814 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003815
3816 while( cur != NULL )
3817 {
3818 next = cur->next;
3819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 mbedtls_free( cur->p );
3821 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003822
3823 cur = next;
3824 }
3825}
3826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3828static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003829#endif
3830
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003831/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003832 * Swap transform_out and out_ctr with the alternative ones
3833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003835{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003837 unsigned char tmp_out_ctr[8];
3838
3839 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003842 return;
3843 }
3844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003846
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003847 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003848 tmp_transform = ssl->transform_out;
3849 ssl->transform_out = ssl->handshake->alt_transform_out;
3850 ssl->handshake->alt_transform_out = tmp_transform;
3851
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003852 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003853 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3854 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003855 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003856
3857 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003858 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3861 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3866 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003867 }
3868 }
3869#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003870}
3871
3872/*
3873 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003874 */
3875int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3876{
3877 int ret = 0;
3878
3879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3880
3881 ret = mbedtls_ssl_flight_transmit( ssl );
3882
3883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3884
3885 return( ret );
3886}
3887
3888/*
3889 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003890 *
3891 * Need to remember the current message in case flush_output returns
3892 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003893 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003894 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003895int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003896{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003897 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003901 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003903
3904 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003905 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003906 ssl_swap_epochs( ssl );
3907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003909 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003910
3911 while( ssl->handshake->cur_msg != NULL )
3912 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003913 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003914 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003915
Hanno Beckere1dcb032018-08-17 16:47:58 +01003916 int const is_finished =
3917 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3918 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3919
Hanno Becker04da1892018-08-14 13:22:10 +01003920 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3921 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3922
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003923 /* Swap epochs before sending Finished: we can't do it after
3924 * sending ChangeCipherSpec, in case write returns WANT_READ.
3925 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003926 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003927 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003929 ssl_swap_epochs( ssl );
3930 }
3931
Hanno Becker67bc7c32018-08-06 11:33:50 +01003932 ret = ssl_get_remaining_payload_in_datagram( ssl );
3933 if( ret < 0 )
3934 return( ret );
3935 max_frag_len = (size_t) ret;
3936
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003937 /* CCS is copied as is, while HS messages may need fragmentation */
3938 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3939 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003940 if( max_frag_len == 0 )
3941 {
3942 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3943 return( ret );
3944
3945 continue;
3946 }
3947
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003948 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003949 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003950 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003951
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003952 /* Update position inside current message */
3953 ssl->handshake->cur_msg_p += cur->len;
3954 }
3955 else
3956 {
3957 const unsigned char * const p = ssl->handshake->cur_msg_p;
3958 const size_t hs_len = cur->len - 12;
3959 const size_t frag_off = p - ( cur->p + 12 );
3960 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003961 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003962
Hanno Beckere1dcb032018-08-17 16:47:58 +01003963 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003964 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003965 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003966 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003967
Hanno Becker67bc7c32018-08-06 11:33:50 +01003968 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3969 return( ret );
3970
3971 continue;
3972 }
3973 max_hs_frag_len = max_frag_len - 12;
3974
3975 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3976 max_hs_frag_len : rem_len;
3977
3978 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003979 {
3980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003981 (unsigned) cur_hs_frag_len,
3982 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003983 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003984
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003985 /* Messages are stored with handshake headers as if not fragmented,
3986 * copy beginning of headers then fill fragmentation fields.
3987 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3988 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003989
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003990 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3991 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3992 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3993
Hanno Becker67bc7c32018-08-06 11:33:50 +01003994 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3995 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3996 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003997
3998 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3999
Hanno Becker3f7b9732018-08-28 09:53:25 +01004000 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004001 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4002 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004003 ssl->out_msgtype = cur->type;
4004
4005 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004006 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004007 }
4008
4009 /* If done with the current message move to the next one if any */
4010 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4011 {
4012 if( cur->next != NULL )
4013 {
4014 ssl->handshake->cur_msg = cur->next;
4015 ssl->handshake->cur_msg_p = cur->next->p + 12;
4016 }
4017 else
4018 {
4019 ssl->handshake->cur_msg = NULL;
4020 ssl->handshake->cur_msg_p = NULL;
4021 }
4022 }
4023
4024 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004025 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004028 return( ret );
4029 }
4030 }
4031
Hanno Becker67bc7c32018-08-06 11:33:50 +01004032 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4033 return( ret );
4034
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004035 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4037 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004038 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004041 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4042 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004043
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004045
4046 return( 0 );
4047}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004048
4049/*
4050 * To be called when the last message of an incoming flight is received.
4051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004053{
4054 /* We won't need to resend that one any more */
4055 ssl_flight_free( ssl->handshake->flight );
4056 ssl->handshake->flight = NULL;
4057 ssl->handshake->cur_msg = NULL;
4058
4059 /* The next incoming flight will start with this msg_seq */
4060 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4061
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004062 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004063 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004064
Hanno Becker0271f962018-08-16 13:23:47 +01004065 /* Clear future message buffering structure. */
4066 ssl_buffering_free( ssl );
4067
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004068 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004069 ssl_set_timer( ssl, 0 );
4070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4072 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004075 }
4076 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004077 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004078}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004079
4080/*
4081 * To be called when the last message of an outgoing flight is send.
4082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004083void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004084{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004085 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004086 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004088 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4089 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004091 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004092 }
4093 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004096#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004097
Paul Bakker5121ce52009-01-03 21:22:43 +00004098/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004099 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004100 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004101
4102/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004103 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004104 *
4105 * - fill in handshake headers
4106 * - update handshake checksum
4107 * - DTLS: save message for resending
4108 * - then pass to the record layer
4109 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004110 * DTLS: except for HelloRequest, messages are only queued, and will only be
4111 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004112 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004113 * Inputs:
4114 * - ssl->out_msglen: 4 + actual handshake message len
4115 * (4 is the size of handshake headers for TLS)
4116 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4117 * - ssl->out_msg + 4: the handshake message body
4118 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004119 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004120 * - ssl->out_msglen: the length of the record contents
4121 * (including handshake headers but excluding record headers)
4122 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004123 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004124int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004125{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004126 int ret;
4127 const size_t hs_len = ssl->out_msglen - 4;
4128 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004129
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4131
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004132 /*
4133 * Sanity checks
4134 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004135 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004136 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4137 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004138 /* In SSLv3, the client might send a NoCertificate alert. */
4139#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4140 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4141 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4142 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4143#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4144 {
4145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4146 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4147 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004148 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004149
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004150 /* Whenever we send anything different from a
4151 * HelloRequest we should be in a handshake - double check. */
4152 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4153 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004154 ssl->handshake == NULL )
4155 {
4156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4157 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4158 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004160#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004161 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004162 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004164 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4166 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004167 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004168#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004169
Hanno Beckerb50a2532018-08-06 11:52:54 +01004170 /* Double-check that we did not exceed the bounds
4171 * of the outgoing record buffer.
4172 * This should never fail as the various message
4173 * writing functions must obey the bounds of the
4174 * outgoing record buffer, but better be safe.
4175 *
4176 * Note: We deliberately do not check for the MTU or MFL here.
4177 */
4178 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4179 {
4180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4181 "size %u, maximum %u",
4182 (unsigned) ssl->out_msglen,
4183 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4184 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4185 }
4186
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004187 /*
4188 * Fill handshake headers
4189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004191 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004192 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4193 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4194 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004195
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004196 /*
4197 * DTLS has additional fields in the Handshake layer,
4198 * between the length field and the actual payload:
4199 * uint16 message_seq;
4200 * uint24 fragment_offset;
4201 * uint24 fragment_length;
4202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004204 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004205 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004206 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004207 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004208 {
4209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4210 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004211 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004212 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004213 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4214 }
4215
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004216 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004217 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004218
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004219 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004220 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004221 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004222 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4223 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4224 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004225 }
4226 else
4227 {
4228 ssl->out_msg[4] = 0;
4229 ssl->out_msg[5] = 0;
4230 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004231
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004232 /* Handshake hashes are computed without fragmentation,
4233 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004234 memset( ssl->out_msg + 6, 0x00, 3 );
4235 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004236 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004238
Hanno Becker0207e532018-08-28 10:28:28 +01004239 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004240 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4241 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004242 }
4243
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004244 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004246 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004247 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4248 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004249 {
4250 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004253 return( ret );
4254 }
4255 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004256 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004257#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004258 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004259 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004260 {
4261 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4262 return( ret );
4263 }
4264 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004265
4266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4267
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004268 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004269}
4270
4271/*
4272 * Record layer functions
4273 */
4274
4275/*
4276 * Write current record.
4277 *
4278 * Uses:
4279 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4280 * - ssl->out_msglen: length of the record content (excl headers)
4281 * - ssl->out_msg: record content
4282 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004283int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004284{
4285 int ret, done = 0;
4286 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004287 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004288
4289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004292 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004294 {
4295 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004298 return( ret );
4299 }
4300
4301 len = ssl->out_msglen;
4302 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4306 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310 ret = mbedtls_ssl_hw_record_write( ssl );
4311 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4314 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004315 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004316
4317 if( ret == 0 )
4318 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004319 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004321 if( !done )
4322 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004323 unsigned i;
4324 size_t protected_record_size;
4325
Hanno Becker6430faf2019-05-08 11:57:13 +01004326 /* Skip writing the record content type to after the encryption,
4327 * as it may change when using the CID extension. */
4328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004330 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004331
Hanno Becker19859472018-08-06 09:40:20 +01004332 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004333 ssl->out_len[0] = (unsigned char)( len >> 8 );
4334 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004335
Paul Bakker48916f92012-09-16 19:57:18 +00004336 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004337 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004338 mbedtls_record rec;
4339
4340 rec.buf = ssl->out_iv;
4341 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4342 ( ssl->out_iv - ssl->out_buf );
4343 rec.data_len = ssl->out_msglen;
4344 rec.data_offset = ssl->out_msg - rec.buf;
4345
4346 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4347 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4348 ssl->conf->transport, rec.ver );
4349 rec.type = ssl->out_msgtype;
4350
Hanno Beckera0e20d02019-05-15 14:03:01 +01004351#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004352 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004353 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004354#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004355
Hanno Beckera18d1322018-01-03 14:27:32 +00004356 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004357 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004360 return( ret );
4361 }
4362
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004363 if( rec.data_offset != 0 )
4364 {
4365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4366 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4367 }
4368
Hanno Becker6430faf2019-05-08 11:57:13 +01004369 /* Update the record content type and CID. */
4370 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004371#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004372 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004373#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004374 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004375 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4376 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004377 }
4378
Hanno Becker5903de42019-05-03 14:46:38 +01004379 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004380
4381#if defined(MBEDTLS_SSL_PROTO_DTLS)
4382 /* In case of DTLS, double-check that we don't exceed
4383 * the remaining space in the datagram. */
4384 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4385 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004386 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004387 if( ret < 0 )
4388 return( ret );
4389
4390 if( protected_record_size > (size_t) ret )
4391 {
4392 /* Should never happen */
4393 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4394 }
4395 }
4396#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004397
Hanno Becker6430faf2019-05-08 11:57:13 +01004398 /* Now write the potentially updated record content type. */
4399 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004402 "version = [%d:%d], msglen = %d",
4403 ssl->out_hdr[0], ssl->out_hdr[1],
4404 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004407 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004408
4409 ssl->out_left += protected_record_size;
4410 ssl->out_hdr += protected_record_size;
4411 ssl_update_out_pointers( ssl, ssl->transform_out );
4412
Hanno Becker04484622018-08-06 09:49:38 +01004413 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4414 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4415 break;
4416
4417 /* The loop goes to its end iff the counter is wrapping */
4418 if( i == ssl_ep_len( ssl ) )
4419 {
4420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4421 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4422 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004423 }
4424
Hanno Becker67bc7c32018-08-06 11:33:50 +01004425#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004426 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4427 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004428 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004429 size_t remaining;
4430 ret = ssl_get_remaining_payload_in_datagram( ssl );
4431 if( ret < 0 )
4432 {
4433 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4434 ret );
4435 return( ret );
4436 }
4437
4438 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004439 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004440 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004441 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004442 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004443 else
4444 {
Hanno Becker513815a2018-08-20 11:56:09 +01004445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004446 }
4447 }
4448#endif /* MBEDTLS_SSL_PROTO_DTLS */
4449
4450 if( ( flush == SSL_FORCE_FLUSH ) &&
4451 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004454 return( ret );
4455 }
4456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004458
4459 return( 0 );
4460}
4461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004463
4464static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4465{
4466 if( ssl->in_msglen < ssl->in_hslen ||
4467 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4468 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4469 {
4470 return( 1 );
4471 }
4472 return( 0 );
4473}
Hanno Becker44650b72018-08-16 12:51:11 +01004474
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004475static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004476{
4477 return( ( ssl->in_msg[9] << 16 ) |
4478 ( ssl->in_msg[10] << 8 ) |
4479 ssl->in_msg[11] );
4480}
4481
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004482static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004483{
4484 return( ( ssl->in_msg[6] << 16 ) |
4485 ( ssl->in_msg[7] << 8 ) |
4486 ssl->in_msg[8] );
4487}
4488
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004489static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004490{
4491 uint32_t msg_len, frag_off, frag_len;
4492
4493 msg_len = ssl_get_hs_total_len( ssl );
4494 frag_off = ssl_get_hs_frag_off( ssl );
4495 frag_len = ssl_get_hs_frag_len( ssl );
4496
4497 if( frag_off > msg_len )
4498 return( -1 );
4499
4500 if( frag_len > msg_len - frag_off )
4501 return( -1 );
4502
4503 if( frag_len + 12 > ssl->in_msglen )
4504 return( -1 );
4505
4506 return( 0 );
4507}
4508
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004509/*
4510 * Mark bits in bitmask (used for DTLS HS reassembly)
4511 */
4512static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4513{
4514 unsigned int start_bits, end_bits;
4515
4516 start_bits = 8 - ( offset % 8 );
4517 if( start_bits != 8 )
4518 {
4519 size_t first_byte_idx = offset / 8;
4520
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004521 /* Special case */
4522 if( len <= start_bits )
4523 {
4524 for( ; len != 0; len-- )
4525 mask[first_byte_idx] |= 1 << ( start_bits - len );
4526
4527 /* Avoid potential issues with offset or len becoming invalid */
4528 return;
4529 }
4530
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004531 offset += start_bits; /* Now offset % 8 == 0 */
4532 len -= start_bits;
4533
4534 for( ; start_bits != 0; start_bits-- )
4535 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4536 }
4537
4538 end_bits = len % 8;
4539 if( end_bits != 0 )
4540 {
4541 size_t last_byte_idx = ( offset + len ) / 8;
4542
4543 len -= end_bits; /* Now len % 8 == 0 */
4544
4545 for( ; end_bits != 0; end_bits-- )
4546 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4547 }
4548
4549 memset( mask + offset / 8, 0xFF, len / 8 );
4550}
4551
4552/*
4553 * Check that bitmask is full
4554 */
4555static int ssl_bitmask_check( unsigned char *mask, size_t len )
4556{
4557 size_t i;
4558
4559 for( i = 0; i < len / 8; i++ )
4560 if( mask[i] != 0xFF )
4561 return( -1 );
4562
4563 for( i = 0; i < len % 8; i++ )
4564 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4565 return( -1 );
4566
4567 return( 0 );
4568}
4569
Hanno Becker56e205e2018-08-16 09:06:12 +01004570/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004571static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004572 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004573{
Hanno Becker56e205e2018-08-16 09:06:12 +01004574 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004575
Hanno Becker56e205e2018-08-16 09:06:12 +01004576 alloc_len = 12; /* Handshake header */
4577 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004578
Hanno Beckerd07df862018-08-16 09:14:58 +01004579 if( add_bitmap )
4580 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004581
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004582 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004583}
Hanno Becker56e205e2018-08-16 09:06:12 +01004584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004585#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004586
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004587static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004588{
4589 return( ( ssl->in_msg[1] << 16 ) |
4590 ( ssl->in_msg[2] << 8 ) |
4591 ssl->in_msg[3] );
4592}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004593
Simon Butcher99000142016-10-13 17:21:01 +01004594int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004599 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004601 }
4602
Hanno Becker12555c62018-08-16 12:47:53 +01004603 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004606 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004607 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004610 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004611 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004612 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004613 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004614
Hanno Becker44650b72018-08-16 12:51:11 +01004615 if( ssl_check_hs_header( ssl ) != 0 )
4616 {
4617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4618 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4619 }
4620
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004621 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004622 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4623 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4624 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4625 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004626 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004627 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4628 {
4629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4630 recv_msg_seq,
4631 ssl->handshake->in_msg_seq ) );
4632 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4633 }
4634
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004635 /* Retransmit only on last message from previous flight, to avoid
4636 * too many retransmissions.
4637 * Besides, No sane server ever retransmits HelloVerifyRequest */
4638 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004642 "message_seq = %d, start_of_flight = %d",
4643 recv_msg_seq,
4644 ssl->handshake->in_flight_start_seq ) );
4645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004649 return( ret );
4650 }
4651 }
4652 else
4653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004655 "message_seq = %d, expected = %d",
4656 recv_msg_seq,
4657 ssl->handshake->in_msg_seq ) );
4658 }
4659
Hanno Becker90333da2017-10-10 11:27:13 +01004660 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004661 }
4662 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004663
Hanno Becker6d97ef52018-08-16 13:09:04 +01004664 /* Message reassembly is handled alongside buffering of future
4665 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004666 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004667 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004668 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004671 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004672 }
4673 }
4674 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004676 /* With TLS we don't handle fragmentation (for now) */
4677 if( ssl->in_msglen < ssl->in_hslen )
4678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4680 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004681 }
4682
Simon Butcher99000142016-10-13 17:21:01 +01004683 return( 0 );
4684}
4685
4686void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4687{
Hanno Becker0271f962018-08-16 13:23:47 +01004688 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004689
Hanno Becker0271f962018-08-16 13:23:47 +01004690 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004691 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004692 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004693 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004694
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004695 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004697 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004698 ssl->handshake != NULL )
4699 {
Hanno Becker0271f962018-08-16 13:23:47 +01004700 unsigned offset;
4701 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004702
Hanno Becker0271f962018-08-16 13:23:47 +01004703 /* Increment handshake sequence number */
4704 hs->in_msg_seq++;
4705
4706 /*
4707 * Clear up handshake buffering and reassembly structure.
4708 */
4709
4710 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004711 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004712
4713 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004714 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4715 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004716 offset++, hs_buf++ )
4717 {
4718 *hs_buf = *(hs_buf + 1);
4719 }
4720
4721 /* Create a fresh last entry */
4722 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004723 }
4724#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004725}
4726
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004727/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004728 * DTLS anti-replay: RFC 6347 4.1.2.6
4729 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004730 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4731 * Bit n is set iff record number in_window_top - n has been seen.
4732 *
4733 * Usually, in_window_top is the last record number seen and the lsb of
4734 * in_window is set. The only exception is the initial state (record number 0
4735 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004737#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4738static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004739{
4740 ssl->in_window_top = 0;
4741 ssl->in_window = 0;
4742}
4743
4744static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4745{
4746 return( ( (uint64_t) buf[0] << 40 ) |
4747 ( (uint64_t) buf[1] << 32 ) |
4748 ( (uint64_t) buf[2] << 24 ) |
4749 ( (uint64_t) buf[3] << 16 ) |
4750 ( (uint64_t) buf[4] << 8 ) |
4751 ( (uint64_t) buf[5] ) );
4752}
4753
4754/*
4755 * Return 0 if sequence number is acceptable, -1 otherwise
4756 */
Hanno Becker0183d692019-07-12 08:50:37 +01004757int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004758{
4759 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4760 uint64_t bit;
4761
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004762 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004763 return( 0 );
4764
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004765 if( rec_seqnum > ssl->in_window_top )
4766 return( 0 );
4767
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004768 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004769
4770 if( bit >= 64 )
4771 return( -1 );
4772
4773 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4774 return( -1 );
4775
4776 return( 0 );
4777}
4778
4779/*
4780 * Update replay window on new validated record
4781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004783{
4784 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4785
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004786 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004787 return;
4788
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004789 if( rec_seqnum > ssl->in_window_top )
4790 {
4791 /* Update window_top and the contents of the window */
4792 uint64_t shift = rec_seqnum - ssl->in_window_top;
4793
4794 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004795 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004796 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004797 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004798 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004799 ssl->in_window |= 1;
4800 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004801
4802 ssl->in_window_top = rec_seqnum;
4803 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004804 else
4805 {
4806 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004807 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004808
4809 if( bit < 64 ) /* Always true, but be extra sure */
4810 ssl->in_window |= (uint64_t) 1 << bit;
4811 }
4812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004814
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004815#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004816/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004817static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4818
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004819/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004820 * Without any SSL context, check if a datagram looks like a ClientHello with
4821 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004822 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004823 *
4824 * - if cookie is valid, return 0
4825 * - if ClientHello looks superficially valid but cookie is not,
4826 * fill obuf and set olen, then
4827 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4828 * - otherwise return a specific error code
4829 */
4830static int ssl_check_dtls_clihlo_cookie(
4831 mbedtls_ssl_cookie_write_t *f_cookie_write,
4832 mbedtls_ssl_cookie_check_t *f_cookie_check,
4833 void *p_cookie,
4834 const unsigned char *cli_id, size_t cli_id_len,
4835 const unsigned char *in, size_t in_len,
4836 unsigned char *obuf, size_t buf_len, size_t *olen )
4837{
4838 size_t sid_len, cookie_len;
4839 unsigned char *p;
4840
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004841 /*
4842 * Structure of ClientHello with record and handshake headers,
4843 * and expected values. We don't need to check a lot, more checks will be
4844 * done when actually parsing the ClientHello - skipping those checks
4845 * avoids code duplication and does not make cookie forging any easier.
4846 *
4847 * 0-0 ContentType type; copied, must be handshake
4848 * 1-2 ProtocolVersion version; copied
4849 * 3-4 uint16 epoch; copied, must be 0
4850 * 5-10 uint48 sequence_number; copied
4851 * 11-12 uint16 length; (ignored)
4852 *
4853 * 13-13 HandshakeType msg_type; (ignored)
4854 * 14-16 uint24 length; (ignored)
4855 * 17-18 uint16 message_seq; copied
4856 * 19-21 uint24 fragment_offset; copied, must be 0
4857 * 22-24 uint24 fragment_length; (ignored)
4858 *
4859 * 25-26 ProtocolVersion client_version; (ignored)
4860 * 27-58 Random random; (ignored)
4861 * 59-xx SessionID session_id; 1 byte len + sid_len content
4862 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4863 * ...
4864 *
4865 * Minimum length is 61 bytes.
4866 */
4867 if( in_len < 61 ||
4868 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4869 in[3] != 0 || in[4] != 0 ||
4870 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4871 {
4872 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4873 }
4874
4875 sid_len = in[59];
4876 if( sid_len > in_len - 61 )
4877 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4878
4879 cookie_len = in[60 + sid_len];
4880 if( cookie_len > in_len - 60 )
4881 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4882
4883 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4884 cli_id, cli_id_len ) == 0 )
4885 {
4886 /* Valid cookie */
4887 return( 0 );
4888 }
4889
4890 /*
4891 * If we get here, we've got an invalid cookie, let's prepare HVR.
4892 *
4893 * 0-0 ContentType type; copied
4894 * 1-2 ProtocolVersion version; copied
4895 * 3-4 uint16 epoch; copied
4896 * 5-10 uint48 sequence_number; copied
4897 * 11-12 uint16 length; olen - 13
4898 *
4899 * 13-13 HandshakeType msg_type; hello_verify_request
4900 * 14-16 uint24 length; olen - 25
4901 * 17-18 uint16 message_seq; copied
4902 * 19-21 uint24 fragment_offset; copied
4903 * 22-24 uint24 fragment_length; olen - 25
4904 *
4905 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4906 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4907 *
4908 * Minimum length is 28.
4909 */
4910 if( buf_len < 28 )
4911 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4912
4913 /* Copy most fields and adapt others */
4914 memcpy( obuf, in, 25 );
4915 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4916 obuf[25] = 0xfe;
4917 obuf[26] = 0xff;
4918
4919 /* Generate and write actual cookie */
4920 p = obuf + 28;
4921 if( f_cookie_write( p_cookie,
4922 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4923 {
4924 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4925 }
4926
4927 *olen = p - obuf;
4928
4929 /* Go back and fill length fields */
4930 obuf[27] = (unsigned char)( *olen - 28 );
4931
4932 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4933 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4934 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4935
4936 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4937 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4938
4939 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4940}
4941
4942/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004943 * Handle possible client reconnect with the same UDP quadruplet
4944 * (RFC 6347 Section 4.2.8).
4945 *
4946 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4947 * that looks like a ClientHello.
4948 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004949 * - if the input looks like a ClientHello without cookies,
4950 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004951 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004952 * - if the input looks like a ClientHello with a valid cookie,
4953 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004954 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004955 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004956 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004957 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004958 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4959 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004960 */
4961static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4962{
4963 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004964 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004965
Hanno Becker2fddd372019-07-10 14:37:41 +01004966 if( ssl->conf->f_cookie_write == NULL ||
4967 ssl->conf->f_cookie_check == NULL )
4968 {
4969 /* If we can't use cookies to verify reachability of the peer,
4970 * drop the record. */
4971 return( 0 );
4972 }
4973
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004974 ret = ssl_check_dtls_clihlo_cookie(
4975 ssl->conf->f_cookie_write,
4976 ssl->conf->f_cookie_check,
4977 ssl->conf->p_cookie,
4978 ssl->cli_id, ssl->cli_id_len,
4979 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004980 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004981
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004982 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4983
4984 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004985 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004986 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004987 * If the error is permanent we'll catch it later,
4988 * if it's not, then hopefully it'll work next time. */
4989 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004990 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004991 }
4992
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004993 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004994 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004995 /* Got a valid cookie, partially reset context */
4996 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4997 {
4998 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4999 return( ret );
5000 }
5001
5002 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005003 }
5004
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005005 return( ret );
5006}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005007#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005008
Hanno Beckerf661c9c2019-05-03 13:25:54 +01005009static int ssl_check_record_type( uint8_t record_type )
5010{
5011 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5012 record_type != MBEDTLS_SSL_MSG_ALERT &&
5013 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5014 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5015 {
5016 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5017 }
5018
5019 return( 0 );
5020}
5021
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005022/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005023 * ContentType type;
5024 * ProtocolVersion version;
5025 * uint16 epoch; // DTLS only
5026 * uint48 sequence_number; // DTLS only
5027 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005028 *
5029 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005030 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005031 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5032 *
5033 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005034 * 1. proceed with the record if this function returns 0
5035 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5036 * 3. return CLIENT_RECONNECT if this function return that value
5037 * 4. drop the whole datagram if this function returns anything else.
5038 * Point 2 is needed when the peer is resending, and we have already received
5039 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005040 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005041static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005042 unsigned char *buf,
5043 size_t len,
5044 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005045{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005046 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005047
Hanno Beckere5e7e782019-07-11 12:29:35 +01005048 size_t const rec_hdr_type_offset = 0;
5049 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005050
Hanno Beckere5e7e782019-07-11 12:29:35 +01005051 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5052 rec_hdr_type_len;
5053 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005054
Hanno Beckere5e7e782019-07-11 12:29:35 +01005055 size_t const rec_hdr_ctr_len = 8;
5056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005057 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005058 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5059 rec_hdr_version_len;
5060
Hanno Beckera0e20d02019-05-15 14:03:01 +01005061#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005062 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5063 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005064 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005065#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5066#endif /* MBEDTLS_SSL_PROTO_DTLS */
5067
5068 size_t rec_hdr_len_offset; /* To be determined */
5069 size_t const rec_hdr_len_len = 2;
5070
5071 /*
5072 * Check minimum lengths for record header.
5073 */
5074
5075#if defined(MBEDTLS_SSL_PROTO_DTLS)
5076 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5077 {
5078 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5079 }
5080 else
5081#endif /* MBEDTLS_SSL_PROTO_DTLS */
5082 {
5083 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5084 }
5085
5086 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5087 {
5088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5089 (unsigned) len,
5090 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5091 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5092 }
5093
5094 /*
5095 * Parse and validate record content type
5096 */
5097
5098 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005099
5100 /* Check record content type */
5101#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5102 rec->cid_len = 0;
5103
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005104 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005105 ssl->conf->cid_len != 0 &&
5106 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005107 {
5108 /* Shift pointers to account for record header including CID
5109 * struct {
5110 * ContentType special_type = tls12_cid;
5111 * ProtocolVersion version;
5112 * uint16 epoch;
5113 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005114 * opaque cid[cid_length]; // Additional field compared to
5115 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005116 * uint16 length;
5117 * opaque enc_content[DTLSCiphertext.length];
5118 * } DTLSCiphertext;
5119 */
5120
5121 /* So far, we only support static CID lengths
5122 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005123 rec_hdr_cid_len = ssl->conf->cid_len;
5124 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005125
Hanno Beckere5e7e782019-07-11 12:29:35 +01005126 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005127 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5129 (unsigned) len,
5130 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005131 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005132 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005133
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005134 /* configured CID len is guaranteed at most 255, see
5135 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5136 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005137 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005138 }
5139 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005140#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005141 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005142 if( ssl_check_record_type( rec->type ) )
5143 {
Hanno Becker54229812019-07-12 14:40:00 +01005144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5145 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005146 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5147 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005148 }
5149
Hanno Beckere5e7e782019-07-11 12:29:35 +01005150 /*
5151 * Parse and validate record version
5152 */
5153
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005154 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5155 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005156 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5157 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005158 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005159
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005160 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5163 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005164 }
5165
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005166 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5169 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005170 }
5171
Hanno Beckere5e7e782019-07-11 12:29:35 +01005172 /*
5173 * Parse/Copy record sequence number.
5174 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005175
Hanno Beckere5e7e782019-07-11 12:29:35 +01005176#if defined(MBEDTLS_SSL_PROTO_DTLS)
5177 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005178 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005179 /* Copy explicit record sequence number from input buffer. */
5180 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5181 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005182 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005183 else
5184#endif /* MBEDTLS_SSL_PROTO_DTLS */
5185 {
5186 /* Copy implicit record sequence number from SSL context structure. */
5187 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5188 }
Paul Bakker40e46942009-01-03 21:51:57 +00005189
Hanno Beckere5e7e782019-07-11 12:29:35 +01005190 /*
5191 * Parse record length.
5192 */
5193
Hanno Beckere5e7e782019-07-11 12:29:35 +01005194 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005195 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5196 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005197 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005198
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005200 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005201 rec->type,
5202 major_ver, minor_ver, rec->data_len ) );
5203
5204 rec->buf = buf;
5205 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005206
Hanno Beckerd417cc92019-07-26 08:20:27 +01005207 if( rec->data_len == 0 )
5208 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005209
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005210 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005211 * DTLS-related tests.
5212 * Check epoch before checking length constraint because
5213 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5214 * message gets duplicated before the corresponding Finished message,
5215 * the second ChangeCipherSpec should be discarded because it belongs
5216 * to an old epoch, but not because its length is shorter than
5217 * the minimum record length for packets using the new record transform.
5218 * Note that these two kinds of failures are handled differently,
5219 * as an unexpected record is silently skipped but an invalid
5220 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005221 */
5222#if defined(MBEDTLS_SSL_PROTO_DTLS)
5223 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5224 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005225 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005226
Hanno Becker955a5c92019-07-10 17:12:07 +01005227 /* Check that the datagram is large enough to contain a record
5228 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005229 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005230 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5232 (unsigned) len,
5233 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005234 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5235 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005236
Hanno Becker37cfe732019-07-10 17:20:01 +01005237 /* Records from other, non-matching epochs are silently discarded.
5238 * (The case of same-port Client reconnects must be considered in
5239 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005240 if( rec_epoch != ssl->in_epoch )
5241 {
5242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5243 "expected %d, received %d",
5244 ssl->in_epoch, rec_epoch ) );
5245
Hanno Becker552f7472019-07-19 10:59:12 +01005246 /* Records from the next epoch are considered for buffering
5247 * (concretely: early Finished messages). */
5248 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005249 {
Hanno Becker552f7472019-07-19 10:59:12 +01005250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5251 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005252 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005253
Hanno Becker2fddd372019-07-10 14:37:41 +01005254 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005255 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005256#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005257 /* For records from the correct epoch, check whether their
5258 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005259 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005260 {
5261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5262 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5263 }
5264#endif
5265 }
5266#endif /* MBEDTLS_SSL_PROTO_DTLS */
5267
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005268 return( 0 );
5269}
Paul Bakker5121ce52009-01-03 21:22:43 +00005270
Paul Bakker5121ce52009-01-03 21:22:43 +00005271
Hanno Becker2fddd372019-07-10 14:37:41 +01005272#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5273static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5274{
5275 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5276
5277 /*
5278 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5279 * access the first byte of record content (handshake type), as we
5280 * have an active transform (possibly iv_len != 0), so use the
5281 * fact that the record header len is 13 instead.
5282 */
5283 if( rec_epoch == 0 &&
5284 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5285 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5286 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5287 ssl->in_left > 13 &&
5288 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5289 {
5290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5291 "from the same port" ) );
5292 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005293 }
5294
5295 return( 0 );
5296}
Hanno Becker2fddd372019-07-10 14:37:41 +01005297#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005298
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005299/*
5300 * If applicable, decrypt (and decompress) record content
5301 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005302static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5303 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005304{
5305 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005308 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005310#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5311 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315 ret = mbedtls_ssl_hw_record_read( ssl );
5316 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5319 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005320 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005321
5322 if( ret == 0 )
5323 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005326 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005327 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005328 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005329
Hanno Beckera18d1322018-01-03 14:27:32 +00005330 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005331 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005334
Hanno Beckera0e20d02019-05-15 14:03:01 +01005335#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005336 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5337 ssl->conf->ignore_unexpected_cid
5338 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5339 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005341 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005342 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005343#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005344
Paul Bakker5121ce52009-01-03 21:22:43 +00005345 return( ret );
5346 }
5347
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005348 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005349 {
5350 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005351 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005352 }
5353
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005354 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005355 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005356
Hanno Beckera0e20d02019-05-15 14:03:01 +01005357#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005358 /* We have already checked the record content type
5359 * in ssl_parse_record_header(), failing or silently
5360 * dropping the record in the case of an unknown type.
5361 *
5362 * Since with the use of CIDs, the record content type
5363 * might change during decryption, re-check the record
5364 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005365 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005366 {
5367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5368 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5369 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005370#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005371
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005372 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005373 {
5374#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5375 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005376 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005377 {
5378 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5380 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5381 }
5382#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5383
5384 ssl->nb_zero++;
5385
5386 /*
5387 * Three or more empty messages may be a DoS attack
5388 * (excessive CPU consumption).
5389 */
5390 if( ssl->nb_zero > 3 )
5391 {
5392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005393 "messages, possible DoS attack" ) );
5394 /* Treat the records as if they were not properly authenticated,
5395 * thereby failing the connection if we see more than allowed
5396 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005397 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5398 }
5399 }
5400 else
5401 ssl->nb_zero = 0;
5402
5403#if defined(MBEDTLS_SSL_PROTO_DTLS)
5404 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5405 {
5406 ; /* in_ctr read from peer, not maintained internally */
5407 }
5408 else
5409#endif
5410 {
5411 unsigned i;
5412 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5413 if( ++ssl->in_ctr[i - 1] != 0 )
5414 break;
5415
5416 /* The loop goes to its end iff the counter is wrapping */
5417 if( i == ssl_ep_len( ssl ) )
5418 {
5419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5420 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5421 }
5422 }
5423
Paul Bakker5121ce52009-01-03 21:22:43 +00005424 }
5425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005427 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005429 {
5430 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005433 return( ret );
5434 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005435 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005439 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005442 }
5443#endif
5444
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005445 /* Check actual (decrypted) record content length against
5446 * configured maximum. */
5447 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5448 {
5449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5450 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5451 }
5452
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005453 return( 0 );
5454}
5455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005456static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005457
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005458/*
5459 * Read a record.
5460 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005461 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5462 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5463 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005464 */
Hanno Becker1097b342018-08-15 14:09:41 +01005465
5466/* Helper functions for mbedtls_ssl_read_record(). */
5467static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005468static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5469static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005470
Hanno Becker327c93b2018-08-15 13:56:18 +01005471int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005472 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005473{
5474 int ret;
5475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005477
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005478 if( ssl->keep_current_message == 0 )
5479 {
5480 do {
Simon Butcher99000142016-10-13 17:21:01 +01005481
Hanno Becker26994592018-08-15 14:14:59 +01005482 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005483 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005484 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005485
Hanno Beckere74d5562018-08-15 14:26:08 +01005486 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005487 {
Hanno Becker40f50842018-08-15 14:48:01 +01005488#if defined(MBEDTLS_SSL_PROTO_DTLS)
5489 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005490
Hanno Becker40f50842018-08-15 14:48:01 +01005491 /* We only check for buffered messages if the
5492 * current datagram is fully consumed. */
5493 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005494 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005495 {
Hanno Becker40f50842018-08-15 14:48:01 +01005496 if( ssl_load_buffered_message( ssl ) == 0 )
5497 have_buffered = 1;
5498 }
5499
5500 if( have_buffered == 0 )
5501#endif /* MBEDTLS_SSL_PROTO_DTLS */
5502 {
5503 ret = ssl_get_next_record( ssl );
5504 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5505 continue;
5506
5507 if( ret != 0 )
5508 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005509 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005510 return( ret );
5511 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005512 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005513 }
5514
5515 ret = mbedtls_ssl_handle_message_type( ssl );
5516
Hanno Becker40f50842018-08-15 14:48:01 +01005517#if defined(MBEDTLS_SSL_PROTO_DTLS)
5518 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5519 {
5520 /* Buffer future message */
5521 ret = ssl_buffer_message( ssl );
5522 if( ret != 0 )
5523 return( ret );
5524
5525 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5526 }
5527#endif /* MBEDTLS_SSL_PROTO_DTLS */
5528
Hanno Becker90333da2017-10-10 11:27:13 +01005529 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5530 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005531
5532 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005533 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005534 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005535 return( ret );
5536 }
5537
Hanno Becker327c93b2018-08-15 13:56:18 +01005538 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005539 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005540 {
5541 mbedtls_ssl_update_handshake_status( ssl );
5542 }
Simon Butcher99000142016-10-13 17:21:01 +01005543 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005544 else
Simon Butcher99000142016-10-13 17:21:01 +01005545 {
Hanno Becker02f59072018-08-15 14:00:24 +01005546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005547 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005548 }
5549
5550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5551
5552 return( 0 );
5553}
5554
Hanno Becker40f50842018-08-15 14:48:01 +01005555#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005556static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005557{
Hanno Becker40f50842018-08-15 14:48:01 +01005558 if( ssl->in_left > ssl->next_record_offset )
5559 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005560
Hanno Becker40f50842018-08-15 14:48:01 +01005561 return( 0 );
5562}
5563
5564static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5565{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005566 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005567 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005568 int ret = 0;
5569
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005570 if( hs == NULL )
5571 return( -1 );
5572
Hanno Beckere00ae372018-08-20 09:39:42 +01005573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5574
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005575 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5576 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5577 {
5578 /* Check if we have seen a ChangeCipherSpec before.
5579 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005580 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005581 {
5582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5583 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005584 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005585 }
5586
Hanno Becker39b8bc92018-08-28 17:17:13 +01005587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005588 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5589 ssl->in_msglen = 1;
5590 ssl->in_msg[0] = 1;
5591
5592 /* As long as they are equal, the exact value doesn't matter. */
5593 ssl->in_left = 0;
5594 ssl->next_record_offset = 0;
5595
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005596 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005597 goto exit;
5598 }
Hanno Becker37f95322018-08-16 13:55:32 +01005599
Hanno Beckerb8f50142018-08-28 10:01:34 +01005600#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005601 /* Debug only */
5602 {
5603 unsigned offset;
5604 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5605 {
5606 hs_buf = &hs->buffering.hs[offset];
5607 if( hs_buf->is_valid == 1 )
5608 {
5609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5610 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005611 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005612 }
5613 }
5614 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005615#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005616
5617 /* Check if we have buffered and/or fully reassembled the
5618 * next handshake message. */
5619 hs_buf = &hs->buffering.hs[0];
5620 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5621 {
5622 /* Synthesize a record containing the buffered HS message. */
5623 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5624 ( hs_buf->data[2] << 8 ) |
5625 hs_buf->data[3];
5626
5627 /* Double-check that we haven't accidentally buffered
5628 * a message that doesn't fit into the input buffer. */
5629 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5630 {
5631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5632 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5633 }
5634
5635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5636 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5637 hs_buf->data, msg_len + 12 );
5638
5639 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5640 ssl->in_hslen = msg_len + 12;
5641 ssl->in_msglen = msg_len + 12;
5642 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5643
5644 ret = 0;
5645 goto exit;
5646 }
5647 else
5648 {
5649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5650 hs->in_msg_seq ) );
5651 }
5652
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005653 ret = -1;
5654
5655exit:
5656
5657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5658 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005659}
5660
Hanno Beckera02b0b42018-08-21 17:20:27 +01005661static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5662 size_t desired )
5663{
5664 int offset;
5665 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5667 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005668
Hanno Becker01315ea2018-08-21 17:22:17 +01005669 /* Get rid of future records epoch first, if such exist. */
5670 ssl_free_buffered_record( ssl );
5671
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 future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005677 return( 0 );
5678 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005679
Hanno Becker4f432ad2018-08-28 10:02:32 +01005680 /* We don't have enough space to buffer the next expected handshake
5681 * message. Remove buffers used for future messages to gain space,
5682 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005683 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5684 offset >= 0; offset-- )
5685 {
5686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5687 offset ) );
5688
Hanno Beckerb309b922018-08-23 13:18:05 +01005689 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005690
5691 /* Check if we have enough space available now. */
5692 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5693 hs->buffering.total_bytes_buffered ) )
5694 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005696 return( 0 );
5697 }
5698 }
5699
5700 return( -1 );
5701}
5702
Hanno Becker40f50842018-08-15 14:48:01 +01005703static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5704{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005705 int ret = 0;
5706 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5707
5708 if( hs == NULL )
5709 return( 0 );
5710
5711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5712
5713 switch( ssl->in_msgtype )
5714 {
5715 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005717
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005718 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005719 break;
5720
5721 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005722 {
5723 unsigned recv_msg_seq_offset;
5724 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5725 mbedtls_ssl_hs_buffer *hs_buf;
5726 size_t msg_len = ssl->in_hslen - 12;
5727
5728 /* We should never receive an old handshake
5729 * message - double-check nonetheless. */
5730 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5731 {
5732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5733 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5734 }
5735
5736 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5737 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5738 {
5739 /* Silently ignore -- message too far in the future */
5740 MBEDTLS_SSL_DEBUG_MSG( 2,
5741 ( "Ignore future HS message with sequence number %u, "
5742 "buffering window %u - %u",
5743 recv_msg_seq, ssl->handshake->in_msg_seq,
5744 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5745
5746 goto exit;
5747 }
5748
5749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5750 recv_msg_seq, recv_msg_seq_offset ) );
5751
5752 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5753
5754 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005755 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005756 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005757 size_t reassembly_buf_sz;
5758
Hanno Becker37f95322018-08-16 13:55:32 +01005759 hs_buf->is_fragmented =
5760 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5761
5762 /* We copy the message back into the input buffer
5763 * after reassembly, so check that it's not too large.
5764 * This is an implementation-specific limitation
5765 * and not one from the standard, hence it is not
5766 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005767 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005768 {
5769 /* Ignore message */
5770 goto exit;
5771 }
5772
Hanno Beckere0b150f2018-08-21 15:51:03 +01005773 /* Check if we have enough space to buffer the message. */
5774 if( hs->buffering.total_bytes_buffered >
5775 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5776 {
5777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5779 }
5780
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005781 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5782 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005783
5784 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5785 hs->buffering.total_bytes_buffered ) )
5786 {
5787 if( recv_msg_seq_offset > 0 )
5788 {
5789 /* If we can't buffer a future message because
5790 * of space limitations -- ignore. */
5791 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",
5792 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5793 (unsigned) hs->buffering.total_bytes_buffered ) );
5794 goto exit;
5795 }
Hanno Beckere1801392018-08-21 16:51:05 +01005796 else
5797 {
5798 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",
5799 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5800 (unsigned) hs->buffering.total_bytes_buffered ) );
5801 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005802
Hanno Beckera02b0b42018-08-21 17:20:27 +01005803 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005804 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005805 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",
5806 (unsigned) msg_len,
5807 (unsigned) reassembly_buf_sz,
5808 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005809 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005810 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5811 goto exit;
5812 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005813 }
5814
5815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5816 msg_len ) );
5817
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005818 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5819 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005820 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005821 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005822 goto exit;
5823 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005824 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005825
5826 /* Prepare final header: copy msg_type, length and message_seq,
5827 * then add standardised fragment_offset and fragment_length */
5828 memcpy( hs_buf->data, ssl->in_msg, 6 );
5829 memset( hs_buf->data + 6, 0, 3 );
5830 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5831
5832 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005833
5834 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005835 }
5836 else
5837 {
5838 /* Make sure msg_type and length are consistent */
5839 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5840 {
5841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5842 /* Ignore */
5843 goto exit;
5844 }
5845 }
5846
Hanno Becker4422bbb2018-08-20 09:40:19 +01005847 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005848 {
5849 size_t frag_len, frag_off;
5850 unsigned char * const msg = hs_buf->data + 12;
5851
5852 /*
5853 * Check and copy current fragment
5854 */
5855
5856 /* Validation of header fields already done in
5857 * mbedtls_ssl_prepare_handshake_record(). */
5858 frag_off = ssl_get_hs_frag_off( ssl );
5859 frag_len = ssl_get_hs_frag_len( ssl );
5860
5861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5862 frag_off, frag_len ) );
5863 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5864
5865 if( hs_buf->is_fragmented )
5866 {
5867 unsigned char * const bitmask = msg + msg_len;
5868 ssl_bitmask_set( bitmask, frag_off, frag_len );
5869 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5870 msg_len ) == 0 );
5871 }
5872 else
5873 {
5874 hs_buf->is_complete = 1;
5875 }
5876
5877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5878 hs_buf->is_complete ? "" : "not yet " ) );
5879 }
5880
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005881 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005882 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005883
5884 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005885 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005886 break;
5887 }
5888
5889exit:
5890
5891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5892 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005893}
5894#endif /* MBEDTLS_SSL_PROTO_DTLS */
5895
Hanno Becker1097b342018-08-15 14:09:41 +01005896static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005897{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005898 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005899 * Consume last content-layer message and potentially
5900 * update in_msglen which keeps track of the contents'
5901 * consumption state.
5902 *
5903 * (1) Handshake messages:
5904 * Remove last handshake message, move content
5905 * and adapt in_msglen.
5906 *
5907 * (2) Alert messages:
5908 * Consume whole record content, in_msglen = 0.
5909 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005910 * (3) Change cipher spec:
5911 * Consume whole record content, in_msglen = 0.
5912 *
5913 * (4) Application data:
5914 * Don't do anything - the record layer provides
5915 * the application data as a stream transport
5916 * and consumes through mbedtls_ssl_read only.
5917 *
5918 */
5919
5920 /* Case (1): Handshake messages */
5921 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005922 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005923 /* Hard assertion to be sure that no application data
5924 * is in flight, as corrupting ssl->in_msglen during
5925 * ssl->in_offt != NULL is fatal. */
5926 if( ssl->in_offt != NULL )
5927 {
5928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5930 }
5931
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005932 /*
5933 * Get next Handshake message in the current record
5934 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005935
Hanno Becker4a810fb2017-05-24 16:27:30 +01005936 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005937 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005938 * current handshake content: If DTLS handshake
5939 * fragmentation is used, that's the fragment
5940 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005941 * size here is faulty and should be changed at
5942 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005943 * (2) While it doesn't seem to cause problems, one
5944 * has to be very careful not to assume that in_hslen
5945 * is always <= in_msglen in a sensible communication.
5946 * Again, it's wrong for DTLS handshake fragmentation.
5947 * The following check is therefore mandatory, and
5948 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005949 * Additionally, ssl->in_hslen might be arbitrarily out of
5950 * bounds after handling a DTLS message with an unexpected
5951 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005952 */
5953 if( ssl->in_hslen < ssl->in_msglen )
5954 {
5955 ssl->in_msglen -= ssl->in_hslen;
5956 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5957 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005958
Hanno Becker4a810fb2017-05-24 16:27:30 +01005959 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5960 ssl->in_msg, ssl->in_msglen );
5961 }
5962 else
5963 {
5964 ssl->in_msglen = 0;
5965 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005966
Hanno Becker4a810fb2017-05-24 16:27:30 +01005967 ssl->in_hslen = 0;
5968 }
5969 /* Case (4): Application data */
5970 else if( ssl->in_offt != NULL )
5971 {
5972 return( 0 );
5973 }
5974 /* Everything else (CCS & Alerts) */
5975 else
5976 {
5977 ssl->in_msglen = 0;
5978 }
5979
Hanno Becker1097b342018-08-15 14:09:41 +01005980 return( 0 );
5981}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005982
Hanno Beckere74d5562018-08-15 14:26:08 +01005983static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5984{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005985 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005986 return( 1 );
5987
5988 return( 0 );
5989}
5990
Hanno Becker5f066e72018-08-16 14:56:31 +01005991#if defined(MBEDTLS_SSL_PROTO_DTLS)
5992
5993static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5994{
5995 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5996 if( hs == NULL )
5997 return;
5998
Hanno Becker01315ea2018-08-21 17:22:17 +01005999 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006000 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006001 hs->buffering.total_bytes_buffered -=
6002 hs->buffering.future_record.len;
6003
6004 mbedtls_free( hs->buffering.future_record.data );
6005 hs->buffering.future_record.data = NULL;
6006 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006007}
6008
6009static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6010{
6011 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6012 unsigned char * rec;
6013 size_t rec_len;
6014 unsigned rec_epoch;
6015
6016 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6017 return( 0 );
6018
6019 if( hs == NULL )
6020 return( 0 );
6021
Hanno Becker5f066e72018-08-16 14:56:31 +01006022 rec = hs->buffering.future_record.data;
6023 rec_len = hs->buffering.future_record.len;
6024 rec_epoch = hs->buffering.future_record.epoch;
6025
6026 if( rec == NULL )
6027 return( 0 );
6028
Hanno Becker4cb782d2018-08-20 11:19:05 +01006029 /* Only consider loading future records if the
6030 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006031 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006032 return( 0 );
6033
Hanno Becker5f066e72018-08-16 14:56:31 +01006034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6035
6036 if( rec_epoch != ssl->in_epoch )
6037 {
6038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6039 goto exit;
6040 }
6041
6042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6043
6044 /* Double-check that the record is not too large */
6045 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6046 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6047 {
6048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6049 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6050 }
6051
6052 memcpy( ssl->in_hdr, rec, rec_len );
6053 ssl->in_left = rec_len;
6054 ssl->next_record_offset = 0;
6055
6056 ssl_free_buffered_record( ssl );
6057
6058exit:
6059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6060 return( 0 );
6061}
6062
Hanno Becker519f15d2019-07-11 12:43:20 +01006063static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6064 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006065{
6066 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006067
6068 /* Don't buffer future records outside handshakes. */
6069 if( hs == NULL )
6070 return( 0 );
6071
6072 /* Only buffer handshake records (we are only interested
6073 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006074 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006075 return( 0 );
6076
6077 /* Don't buffer more than one future epoch record. */
6078 if( hs->buffering.future_record.data != NULL )
6079 return( 0 );
6080
Hanno Becker01315ea2018-08-21 17:22:17 +01006081 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006082 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006083 hs->buffering.total_bytes_buffered ) )
6084 {
6085 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 +01006086 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006087 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006088 return( 0 );
6089 }
6090
Hanno Becker5f066e72018-08-16 14:56:31 +01006091 /* Buffer record */
6092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6093 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006094 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006095
6096 /* ssl_parse_record_header() only considers records
6097 * of the next epoch as candidates for buffering. */
6098 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006099 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006100
6101 hs->buffering.future_record.data =
6102 mbedtls_calloc( 1, hs->buffering.future_record.len );
6103 if( hs->buffering.future_record.data == NULL )
6104 {
6105 /* If we run out of RAM trying to buffer a
6106 * record from the next epoch, just ignore. */
6107 return( 0 );
6108 }
6109
Hanno Becker519f15d2019-07-11 12:43:20 +01006110 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006111
Hanno Becker519f15d2019-07-11 12:43:20 +01006112 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006113 return( 0 );
6114}
6115
6116#endif /* MBEDTLS_SSL_PROTO_DTLS */
6117
Hanno Beckere74d5562018-08-15 14:26:08 +01006118static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006119{
6120 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006121 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006122
Hanno Becker5f066e72018-08-16 14:56:31 +01006123#if defined(MBEDTLS_SSL_PROTO_DTLS)
6124 /* We might have buffered a future record; if so,
6125 * and if the epoch matches now, load it.
6126 * On success, this call will set ssl->in_left to
6127 * the length of the buffered record, so that
6128 * the calls to ssl_fetch_input() below will
6129 * essentially be no-ops. */
6130 ret = ssl_load_buffered_record( ssl );
6131 if( ret != 0 )
6132 return( ret );
6133#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006134
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006135 /* Ensure that we have enough space available for the default form
6136 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6137 * with no space for CIDs counted in). */
6138 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6139 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006142 return( ret );
6143 }
6144
Hanno Beckere5e7e782019-07-11 12:29:35 +01006145 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6146 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006149 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006150 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006151 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6152 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006153 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006154 if( ret != 0 )
6155 return( ret );
6156
6157 /* Fall through to handling of unexpected records */
6158 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6159 }
6160
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006161 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6162 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006163#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006164 /* Reset in pointers to default state for TLS/DTLS records,
6165 * assuming no CID and no offset between record content and
6166 * record plaintext. */
6167 ssl_update_in_pointers( ssl );
6168
Hanno Becker7ae20e02019-07-12 08:33:49 +01006169 /* Setup internal message pointers from record structure. */
6170 ssl->in_msgtype = rec.type;
6171#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6172 ssl->in_len = ssl->in_cid + rec.cid_len;
6173#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6174 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6175 ssl->in_msglen = rec.data_len;
6176
Hanno Becker2fddd372019-07-10 14:37:41 +01006177 ret = ssl_check_client_reconnect( ssl );
6178 if( ret != 0 )
6179 return( ret );
6180#endif
6181
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006182 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006183 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006184
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6186 "(header)" ) );
6187 }
6188 else
6189 {
6190 /* Skip invalid record and the rest of the datagram */
6191 ssl->next_record_offset = 0;
6192 ssl->in_left = 0;
6193
6194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6195 "(header)" ) );
6196 }
6197
6198 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006199 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006200 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006201 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006202#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006203 {
6204 return( ret );
6205 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006206 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006209 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006210 {
Hanno Beckera8814792019-07-10 15:01:45 +01006211 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006212 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006213 if( ssl->next_record_offset < ssl->in_left )
6214 {
6215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6216 }
6217 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006218 else
6219#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006220 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006221 /*
6222 * Fetch record contents from underlying transport.
6223 */
Hanno Beckera3175662019-07-11 12:50:29 +01006224 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006225 if( ret != 0 )
6226 {
6227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6228 return( ret );
6229 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006230
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006231 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006232 }
6233
6234 /*
6235 * Decrypt record contents.
6236 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006237
Hanno Beckerfdf66042019-07-11 13:07:45 +01006238 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006240#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006241 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006242 {
6243 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006244 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006245 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006246 /* Except when waiting for Finished as a bad mac here
6247 * probably means something went wrong in the handshake
6248 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6249 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6250 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6251 {
6252#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6253 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6254 {
6255 mbedtls_ssl_send_alert_message( ssl,
6256 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6257 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6258 }
6259#endif
6260 return( ret );
6261 }
6262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006264 if( ssl->conf->badmac_limit != 0 &&
6265 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6268 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006269 }
6270#endif
6271
Hanno Becker4a810fb2017-05-24 16:27:30 +01006272 /* As above, invalid records cause
6273 * dismissal of the whole datagram. */
6274
6275 ssl->next_record_offset = 0;
6276 ssl->in_left = 0;
6277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006279 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006280 }
6281
6282 return( ret );
6283 }
6284 else
6285#endif
6286 {
6287 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6289 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291 mbedtls_ssl_send_alert_message( ssl,
6292 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6293 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006294 }
6295#endif
6296 return( ret );
6297 }
6298 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006299
Hanno Becker44d89b22019-07-12 09:40:44 +01006300
6301 /* Reset in pointers to default state for TLS/DTLS records,
6302 * assuming no CID and no offset between record content and
6303 * record plaintext. */
6304 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006305#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6306 ssl->in_len = ssl->in_cid + rec.cid_len;
6307#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6308 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006309
Hanno Becker8685c822019-07-12 09:37:30 +01006310 /* The record content type may change during decryption,
6311 * so re-read it. */
6312 ssl->in_msgtype = rec.type;
6313 /* Also update the input buffer, because unfortunately
6314 * the server-side ssl_parse_client_hello() reparses the
6315 * record header when receiving a ClientHello initiating
6316 * a renegotiation. */
6317 ssl->in_hdr[0] = rec.type;
6318 ssl->in_msg = rec.buf + rec.data_offset;
6319 ssl->in_msglen = rec.data_len;
6320 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6321 ssl->in_len[1] = (unsigned char)( rec.data_len );
6322
Simon Butcher99000142016-10-13 17:21:01 +01006323 return( 0 );
6324}
6325
6326int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6327{
6328 int ret;
6329
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006330 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006331 * Handle particular types of records
6332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006334 {
Simon Butcher99000142016-10-13 17:21:01 +01006335 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6336 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006337 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006339 }
6340
Hanno Beckere678eaa2018-08-21 14:57:46 +01006341 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006342 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006343 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006344 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6346 ssl->in_msglen ) );
6347 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006348 }
6349
Hanno Beckere678eaa2018-08-21 14:57:46 +01006350 if( ssl->in_msg[0] != 1 )
6351 {
6352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6353 ssl->in_msg[0] ) );
6354 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6355 }
6356
6357#if defined(MBEDTLS_SSL_PROTO_DTLS)
6358 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6359 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6360 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6361 {
6362 if( ssl->handshake == NULL )
6363 {
6364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6365 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6366 }
6367
6368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6369 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6370 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006371#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006372 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006375 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006376 if( ssl->in_msglen != 2 )
6377 {
6378 /* Note: Standard allows for more than one 2 byte alert
6379 to be packed in a single message, but Mbed TLS doesn't
6380 currently support this. */
6381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6382 ssl->in_msglen ) );
6383 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6384 }
6385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006387 ssl->in_msg[0], ssl->in_msg[1] ) );
6388
6389 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006390 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006395 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006396 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006397 }
6398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6400 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6403 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006404 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006405
6406#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6407 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6408 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6409 {
Hanno Becker90333da2017-10-10 11:27:13 +01006410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006411 /* Will be handled when trying to parse ServerHello */
6412 return( 0 );
6413 }
6414#endif
6415
6416#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6417 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6418 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6419 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6420 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6421 {
6422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6423 /* Will be handled in mbedtls_ssl_parse_certificate() */
6424 return( 0 );
6425 }
6426#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6427
6428 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006429 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006430 }
6431
Hanno Beckerc76c6192017-06-06 10:03:17 +01006432#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006433 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006434 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006435 /* Drop unexpected ApplicationData records,
6436 * except at the beginning of renegotiations */
6437 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6438 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6439#if defined(MBEDTLS_SSL_RENEGOTIATION)
6440 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6441 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006442#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006443 )
6444 {
6445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6446 return( MBEDTLS_ERR_SSL_NON_FATAL );
6447 }
6448
6449 if( ssl->handshake != NULL &&
6450 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6451 {
6452 ssl_handshake_wrapup_free_hs_transform( ssl );
6453 }
6454 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006455#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006456
Paul Bakker5121ce52009-01-03 21:22:43 +00006457 return( 0 );
6458}
6459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006461{
6462 int ret;
6463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6465 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6466 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006467 {
6468 return( ret );
6469 }
6470
6471 return( 0 );
6472}
6473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006475 unsigned char level,
6476 unsigned char message )
6477{
6478 int ret;
6479
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006480 if( ssl == NULL || ssl->conf == NULL )
6481 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006487 ssl->out_msglen = 2;
6488 ssl->out_msg[0] = level;
6489 ssl->out_msg[1] = message;
6490
Hanno Becker67bc7c32018-08-06 11:33:50 +01006491 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006494 return( ret );
6495 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006497
6498 return( 0 );
6499}
6500
Hanno Beckerb9d44792019-02-08 07:19:04 +00006501#if defined(MBEDTLS_X509_CRT_PARSE_C)
6502static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6503{
6504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6505 if( session->peer_cert != NULL )
6506 {
6507 mbedtls_x509_crt_free( session->peer_cert );
6508 mbedtls_free( session->peer_cert );
6509 session->peer_cert = NULL;
6510 }
6511#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6512 if( session->peer_cert_digest != NULL )
6513 {
6514 /* Zeroization is not necessary. */
6515 mbedtls_free( session->peer_cert_digest );
6516 session->peer_cert_digest = NULL;
6517 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6518 session->peer_cert_digest_len = 0;
6519 }
6520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6521}
6522#endif /* MBEDTLS_X509_CRT_PARSE_C */
6523
Paul Bakker5121ce52009-01-03 21:22:43 +00006524/*
6525 * Handshake functions
6526 */
Hanno Becker21489932019-02-05 13:20:55 +00006527#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006528/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006530{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006531 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6532 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006535
Hanno Becker7177a882019-02-05 13:36:46 +00006536 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006539 ssl->state++;
6540 return( 0 );
6541 }
6542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6544 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006545}
6546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006548{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006549 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6550 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006553
Hanno Becker7177a882019-02-05 13:36:46 +00006554 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006557 ssl->state++;
6558 return( 0 );
6559 }
6560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006563}
Gilles Peskinef9828522017-05-03 12:28:43 +02006564
Hanno Becker21489932019-02-05 13:20:55 +00006565#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006566/* Some certificate support -> implement write and parse */
6567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006571 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006573 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6574 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006577
Hanno Becker7177a882019-02-05 13:36:46 +00006578 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006581 ssl->state++;
6582 return( 0 );
6583 }
6584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006586 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006587 {
6588 if( ssl->client_auth == 0 )
6589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006591 ssl->state++;
6592 return( 0 );
6593 }
6594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006596 /*
6597 * If using SSLv3 and got no cert, send an Alert message
6598 * (otherwise an empty Certificate message will be sent).
6599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6601 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006602 {
6603 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6605 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6606 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006609 goto write_msg;
6610 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006612 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613#endif /* MBEDTLS_SSL_CLI_C */
6614#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006615 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6620 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006621 }
6622 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006623#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006626
6627 /*
6628 * 0 . 0 handshake type
6629 * 1 . 3 handshake length
6630 * 4 . 6 length of all certs
6631 * 7 . 9 length of cert. 1
6632 * 10 . n-1 peer certificate
6633 * n . n+2 length of cert. 2
6634 * n+3 . ... upper level cert, etc.
6635 */
6636 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006638
Paul Bakker29087132010-03-21 21:03:34 +00006639 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006640 {
6641 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006642 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006645 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006647 }
6648
6649 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6650 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6651 ssl->out_msg[i + 2] = (unsigned char)( n );
6652
6653 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6654 i += n; crt = crt->next;
6655 }
6656
6657 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6658 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6659 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6660
6661 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6663 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006664
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006665#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006666write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006667#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006668
6669 ssl->state++;
6670
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006671 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006672 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006674 return( ret );
6675 }
6676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006678
Paul Bakkered27a042013-04-18 22:46:23 +02006679 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006680}
6681
Hanno Becker84879e32019-01-31 07:44:03 +00006682#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006683
6684#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006685static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6686 unsigned char *crt_buf,
6687 size_t crt_buf_len )
6688{
6689 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6690
6691 if( peer_crt == NULL )
6692 return( -1 );
6693
6694 if( peer_crt->raw.len != crt_buf_len )
6695 return( -1 );
6696
Hanno Becker46f34d02019-02-08 14:00:04 +00006697 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006698}
Hanno Becker177475a2019-02-05 17:02:46 +00006699#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6700static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6701 unsigned char *crt_buf,
6702 size_t crt_buf_len )
6703{
6704 int ret;
6705 unsigned char const * const peer_cert_digest =
6706 ssl->session->peer_cert_digest;
6707 mbedtls_md_type_t const peer_cert_digest_type =
6708 ssl->session->peer_cert_digest_type;
6709 mbedtls_md_info_t const * const digest_info =
6710 mbedtls_md_info_from_type( peer_cert_digest_type );
6711 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6712 size_t digest_len;
6713
6714 if( peer_cert_digest == NULL || digest_info == NULL )
6715 return( -1 );
6716
6717 digest_len = mbedtls_md_get_size( digest_info );
6718 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6719 return( -1 );
6720
6721 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6722 if( ret != 0 )
6723 return( -1 );
6724
6725 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6726}
6727#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006728#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006729
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006730/*
6731 * Once the certificate message is read, parse it into a cert chain and
6732 * perform basic checks, but leave actual verification to the caller
6733 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006734static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6735 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006736{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006737 int ret;
6738#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6739 int crt_cnt=0;
6740#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006741 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006742 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6748 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006750 }
6751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6753 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006756 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6757 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006759 }
6760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006762
Paul Bakker5121ce52009-01-03 21:22:43 +00006763 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006765 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006766 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006767
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006768 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006772 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6773 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006775 }
6776
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006777 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6778 i += 3;
6779
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006780 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006781 while( i < ssl->in_hslen )
6782 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006783 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006784 if ( i + 3 > ssl->in_hslen ) {
6785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006786 mbedtls_ssl_send_alert_message( ssl,
6787 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6788 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006789 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6790 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006791 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6792 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006793 if( ssl->in_msg[i] != 0 )
6794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006796 mbedtls_ssl_send_alert_message( ssl,
6797 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6798 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006800 }
6801
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006802 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6804 | (unsigned int) ssl->in_msg[i + 2];
6805 i += 3;
6806
6807 if( n < 128 || i + n > ssl->in_hslen )
6808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006810 mbedtls_ssl_send_alert_message( ssl,
6811 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6812 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006814 }
6815
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006816 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006817#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6818 if( crt_cnt++ == 0 &&
6819 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6820 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006821 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006822 /* During client-side renegotiation, check that the server's
6823 * end-CRTs hasn't changed compared to the initial handshake,
6824 * mitigating the triple handshake attack. On success, reuse
6825 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6827 if( ssl_check_peer_crt_unchanged( ssl,
6828 &ssl->in_msg[i],
6829 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006830 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6832 mbedtls_ssl_send_alert_message( ssl,
6833 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6834 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6835 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006836 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006837
6838 /* Now we can safely free the original chain. */
6839 ssl_clear_peer_cert( ssl->session );
6840 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006841#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6842
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006843 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006844#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006845 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006846#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006847 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006848 * it in-place from the input buffer instead of making a copy. */
6849 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6850#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006851 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006852 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006853 case 0: /*ok*/
6854 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6855 /* Ignore certificate with an unknown algorithm: maybe a
6856 prior certificate was already trusted. */
6857 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006858
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006859 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6860 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6861 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006862
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006863 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6864 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6865 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006866
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006867 default:
6868 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6869 crt_parse_der_failed:
6870 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6871 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6872 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006873 }
6874
6875 i += n;
6876 }
6877
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006878 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006879 return( 0 );
6880}
6881
Hanno Becker4a55f632019-02-05 12:49:06 +00006882#if defined(MBEDTLS_SSL_SRV_C)
6883static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6884{
6885 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6886 return( -1 );
6887
6888#if defined(MBEDTLS_SSL_PROTO_SSL3)
6889 /*
6890 * Check if the client sent an empty certificate
6891 */
6892 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6893 {
6894 if( ssl->in_msglen == 2 &&
6895 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6896 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6897 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6898 {
6899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6900 return( 0 );
6901 }
6902
6903 return( -1 );
6904 }
6905#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6906
6907#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6908 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6909 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6910 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6911 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6912 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6913 {
6914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6915 return( 0 );
6916 }
6917
6918 return( -1 );
6919#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6920 MBEDTLS_SSL_PROTO_TLS1_2 */
6921}
6922#endif /* MBEDTLS_SSL_SRV_C */
6923
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006924/* Check if a certificate message is expected.
6925 * Return either
6926 * - SSL_CERTIFICATE_EXPECTED, or
6927 * - SSL_CERTIFICATE_SKIP
6928 * indicating whether a Certificate message is expected or not.
6929 */
6930#define SSL_CERTIFICATE_EXPECTED 0
6931#define SSL_CERTIFICATE_SKIP 1
6932static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6933 int authmode )
6934{
6935 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006936 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006937
6938 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6939 return( SSL_CERTIFICATE_SKIP );
6940
6941#if defined(MBEDTLS_SSL_SRV_C)
6942 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6943 {
6944 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6945 return( SSL_CERTIFICATE_SKIP );
6946
6947 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6948 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006949 ssl->session_negotiate->verify_result =
6950 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6951 return( SSL_CERTIFICATE_SKIP );
6952 }
6953 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006954#else
6955 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006956#endif /* MBEDTLS_SSL_SRV_C */
6957
6958 return( SSL_CERTIFICATE_EXPECTED );
6959}
6960
Hanno Becker68636192019-02-05 14:36:34 +00006961static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6962 int authmode,
6963 mbedtls_x509_crt *chain,
6964 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006965{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006966 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006967 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006968 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006969 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006970
Hanno Becker8927c832019-04-03 12:52:50 +01006971 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6972 void *p_vrfy;
6973
Hanno Becker68636192019-02-05 14:36:34 +00006974 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6975 return( 0 );
6976
Hanno Becker8927c832019-04-03 12:52:50 +01006977 if( ssl->f_vrfy != NULL )
6978 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006980 f_vrfy = ssl->f_vrfy;
6981 p_vrfy = ssl->p_vrfy;
6982 }
6983 else
6984 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006985 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006986 f_vrfy = ssl->conf->f_vrfy;
6987 p_vrfy = ssl->conf->p_vrfy;
6988 }
6989
Hanno Becker68636192019-02-05 14:36:34 +00006990 /*
6991 * Main check: verify certificate
6992 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006993#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6994 if( ssl->conf->f_ca_cb != NULL )
6995 {
6996 ((void) rs_ctx);
6997 have_ca_chain = 1;
6998
6999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03007000 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007001 chain,
7002 ssl->conf->f_ca_cb,
7003 ssl->conf->p_ca_cb,
7004 ssl->conf->cert_profile,
7005 ssl->hostname,
7006 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007007 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007008 }
7009 else
7010#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7011 {
7012 mbedtls_x509_crt *ca_chain;
7013 mbedtls_x509_crl *ca_crl;
7014
7015#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7016 if( ssl->handshake->sni_ca_chain != NULL )
7017 {
7018 ca_chain = ssl->handshake->sni_ca_chain;
7019 ca_crl = ssl->handshake->sni_ca_crl;
7020 }
7021 else
7022#endif
7023 {
7024 ca_chain = ssl->conf->ca_chain;
7025 ca_crl = ssl->conf->ca_crl;
7026 }
7027
7028 if( ca_chain != NULL )
7029 have_ca_chain = 1;
7030
7031 ret = mbedtls_x509_crt_verify_restartable(
7032 chain,
7033 ca_chain, ca_crl,
7034 ssl->conf->cert_profile,
7035 ssl->hostname,
7036 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007037 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007038 }
Hanno Becker68636192019-02-05 14:36:34 +00007039
7040 if( ret != 0 )
7041 {
7042 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7043 }
7044
7045#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7046 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7047 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7048#endif
7049
7050 /*
7051 * Secondary checks: always done, but change 'ret' only if it was 0
7052 */
7053
7054#if defined(MBEDTLS_ECP_C)
7055 {
7056 const mbedtls_pk_context *pk = &chain->pk;
7057
7058 /* If certificate uses an EC key, make sure the curve is OK */
7059 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7060 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7061 {
7062 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7063
7064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7065 if( ret == 0 )
7066 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7067 }
7068 }
7069#endif /* MBEDTLS_ECP_C */
7070
7071 if( mbedtls_ssl_check_cert_usage( chain,
7072 ciphersuite_info,
7073 ! ssl->conf->endpoint,
7074 &ssl->session_negotiate->verify_result ) != 0 )
7075 {
7076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7077 if( ret == 0 )
7078 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7079 }
7080
7081 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7082 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7083 * with details encoded in the verification flags. All other kinds
7084 * of error codes, including those from the user provided f_vrfy
7085 * functions, are treated as fatal and lead to a failure of
7086 * ssl_parse_certificate even if verification was optional. */
7087 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7088 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7089 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7090 {
7091 ret = 0;
7092 }
7093
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007094 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007095 {
7096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7097 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7098 }
7099
7100 if( ret != 0 )
7101 {
7102 uint8_t alert;
7103
7104 /* The certificate may have been rejected for several reasons.
7105 Pick one and send the corresponding alert. Which alert to send
7106 may be a subject of debate in some cases. */
7107 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7108 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7109 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7110 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7111 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7112 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7113 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7114 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7115 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7116 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7117 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7118 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7119 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7120 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7121 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7122 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7123 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7124 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7125 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7126 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7127 else
7128 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7129 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7130 alert );
7131 }
7132
7133#if defined(MBEDTLS_DEBUG_C)
7134 if( ssl->session_negotiate->verify_result != 0 )
7135 {
7136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7137 ssl->session_negotiate->verify_result ) );
7138 }
7139 else
7140 {
7141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7142 }
7143#endif /* MBEDTLS_DEBUG_C */
7144
7145 return( ret );
7146}
7147
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007148#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7149static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7150 unsigned char *start, size_t len )
7151{
7152 int ret;
7153 /* Remember digest of the peer's end-CRT. */
7154 ssl->session_negotiate->peer_cert_digest =
7155 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7156 if( ssl->session_negotiate->peer_cert_digest == NULL )
7157 {
7158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7159 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7160 mbedtls_ssl_send_alert_message( ssl,
7161 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7162 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7163
7164 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7165 }
7166
7167 ret = mbedtls_md( mbedtls_md_info_from_type(
7168 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7169 start, len,
7170 ssl->session_negotiate->peer_cert_digest );
7171
7172 ssl->session_negotiate->peer_cert_digest_type =
7173 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7174 ssl->session_negotiate->peer_cert_digest_len =
7175 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7176
7177 return( ret );
7178}
7179
7180static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7181 unsigned char *start, size_t len )
7182{
7183 unsigned char *end = start + len;
7184 int ret;
7185
7186 /* Make a copy of the peer's raw public key. */
7187 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7188 ret = mbedtls_pk_parse_subpubkey( &start, end,
7189 &ssl->handshake->peer_pubkey );
7190 if( ret != 0 )
7191 {
7192 /* We should have parsed the public key before. */
7193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7194 }
7195
7196 return( 0 );
7197}
7198#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7199
Hanno Becker68636192019-02-05 14:36:34 +00007200int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7201{
7202 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007203 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007204#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7205 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7206 ? ssl->handshake->sni_authmode
7207 : ssl->conf->authmode;
7208#else
7209 const int authmode = ssl->conf->authmode;
7210#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007211 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007212 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007213
7214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7215
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007216 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7217 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007218 {
7219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007220 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007221 }
7222
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007223#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7224 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007225 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007226 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007227 chain = ssl->handshake->ecrs_peer_cert;
7228 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007229 goto crt_verify;
7230 }
7231#endif
7232
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007233 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007234 {
7235 /* mbedtls_ssl_read_record may have sent an alert already. We
7236 let it decide whether to alert. */
7237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007238 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007239 }
7240
Hanno Becker4a55f632019-02-05 12:49:06 +00007241#if defined(MBEDTLS_SSL_SRV_C)
7242 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7243 {
7244 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007245
7246 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007247 ret = 0;
7248 else
7249 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007250
Hanno Becker6bdfab22019-02-05 13:11:17 +00007251 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007252 }
7253#endif /* MBEDTLS_SSL_SRV_C */
7254
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007255 /* Clear existing peer CRT structure in case we tried to
7256 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007257 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007258
7259 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7260 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007261 {
7262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7263 sizeof( mbedtls_x509_crt ) ) );
7264 mbedtls_ssl_send_alert_message( ssl,
7265 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7266 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007267
Hanno Becker3dad3112019-02-05 17:19:52 +00007268 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7269 goto exit;
7270 }
7271 mbedtls_x509_crt_init( chain );
7272
7273 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007274 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007275 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007276
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007277#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7278 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007279 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007280
7281crt_verify:
7282 if( ssl->handshake->ecrs_enabled)
7283 rs_ctx = &ssl->handshake->ecrs_ctx;
7284#endif
7285
Hanno Becker68636192019-02-05 14:36:34 +00007286 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007287 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007288 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007289 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007290
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007291#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007292 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007293 unsigned char *crt_start, *pk_start;
7294 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007295
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007296 /* We parse the CRT chain without copying, so
7297 * these pointers point into the input buffer,
7298 * and are hence still valid after freeing the
7299 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007300
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007301 crt_start = chain->raw.p;
7302 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007303
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007304 pk_start = chain->pk_raw.p;
7305 pk_len = chain->pk_raw.len;
7306
7307 /* Free the CRT structures before computing
7308 * digest and copying the peer's public key. */
7309 mbedtls_x509_crt_free( chain );
7310 mbedtls_free( chain );
7311 chain = NULL;
7312
7313 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007314 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007315 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007316
7317 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7318 if( ret != 0 )
7319 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007320 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007321#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7322 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007323 ssl->session_negotiate->peer_cert = chain;
7324 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007325#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007328
Hanno Becker6bdfab22019-02-05 13:11:17 +00007329exit:
7330
Hanno Becker3dad3112019-02-05 17:19:52 +00007331 if( ret == 0 )
7332 ssl->state++;
7333
7334#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7335 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7336 {
7337 ssl->handshake->ecrs_peer_cert = chain;
7338 chain = NULL;
7339 }
7340#endif
7341
7342 if( chain != NULL )
7343 {
7344 mbedtls_x509_crt_free( chain );
7345 mbedtls_free( chain );
7346 }
7347
Paul Bakker5121ce52009-01-03 21:22:43 +00007348 return( ret );
7349}
Hanno Becker21489932019-02-05 13:20:55 +00007350#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007353{
7354 int ret;
7355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007359 ssl->out_msglen = 1;
7360 ssl->out_msg[0] = 1;
7361
Paul Bakker5121ce52009-01-03 21:22:43 +00007362 ssl->state++;
7363
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007364 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007365 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007367 return( ret );
7368 }
7369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007371
7372 return( 0 );
7373}
7374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007376{
7377 int ret;
7378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007380
Hanno Becker327c93b2018-08-15 13:56:18 +01007381 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007384 return( ret );
7385 }
7386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007390 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7391 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007393 }
7394
Hanno Beckere678eaa2018-08-21 14:57:46 +01007395 /* CCS records are only accepted if they have length 1 and content '1',
7396 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007397
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007398 /*
7399 * Switch to our negotiated transform and session parameters for inbound
7400 * data.
7401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007403 ssl->transform_in = ssl->transform_negotiate;
7404 ssl->session_in = ssl->session_negotiate;
7405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007407 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007410 ssl_dtls_replay_reset( ssl );
7411#endif
7412
7413 /* Increment epoch */
7414 if( ++ssl->in_epoch == 0 )
7415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007417 /* This is highly unlikely to happen for legitimate reasons, so
7418 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007420 }
7421 }
7422 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007424 memset( ssl->in_ctr, 0, 8 );
7425
Hanno Becker79594fd2019-05-08 09:38:41 +01007426 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7429 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007434 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7435 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007437 }
7438 }
7439#endif
7440
Paul Bakker5121ce52009-01-03 21:22:43 +00007441 ssl->state++;
7442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007444
7445 return( 0 );
7446}
7447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7449 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007450{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007451 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7454 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7455 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007456 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007457 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007458#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7460#if defined(MBEDTLS_SHA512_C)
7461 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007462 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7463 else
7464#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465#if defined(MBEDTLS_SHA256_C)
7466 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007467 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007468 else
7469#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007473 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007474 }
Paul Bakker380da532012-04-18 16:10:25 +00007475}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007478{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7480 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007481 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7482 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007483#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7485#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007486#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007487 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007488 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7489#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007490 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007491#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007492#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007494#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007495 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007496 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007497#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007498 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007499#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007500#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007502}
7503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007505 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7508 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007509 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7510 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007511#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7513#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007514#if defined(MBEDTLS_USE_PSA_CRYPTO)
7515 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7516#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007517 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007518#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007519#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007521#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007522 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007523#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007524 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007525#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007526#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007528}
7529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7531 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7532static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007533 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007534{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007535 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7536 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007537}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007538#endif
Paul Bakker380da532012-04-18 16:10:25 +00007539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7541#if defined(MBEDTLS_SHA256_C)
7542static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007543 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007544{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007545#if defined(MBEDTLS_USE_PSA_CRYPTO)
7546 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7547#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007548 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007549#endif
Paul Bakker380da532012-04-18 16:10:25 +00007550}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007551#endif
Paul Bakker380da532012-04-18 16:10:25 +00007552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#if defined(MBEDTLS_SHA512_C)
7554static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007555 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007556{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007557#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007558 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007559#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007560 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007561#endif
Paul Bakker380da532012-04-18 16:10:25 +00007562}
Paul Bakker769075d2012-11-24 11:26:46 +01007563#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007567static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007569{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007570 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007571 mbedtls_md5_context md5;
7572 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007573
Paul Bakker5121ce52009-01-03 21:22:43 +00007574 unsigned char padbuf[48];
7575 unsigned char md5sum[16];
7576 unsigned char sha1sum[20];
7577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007579 if( !session )
7580 session = ssl->session;
7581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007583
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007584 mbedtls_md5_init( &md5 );
7585 mbedtls_sha1_init( &sha1 );
7586
7587 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7588 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007589
7590 /*
7591 * SSLv3:
7592 * hash =
7593 * MD5( master + pad2 +
7594 * MD5( handshake + sender + master + pad1 ) )
7595 * + SHA1( master + pad2 +
7596 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007597 */
7598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007600 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7601 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007602#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007605 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7606 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007607#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007610 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007611
Paul Bakker1ef83d62012-04-11 12:09:53 +00007612 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007613
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007614 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7615 mbedtls_md5_update_ret( &md5, session->master, 48 );
7616 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7617 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007618
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007619 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7620 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7621 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7622 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007623
Paul Bakker1ef83d62012-04-11 12:09:53 +00007624 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007625
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007626 mbedtls_md5_starts_ret( &md5 );
7627 mbedtls_md5_update_ret( &md5, session->master, 48 );
7628 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7629 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7630 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007631
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007632 mbedtls_sha1_starts_ret( &sha1 );
7633 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7634 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7635 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7636 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007639
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007640 mbedtls_md5_free( &md5 );
7641 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007642
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007643 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7644 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7645 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007652static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007654{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007655 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007656 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007657 mbedtls_md5_context md5;
7658 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007659 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007662 if( !session )
7663 session = ssl->session;
7664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007666
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007667 mbedtls_md5_init( &md5 );
7668 mbedtls_sha1_init( &sha1 );
7669
7670 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7671 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007672
Paul Bakker1ef83d62012-04-11 12:09:53 +00007673 /*
7674 * TLSv1:
7675 * hash = PRF( master, finished_label,
7676 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7677 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007680 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7681 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007682#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007685 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7686 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007687#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007690 ? "client finished"
7691 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007692
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007693 mbedtls_md5_finish_ret( &md5, padbuf );
7694 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007695
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007696 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007697 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007700
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007701 mbedtls_md5_free( &md5 );
7702 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007703
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007704 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7711#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007712static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007714{
7715 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007716 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007717 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007718#if defined(MBEDTLS_USE_PSA_CRYPTO)
7719 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007720 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007721 psa_status_t status;
7722#else
7723 mbedtls_sha256_context sha256;
7724#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007727 if( !session )
7728 session = ssl->session;
7729
Andrzej Kurekeb342242019-01-29 09:14:33 -05007730 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7731 ? "client finished"
7732 : "server finished";
7733
7734#if defined(MBEDTLS_USE_PSA_CRYPTO)
7735 sha256_psa = psa_hash_operation_init();
7736
7737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7738
7739 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7740 if( status != PSA_SUCCESS )
7741 {
7742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7743 return;
7744 }
7745
7746 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7747 if( status != PSA_SUCCESS )
7748 {
7749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7750 return;
7751 }
7752 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7753#else
7754
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007755 mbedtls_sha256_init( &sha256 );
7756
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007758
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007759 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007760
7761 /*
7762 * TLSv1.2:
7763 * hash = PRF( master, finished_label,
7764 * Hash( handshake ) )[0.11]
7765 */
7766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767#if !defined(MBEDTLS_SHA256_ALT)
7768 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007769 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007770#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007771
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007772 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007773 mbedtls_sha256_free( &sha256 );
7774#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007775
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007776 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007777 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007780
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007781 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007788static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007790{
7791 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007792 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007793 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007794#if defined(MBEDTLS_USE_PSA_CRYPTO)
7795 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007796 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007797 psa_status_t status;
7798#else
7799 mbedtls_sha512_context sha512;
7800#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007803 if( !session )
7804 session = ssl->session;
7805
Andrzej Kurekeb342242019-01-29 09:14:33 -05007806 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7807 ? "client finished"
7808 : "server finished";
7809
7810#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007811 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007812
7813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7814
Andrzej Kurek972fba52019-01-30 03:29:12 -05007815 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007816 if( status != PSA_SUCCESS )
7817 {
7818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7819 return;
7820 }
7821
Andrzej Kurek972fba52019-01-30 03:29:12 -05007822 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007823 if( status != PSA_SUCCESS )
7824 {
7825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7826 return;
7827 }
7828 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7829#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007830 mbedtls_sha512_init( &sha512 );
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007833
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007834 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007835
7836 /*
7837 * TLSv1.2:
7838 * hash = PRF( master, finished_label,
7839 * Hash( handshake ) )[0.11]
7840 */
7841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007843 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7844 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007845#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007846
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007847 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007848 mbedtls_sha512_free( &sha512 );
7849#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007850
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007851 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007852 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007855
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007856 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007859}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860#endif /* MBEDTLS_SHA512_C */
7861#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007864{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007866
7867 /*
7868 * Free our handshake params
7869 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007870 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007872 ssl->handshake = NULL;
7873
7874 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007875 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007876 */
7877 if( ssl->transform )
7878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879 mbedtls_ssl_transform_free( ssl->transform );
7880 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007881 }
7882 ssl->transform = ssl->transform_negotiate;
7883 ssl->transform_negotiate = NULL;
7884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007886}
7887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007889{
7890 int resume = ssl->handshake->resume;
7891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_RENEGOTIATION)
7895 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007898 ssl->renego_records_seen = 0;
7899 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007900#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007901
7902 /*
7903 * Free the previous session and switch in the current one
7904 */
Paul Bakker0a597072012-09-25 21:55:46 +00007905 if( ssl->session )
7906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007908 /* RFC 7366 3.1: keep the EtM state */
7909 ssl->session_negotiate->encrypt_then_mac =
7910 ssl->session->encrypt_then_mac;
7911#endif
7912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 mbedtls_ssl_session_free( ssl->session );
7914 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007915 }
7916 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007917 ssl->session_negotiate = NULL;
7918
Paul Bakker0a597072012-09-25 21:55:46 +00007919 /*
7920 * Add cache entry
7921 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007922 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007923 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007924 resume == 0 )
7925 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007926 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007928 }
Paul Bakker0a597072012-09-25 21:55:46 +00007929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007931 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007932 ssl->handshake->flight != NULL )
7933 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007934 /* Cancel handshake timer */
7935 ssl_set_timer( ssl, 0 );
7936
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007937 /* Keep last flight around in case we need to resend it:
7938 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007940 }
7941 else
7942#endif
7943 ssl_handshake_wrapup_free_hs_transform( ssl );
7944
Paul Bakker48916f92012-09-16 19:57:18 +00007945 ssl->state++;
7946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007948}
7949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007951{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007952 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007955
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007956 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007957
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007958 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007959
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007960 /*
7961 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7962 * may define some other value. Currently (early 2016), no defined
7963 * ciphersuite does this (and this is unlikely to change as activity has
7964 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007969 ssl->verify_data_len = hash_len;
7970 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007971#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007972
Paul Bakker5121ce52009-01-03 21:22:43 +00007973 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7975 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007976
7977 /*
7978 * In case of session resuming, invert the client and server
7979 * ChangeCipherSpec messages order.
7980 */
Paul Bakker0a597072012-09-25 21:55:46 +00007981 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007984 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007986#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007988 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007990#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007991 }
7992 else
7993 ssl->state++;
7994
Paul Bakker48916f92012-09-16 19:57:18 +00007995 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007996 * Switch to our negotiated transform and session parameters for outbound
7997 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008002 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008003 {
8004 unsigned char i;
8005
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008006 /* Remember current epoch settings for resending */
8007 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01008008 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008009
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008010 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008011 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008012
8013 /* Increment epoch */
8014 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008015 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008016 break;
8017
8018 /* The loop goes to its end iff the counter is wrapping */
8019 if( i == 0 )
8020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8022 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008023 }
8024 }
8025 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008027 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008028
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008029 ssl->transform_out = ssl->transform_negotiate;
8030 ssl->session_out = ssl->session_negotiate;
8031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8033 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8038 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008039 }
8040 }
8041#endif
8042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008044 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008046#endif
8047
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008048 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008049 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008050 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008051 return( ret );
8052 }
8053
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008054#if defined(MBEDTLS_SSL_PROTO_DTLS)
8055 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8056 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8057 {
8058 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8059 return( ret );
8060 }
8061#endif
8062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008064
8065 return( 0 );
8066}
8067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008069#define SSL_MAX_HASH_LEN 36
8070#else
8071#define SSL_MAX_HASH_LEN 12
8072#endif
8073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008075{
Paul Bakker23986e52011-04-24 08:57:21 +00008076 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008077 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008078 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008081
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008082 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008083
Hanno Becker327c93b2018-08-15 13:56:18 +01008084 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008087 return( ret );
8088 }
8089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008093 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8094 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008096 }
8097
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008098 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099#if defined(MBEDTLS_SSL_PROTO_SSL3)
8100 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008101 hash_len = 36;
8102 else
8103#endif
8104 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8107 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008110 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8111 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008113 }
8114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008115 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008116 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008119 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8120 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008122 }
8123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008124#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008125 ssl->verify_data_len = hash_len;
8126 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008127#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008128
Paul Bakker0a597072012-09-25 21:55:46 +00008129 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008132 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008134#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008136 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008139 }
8140 else
8141 ssl->state++;
8142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008144 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008146#endif
8147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008149
8150 return( 0 );
8151}
8152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008153static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008157#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8158 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8159 mbedtls_md5_init( &handshake->fin_md5 );
8160 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008161 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8162 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8165#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008166#if defined(MBEDTLS_USE_PSA_CRYPTO)
8167 handshake->fin_sha256_psa = psa_hash_operation_init();
8168 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8169#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008171 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008172#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008173#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008175#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008176 handshake->fin_sha384_psa = psa_hash_operation_init();
8177 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008178#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008180 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008181#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008182#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008184
8185 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008186
8187#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8188 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8189 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8190#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192#if defined(MBEDTLS_DHM_C)
8193 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195#if defined(MBEDTLS_ECDH_C)
8196 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008197#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008198#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008199 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008200#if defined(MBEDTLS_SSL_CLI_C)
8201 handshake->ecjpake_cache = NULL;
8202 handshake->ecjpake_cache_len = 0;
8203#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008204#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008205
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008206#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008207 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008208#endif
8209
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008210#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8211 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8212#endif
Hanno Becker75173122019-02-06 16:18:31 +00008213
8214#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8215 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8216 mbedtls_pk_init( &handshake->peer_pubkey );
8217#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008218}
8219
Hanno Beckera18d1322018-01-03 14:27:32 +00008220void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008224 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8225 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008226
Hanno Beckerd56ed242018-01-03 15:32:51 +00008227#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228 mbedtls_md_init( &transform->md_ctx_enc );
8229 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008230#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008231}
8232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008236}
8237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008239{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008240 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008241 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008242 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008243 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008245 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008246 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008247
8248 /*
8249 * Either the pointers are now NULL or cleared properly and can be freed.
8250 * Now allocate missing structures.
8251 */
8252 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008253 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008254 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008255 }
Paul Bakker48916f92012-09-16 19:57:18 +00008256
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008257 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008258 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008259 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008260 }
Paul Bakker48916f92012-09-16 19:57:18 +00008261
Paul Bakker82788fb2014-10-20 13:59:19 +02008262 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008263 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008264 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008265 }
Paul Bakker48916f92012-09-16 19:57:18 +00008266
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008267 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008268 if( ssl->handshake == NULL ||
8269 ssl->transform_negotiate == NULL ||
8270 ssl->session_negotiate == NULL )
8271 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274 mbedtls_free( ssl->handshake );
8275 mbedtls_free( ssl->transform_negotiate );
8276 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008277
8278 ssl->handshake = NULL;
8279 ssl->transform_negotiate = NULL;
8280 ssl->session_negotiate = NULL;
8281
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008282 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008283 }
8284
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008285 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008287 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008288 ssl_handshake_params_init( ssl->handshake );
8289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008291 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8292 {
8293 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008294
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008295 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8296 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8297 else
8298 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008299
8300 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008301 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008302#endif
8303
Paul Bakker48916f92012-09-16 19:57:18 +00008304 return( 0 );
8305}
8306
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008307#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008308/* Dummy cookie callbacks for defaults */
8309static int ssl_cookie_write_dummy( void *ctx,
8310 unsigned char **p, unsigned char *end,
8311 const unsigned char *cli_id, size_t cli_id_len )
8312{
8313 ((void) ctx);
8314 ((void) p);
8315 ((void) end);
8316 ((void) cli_id);
8317 ((void) cli_id_len);
8318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008320}
8321
8322static int ssl_cookie_check_dummy( void *ctx,
8323 const unsigned char *cookie, size_t cookie_len,
8324 const unsigned char *cli_id, size_t cli_id_len )
8325{
8326 ((void) ctx);
8327 ((void) cookie);
8328 ((void) cookie_len);
8329 ((void) cli_id);
8330 ((void) cli_id_len);
8331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008333}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008334#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008335
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008336/* Once ssl->out_hdr as the address of the beginning of the
8337 * next outgoing record is set, deduce the other pointers.
8338 *
8339 * Note: For TLS, we save the implicit record sequence number
8340 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8341 * and the caller has to make sure there's space for this.
8342 */
8343
8344static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8345 mbedtls_ssl_transform *transform )
8346{
8347#if defined(MBEDTLS_SSL_PROTO_DTLS)
8348 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8349 {
8350 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008351#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008352 ssl->out_cid = ssl->out_ctr + 8;
8353 ssl->out_len = ssl->out_cid;
8354 if( transform != NULL )
8355 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008356#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008357 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008358#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008359 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008360 }
8361 else
8362#endif
8363 {
8364 ssl->out_ctr = ssl->out_hdr - 8;
8365 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008366#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008367 ssl->out_cid = ssl->out_len;
8368#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008369 ssl->out_iv = ssl->out_hdr + 5;
8370 }
8371
8372 /* Adjust out_msg to make space for explicit IV, if used. */
8373 if( transform != NULL &&
8374 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8375 {
8376 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8377 }
8378 else
8379 ssl->out_msg = ssl->out_iv;
8380}
8381
8382/* Once ssl->in_hdr as the address of the beginning of the
8383 * next incoming record is set, deduce the other pointers.
8384 *
8385 * Note: For TLS, we save the implicit record sequence number
8386 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8387 * and the caller has to make sure there's space for this.
8388 */
8389
Hanno Becker79594fd2019-05-08 09:38:41 +01008390static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008391{
Hanno Becker79594fd2019-05-08 09:38:41 +01008392 /* This function sets the pointers to match the case
8393 * of unprotected TLS/DTLS records, with both ssl->in_iv
8394 * and ssl->in_msg pointing to the beginning of the record
8395 * content.
8396 *
8397 * When decrypting a protected record, ssl->in_msg
8398 * will be shifted to point to the beginning of the
8399 * record plaintext.
8400 */
8401
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008402#if defined(MBEDTLS_SSL_PROTO_DTLS)
8403 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8404 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008405 /* This sets the header pointers to match records
8406 * without CID. When we receive a record containing
8407 * a CID, the fields are shifted accordingly in
8408 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008409 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008410#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008411 ssl->in_cid = ssl->in_ctr + 8;
8412 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008413#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008414 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008415#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008416 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008417 }
8418 else
8419#endif
8420 {
8421 ssl->in_ctr = ssl->in_hdr - 8;
8422 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008423#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008424 ssl->in_cid = ssl->in_len;
8425#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008426 ssl->in_iv = ssl->in_hdr + 5;
8427 }
8428
Hanno Becker79594fd2019-05-08 09:38:41 +01008429 /* This will be adjusted at record decryption time. */
8430 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008431}
8432
Paul Bakker5121ce52009-01-03 21:22:43 +00008433/*
8434 * Initialize an SSL context
8435 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008436void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8437{
8438 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8439}
8440
8441/*
8442 * Setup an SSL context
8443 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008444
8445static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8446{
8447 /* Set the incoming and outgoing record pointers. */
8448#if defined(MBEDTLS_SSL_PROTO_DTLS)
8449 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8450 {
8451 ssl->out_hdr = ssl->out_buf;
8452 ssl->in_hdr = ssl->in_buf;
8453 }
8454 else
8455#endif /* MBEDTLS_SSL_PROTO_DTLS */
8456 {
8457 ssl->out_hdr = ssl->out_buf + 8;
8458 ssl->in_hdr = ssl->in_buf + 8;
8459 }
8460
8461 /* Derive other internal pointers. */
8462 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008463 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008464}
8465
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008466int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008467 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008468{
Paul Bakker48916f92012-09-16 19:57:18 +00008469 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008470
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008471 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008472
8473 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008474 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008475 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008476
8477 /* Set to NULL in case of an error condition */
8478 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008479
Angus Grattond8213d02016-05-25 20:56:48 +10008480 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8481 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008482 {
Angus Grattond8213d02016-05-25 20:56:48 +10008483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008484 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008485 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008486 }
8487
8488 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8489 if( ssl->out_buf == NULL )
8490 {
8491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008492 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008493 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008494 }
8495
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008496 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008497
Paul Bakker48916f92012-09-16 19:57:18 +00008498 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008499 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008500
8501 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008502
8503error:
8504 mbedtls_free( ssl->in_buf );
8505 mbedtls_free( ssl->out_buf );
8506
8507 ssl->conf = NULL;
8508
8509 ssl->in_buf = NULL;
8510 ssl->out_buf = NULL;
8511
8512 ssl->in_hdr = NULL;
8513 ssl->in_ctr = NULL;
8514 ssl->in_len = NULL;
8515 ssl->in_iv = NULL;
8516 ssl->in_msg = NULL;
8517
8518 ssl->out_hdr = NULL;
8519 ssl->out_ctr = NULL;
8520 ssl->out_len = NULL;
8521 ssl->out_iv = NULL;
8522 ssl->out_msg = NULL;
8523
k-stachowiak9f7798e2018-07-31 16:52:32 +02008524 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008525}
8526
8527/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008528 * Reset an initialized and used SSL context for re-use while retaining
8529 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008530 *
8531 * If partial is non-zero, keep data in the input buffer and client ID.
8532 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008533 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008534static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008535{
Paul Bakker48916f92012-09-16 19:57:18 +00008536 int ret;
8537
Hanno Becker7e772132018-08-10 12:38:21 +01008538#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8539 !defined(MBEDTLS_SSL_SRV_C)
8540 ((void) partial);
8541#endif
8542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008544
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008545 /* Cancel any possibly running timer */
8546 ssl_set_timer( ssl, 0 );
8547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008548#if defined(MBEDTLS_SSL_RENEGOTIATION)
8549 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008550 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008551
8552 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8554 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008555#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008557
Paul Bakker7eb013f2011-10-06 12:37:39 +00008558 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008559 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008560
8561 ssl->in_msgtype = 0;
8562 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008563#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008564 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008565 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008568 ssl_dtls_replay_reset( ssl );
8569#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008570
8571 ssl->in_hslen = 0;
8572 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008573
8574 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008575
8576 ssl->out_msgtype = 0;
8577 ssl->out_msglen = 0;
8578 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8580 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008581 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008582#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008583
Hanno Becker19859472018-08-06 09:40:20 +01008584 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8585
Paul Bakker48916f92012-09-16 19:57:18 +00008586 ssl->transform_in = NULL;
8587 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008588
Hanno Becker78640902018-08-13 16:35:15 +01008589 ssl->session_in = NULL;
8590 ssl->session_out = NULL;
8591
Angus Grattond8213d02016-05-25 20:56:48 +10008592 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008593
8594#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008595 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008596#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8597 {
8598 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008599 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008600 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8603 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8606 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8609 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008610 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008611 }
8612#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008613
Paul Bakker48916f92012-09-16 19:57:18 +00008614 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616 mbedtls_ssl_transform_free( ssl->transform );
8617 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008618 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008619 }
Paul Bakker48916f92012-09-16 19:57:18 +00008620
Paul Bakkerc0463502013-02-14 11:19:38 +01008621 if( ssl->session )
8622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623 mbedtls_ssl_session_free( ssl->session );
8624 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008625 ssl->session = NULL;
8626 }
8627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008629 ssl->alpn_chosen = NULL;
8630#endif
8631
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008632#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008633#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008634 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008635#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008636 {
8637 mbedtls_free( ssl->cli_id );
8638 ssl->cli_id = NULL;
8639 ssl->cli_id_len = 0;
8640 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008641#endif
8642
Paul Bakker48916f92012-09-16 19:57:18 +00008643 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8644 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008645
8646 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008647}
8648
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008649/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008650 * Reset an initialized and used SSL context for re-use while retaining
8651 * all application-set variables, function pointers and data.
8652 */
8653int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8654{
8655 return( ssl_session_reset_int( ssl, 0 ) );
8656}
8657
8658/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008659 * SSL set accessors
8660 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008661void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008662{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008663 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008664}
8665
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008666void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008667{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008668 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008669}
8670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008672void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008673{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008674 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008675}
8676#endif
8677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008679void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008680{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008681 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008682}
8683#endif
8684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008686
Hanno Becker1841b0a2018-08-24 11:13:57 +01008687void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8688 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008689{
8690 ssl->disable_datagram_packing = !allow_packing;
8691}
8692
8693void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8694 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008695{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008696 conf->hs_timeout_min = min;
8697 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008698}
8699#endif
8700
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008701void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008702{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008703 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008704}
8705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008707void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008708 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008709 void *p_vrfy )
8710{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008711 conf->f_vrfy = f_vrfy;
8712 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008713}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008715
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008716void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008717 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008718 void *p_rng )
8719{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008720 conf->f_rng = f_rng;
8721 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008722}
8723
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008724void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008725 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008726 void *p_dbg )
8727{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008728 conf->f_dbg = f_dbg;
8729 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008730}
8731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008733 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008734 mbedtls_ssl_send_t *f_send,
8735 mbedtls_ssl_recv_t *f_recv,
8736 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008737{
8738 ssl->p_bio = p_bio;
8739 ssl->f_send = f_send;
8740 ssl->f_recv = f_recv;
8741 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008742}
8743
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008744#if defined(MBEDTLS_SSL_PROTO_DTLS)
8745void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8746{
8747 ssl->mtu = mtu;
8748}
8749#endif
8750
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008751void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008752{
8753 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008754}
8755
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008756void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8757 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008758 mbedtls_ssl_set_timer_t *f_set_timer,
8759 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008760{
8761 ssl->p_timer = p_timer;
8762 ssl->f_set_timer = f_set_timer;
8763 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008764
8765 /* Make sure we start with no timer running */
8766 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008767}
8768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008770void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008771 void *p_cache,
8772 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8773 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008774{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008775 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008776 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008777 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008778}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781#if defined(MBEDTLS_SSL_CLI_C)
8782int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008783{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008784 int ret;
8785
8786 if( ssl == NULL ||
8787 session == NULL ||
8788 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008789 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008792 }
8793
Hanno Becker52055ae2019-02-06 14:30:46 +00008794 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8795 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008796 return( ret );
8797
Paul Bakker0a597072012-09-25 21:55:46 +00008798 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008799
8800 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008801}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008803
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008804void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008805 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008806{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008807 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8808 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8809 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8810 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008811}
8812
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008813void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008814 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008815 int major, int minor )
8816{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008818 return;
8819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008820 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008821 return;
8822
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008823 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008824}
8825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008826#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008827void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008828 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008829{
8830 conf->cert_profile = profile;
8831}
8832
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008833/* Append a new keycert entry to a (possibly empty) list */
8834static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8835 mbedtls_x509_crt *cert,
8836 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008837{
niisato8ee24222018-06-25 19:05:48 +09008838 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008839
niisato8ee24222018-06-25 19:05:48 +09008840 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8841 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008842 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008843
niisato8ee24222018-06-25 19:05:48 +09008844 new_cert->cert = cert;
8845 new_cert->key = key;
8846 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008847
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008848 /* Update head is the list was null, else add to the end */
8849 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008850 {
niisato8ee24222018-06-25 19:05:48 +09008851 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008852 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008853 else
8854 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008855 mbedtls_ssl_key_cert *cur = *head;
8856 while( cur->next != NULL )
8857 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008858 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008859 }
8860
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008861 return( 0 );
8862}
8863
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008864int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008865 mbedtls_x509_crt *own_cert,
8866 mbedtls_pk_context *pk_key )
8867{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008868 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008869}
8870
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008871void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008872 mbedtls_x509_crt *ca_chain,
8873 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008874{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008875 conf->ca_chain = ca_chain;
8876 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008877
8878#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8879 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8880 * cannot be used together. */
8881 conf->f_ca_cb = NULL;
8882 conf->p_ca_cb = NULL;
8883#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008884}
Hanno Becker5adaad92019-03-27 16:54:37 +00008885
8886#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8887void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008888 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008889 void *p_ca_cb )
8890{
8891 conf->f_ca_cb = f_ca_cb;
8892 conf->p_ca_cb = p_ca_cb;
8893
8894 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8895 * cannot be used together. */
8896 conf->ca_chain = NULL;
8897 conf->ca_crl = NULL;
8898}
8899#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008900#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008901
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008902#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8903int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8904 mbedtls_x509_crt *own_cert,
8905 mbedtls_pk_context *pk_key )
8906{
8907 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8908 own_cert, pk_key ) );
8909}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008910
8911void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8912 mbedtls_x509_crt *ca_chain,
8913 mbedtls_x509_crl *ca_crl )
8914{
8915 ssl->handshake->sni_ca_chain = ca_chain;
8916 ssl->handshake->sni_ca_crl = ca_crl;
8917}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008918
8919void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8920 int authmode )
8921{
8922 ssl->handshake->sni_authmode = authmode;
8923}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008924#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8925
Hanno Becker8927c832019-04-03 12:52:50 +01008926#if defined(MBEDTLS_X509_CRT_PARSE_C)
8927void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8928 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8929 void *p_vrfy )
8930{
8931 ssl->f_vrfy = f_vrfy;
8932 ssl->p_vrfy = p_vrfy;
8933}
8934#endif
8935
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008936#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008937/*
8938 * Set EC J-PAKE password for current handshake
8939 */
8940int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8941 const unsigned char *pw,
8942 size_t pw_len )
8943{
8944 mbedtls_ecjpake_role role;
8945
Janos Follath8eb64132016-06-03 15:40:57 +01008946 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8948
8949 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8950 role = MBEDTLS_ECJPAKE_SERVER;
8951 else
8952 role = MBEDTLS_ECJPAKE_CLIENT;
8953
8954 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8955 role,
8956 MBEDTLS_MD_SHA256,
8957 MBEDTLS_ECP_DP_SECP256R1,
8958 pw, pw_len ) );
8959}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008960#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008963
8964static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8965{
8966 /* Remove reference to existing PSK, if any. */
8967#if defined(MBEDTLS_USE_PSA_CRYPTO)
8968 if( conf->psk_opaque != 0 )
8969 {
8970 /* The maintenance of the PSK key slot is the
8971 * user's responsibility. */
8972 conf->psk_opaque = 0;
8973 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008974 /* This and the following branch should never
8975 * be taken simultaenously as we maintain the
8976 * invariant that raw and opaque PSKs are never
8977 * configured simultaneously. As a safeguard,
8978 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008979#endif /* MBEDTLS_USE_PSA_CRYPTO */
8980 if( conf->psk != NULL )
8981 {
8982 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8983
8984 mbedtls_free( conf->psk );
8985 conf->psk = NULL;
8986 conf->psk_len = 0;
8987 }
8988
8989 /* Remove reference to PSK identity, if any. */
8990 if( conf->psk_identity != NULL )
8991 {
8992 mbedtls_free( conf->psk_identity );
8993 conf->psk_identity = NULL;
8994 conf->psk_identity_len = 0;
8995 }
8996}
8997
Hanno Becker7390c712018-11-15 13:33:04 +00008998/* This function assumes that PSK identity in the SSL config is unset.
8999 * It checks that the provided identity is well-formed and attempts
9000 * to make a copy of it in the SSL config.
9001 * On failure, the PSK identity in the config remains unset. */
9002static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
9003 unsigned char const *psk_identity,
9004 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009005{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009006 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00009007 if( psk_identity == NULL ||
9008 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009009 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009010 {
9011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9012 }
9013
Hanno Becker7390c712018-11-15 13:33:04 +00009014 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
9015 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009016 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02009017
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009018 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009019 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02009020
9021 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02009022}
9023
Hanno Becker7390c712018-11-15 13:33:04 +00009024int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9025 const unsigned char *psk, size_t psk_len,
9026 const unsigned char *psk_identity, size_t psk_identity_len )
9027{
9028 int ret;
9029 /* Remove opaque/raw PSK + PSK Identity */
9030 ssl_conf_remove_psk( conf );
9031
9032 /* Check and set raw PSK */
9033 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9035 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9036 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9037 conf->psk_len = psk_len;
9038 memcpy( conf->psk, psk, conf->psk_len );
9039
9040 /* Check and set PSK Identity */
9041 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9042 if( ret != 0 )
9043 ssl_conf_remove_psk( conf );
9044
9045 return( ret );
9046}
9047
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009048static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9049{
9050#if defined(MBEDTLS_USE_PSA_CRYPTO)
9051 if( ssl->handshake->psk_opaque != 0 )
9052 {
9053 ssl->handshake->psk_opaque = 0;
9054 }
9055 else
9056#endif /* MBEDTLS_USE_PSA_CRYPTO */
9057 if( ssl->handshake->psk != NULL )
9058 {
9059 mbedtls_platform_zeroize( ssl->handshake->psk,
9060 ssl->handshake->psk_len );
9061 mbedtls_free( ssl->handshake->psk );
9062 ssl->handshake->psk_len = 0;
9063 }
9064}
9065
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009066int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9067 const unsigned char *psk, size_t psk_len )
9068{
9069 if( psk == NULL || ssl->handshake == NULL )
9070 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9071
9072 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9074
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009075 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009076
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009077 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009078 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009079
9080 ssl->handshake->psk_len = psk_len;
9081 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9082
9083 return( 0 );
9084}
9085
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009086#if defined(MBEDTLS_USE_PSA_CRYPTO)
9087int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009088 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009089 const unsigned char *psk_identity,
9090 size_t psk_identity_len )
9091{
Hanno Becker7390c712018-11-15 13:33:04 +00009092 int ret;
9093 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009094 ssl_conf_remove_psk( conf );
9095
Hanno Becker7390c712018-11-15 13:33:04 +00009096 /* Check and set opaque PSK */
9097 if( psk_slot == 0 )
9098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009099 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009100
9101 /* Check and set PSK Identity */
9102 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9103 psk_identity_len );
9104 if( ret != 0 )
9105 ssl_conf_remove_psk( conf );
9106
9107 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009108}
9109
9110int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009111 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009112{
9113 if( psk_slot == 0 || ssl->handshake == NULL )
9114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9115
9116 ssl_remove_psk( ssl );
9117 ssl->handshake->psk_opaque = psk_slot;
9118 return( 0 );
9119}
9120#endif /* MBEDTLS_USE_PSA_CRYPTO */
9121
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009122void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009123 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009124 size_t),
9125 void *p_psk )
9126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009127 conf->f_psk = f_psk;
9128 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009130#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009131
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009132#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009133
9134#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009135int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009136{
9137 int ret;
9138
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009139 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9140 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9141 {
9142 mbedtls_mpi_free( &conf->dhm_P );
9143 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009144 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009145 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009146
9147 return( 0 );
9148}
Hanno Becker470a8c42017-10-04 15:28:46 +01009149#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009150
Hanno Beckera90658f2017-10-04 15:29:08 +01009151int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9152 const unsigned char *dhm_P, size_t P_len,
9153 const unsigned char *dhm_G, size_t G_len )
9154{
9155 int ret;
9156
9157 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9158 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9159 {
9160 mbedtls_mpi_free( &conf->dhm_P );
9161 mbedtls_mpi_free( &conf->dhm_G );
9162 return( ret );
9163 }
9164
9165 return( 0 );
9166}
Paul Bakker5121ce52009-01-03 21:22:43 +00009167
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009168int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009169{
9170 int ret;
9171
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009172 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9173 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9174 {
9175 mbedtls_mpi_free( &conf->dhm_P );
9176 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009177 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009178 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009179
9180 return( 0 );
9181}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009182#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009183
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009184#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9185/*
9186 * Set the minimum length for Diffie-Hellman parameters
9187 */
9188void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9189 unsigned int bitlen )
9190{
9191 conf->dhm_min_bitlen = bitlen;
9192}
9193#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9194
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009195#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009196/*
9197 * Set allowed/preferred hashes for handshake signatures
9198 */
9199void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9200 const int *hashes )
9201{
9202 conf->sig_hashes = hashes;
9203}
Hanno Becker947194e2017-04-07 13:25:49 +01009204#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009205
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009206#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009207/*
9208 * Set the allowed elliptic curves
9209 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009210void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009211 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009212{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009213 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009214}
Hanno Becker947194e2017-04-07 13:25:49 +01009215#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009216
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009217#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009219{
Hanno Becker947194e2017-04-07 13:25:49 +01009220 /* Initialize to suppress unnecessary compiler warning */
9221 size_t hostname_len = 0;
9222
9223 /* Check if new hostname is valid before
9224 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009225 if( hostname != NULL )
9226 {
9227 hostname_len = strlen( hostname );
9228
9229 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9231 }
9232
9233 /* Now it's clear that we will overwrite the old hostname,
9234 * so we can free it safely */
9235
9236 if( ssl->hostname != NULL )
9237 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009238 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009239 mbedtls_free( ssl->hostname );
9240 }
9241
9242 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009243
Paul Bakker5121ce52009-01-03 21:22:43 +00009244 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009245 {
9246 ssl->hostname = NULL;
9247 }
9248 else
9249 {
9250 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009251 if( ssl->hostname == NULL )
9252 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009253
Hanno Becker947194e2017-04-07 13:25:49 +01009254 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009255
Hanno Becker947194e2017-04-07 13:25:49 +01009256 ssl->hostname[hostname_len] = '\0';
9257 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009258
9259 return( 0 );
9260}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009261#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009262
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009263#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009264void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009266 const unsigned char *, size_t),
9267 void *p_sni )
9268{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009269 conf->f_sni = f_sni;
9270 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009271}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009272#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009275int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009276{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009277 size_t cur_len, tot_len;
9278 const char **p;
9279
9280 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009281 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9282 * MUST NOT be truncated."
9283 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009284 */
9285 tot_len = 0;
9286 for( p = protos; *p != NULL; p++ )
9287 {
9288 cur_len = strlen( *p );
9289 tot_len += cur_len;
9290
9291 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009293 }
9294
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009295 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009296
9297 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009298}
9299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009301{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009302 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009305
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009306void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009307{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009308 conf->max_major_ver = major;
9309 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009310}
9311
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009312void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009313{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009314 conf->min_major_ver = major;
9315 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009316}
9317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009319void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009320{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009321 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009322}
9323#endif
9324
Janos Follath088ce432017-04-10 12:42:31 +01009325#if defined(MBEDTLS_SSL_SRV_C)
9326void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9327 char cert_req_ca_list )
9328{
9329 conf->cert_req_ca_list = cert_req_ca_list;
9330}
9331#endif
9332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009334void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009335{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009336 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009337}
9338#endif
9339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009341void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009342{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009343 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009344}
9345#endif
9346
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009347#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009348void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009349{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009350 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009351}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009352#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009354#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009355int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009356{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009358 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009360 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009361 }
9362
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009363 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009364
9365 return( 0 );
9366}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009370void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009371{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009372 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009377void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009378{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009379 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009380}
9381#endif
9382
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009383void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009384{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009385 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009386}
9387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009389void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009390{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009391 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009392}
9393
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009394void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009395{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009396 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009397}
9398
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009399void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009400 const unsigned char period[8] )
9401{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009402 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009407#if defined(MBEDTLS_SSL_CLI_C)
9408void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009409{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009410 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009411}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009412#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009413
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009414#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009415void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9416 mbedtls_ssl_ticket_write_t *f_ticket_write,
9417 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9418 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009419{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009420 conf->f_ticket_write = f_ticket_write;
9421 conf->f_ticket_parse = f_ticket_parse;
9422 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009423}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009424#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009425#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009426
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009427#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9428void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9429 mbedtls_ssl_export_keys_t *f_export_keys,
9430 void *p_export_keys )
9431{
9432 conf->f_export_keys = f_export_keys;
9433 conf->p_export_keys = p_export_keys;
9434}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009435
9436void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9437 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9438 void *p_export_keys )
9439{
9440 conf->f_export_keys_ext = f_export_keys_ext;
9441 conf->p_export_keys = p_export_keys;
9442}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009443#endif
9444
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009445#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009446void mbedtls_ssl_conf_async_private_cb(
9447 mbedtls_ssl_config *conf,
9448 mbedtls_ssl_async_sign_t *f_async_sign,
9449 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9450 mbedtls_ssl_async_resume_t *f_async_resume,
9451 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009452 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009453{
9454 conf->f_async_sign_start = f_async_sign;
9455 conf->f_async_decrypt_start = f_async_decrypt;
9456 conf->f_async_resume = f_async_resume;
9457 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009458 conf->p_async_config_data = async_config_data;
9459}
9460
Gilles Peskine8f97af72018-04-26 11:46:10 +02009461void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9462{
9463 return( conf->p_async_config_data );
9464}
9465
Gilles Peskine1febfef2018-04-30 11:54:39 +02009466void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009467{
9468 if( ssl->handshake == NULL )
9469 return( NULL );
9470 else
9471 return( ssl->handshake->user_async_ctx );
9472}
9473
Gilles Peskine1febfef2018-04-30 11:54:39 +02009474void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009475 void *ctx )
9476{
9477 if( ssl->handshake != NULL )
9478 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009479}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009480#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009481
Paul Bakker5121ce52009-01-03 21:22:43 +00009482/*
9483 * SSL get accessors
9484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009486{
9487 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9488}
9489
Hanno Becker8b170a02017-10-10 11:51:19 +01009490int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9491{
9492 /*
9493 * Case A: We're currently holding back
9494 * a message for further processing.
9495 */
9496
9497 if( ssl->keep_current_message == 1 )
9498 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009499 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009500 return( 1 );
9501 }
9502
9503 /*
9504 * Case B: Further records are pending in the current datagram.
9505 */
9506
9507#if defined(MBEDTLS_SSL_PROTO_DTLS)
9508 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9509 ssl->in_left > ssl->next_record_offset )
9510 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009512 return( 1 );
9513 }
9514#endif /* MBEDTLS_SSL_PROTO_DTLS */
9515
9516 /*
9517 * Case C: A handshake message is being processed.
9518 */
9519
Hanno Becker8b170a02017-10-10 11:51:19 +01009520 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9521 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009523 return( 1 );
9524 }
9525
9526 /*
9527 * Case D: An application data message is being processed
9528 */
9529 if( ssl->in_offt != NULL )
9530 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009532 return( 1 );
9533 }
9534
9535 /*
9536 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009537 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009538 * we implement support for multiple alerts in single records.
9539 */
9540
9541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9542 return( 0 );
9543}
9544
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009545uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009546{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009547 if( ssl->session != NULL )
9548 return( ssl->session->verify_result );
9549
9550 if( ssl->session_negotiate != NULL )
9551 return( ssl->session_negotiate->verify_result );
9552
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009553 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009554}
9555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009557{
Paul Bakker926c8e42013-03-06 10:23:34 +01009558 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009559 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009562}
9563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009565{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009567 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009568 {
9569 switch( ssl->minor_ver )
9570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009571 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009572 return( "DTLSv1.0" );
9573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009575 return( "DTLSv1.2" );
9576
9577 default:
9578 return( "unknown (DTLS)" );
9579 }
9580 }
9581#endif
9582
Paul Bakker43ca69c2011-01-15 17:35:19 +00009583 switch( ssl->minor_ver )
9584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009586 return( "SSLv3.0" );
9587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009589 return( "TLSv1.0" );
9590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009592 return( "TLSv1.1" );
9593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009595 return( "TLSv1.2" );
9596
Paul Bakker43ca69c2011-01-15 17:35:19 +00009597 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009598 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009599 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009600}
9601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009603{
Hanno Becker3136ede2018-08-17 15:28:19 +01009604 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009606 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009607
Hanno Becker5903de42019-05-03 14:46:38 +01009608 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9609
Hanno Becker78640902018-08-13 16:35:15 +01009610 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009611 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613#if defined(MBEDTLS_ZLIB_SUPPORT)
9614 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9615 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009616#endif
9617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 case MBEDTLS_MODE_GCM:
9621 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009622 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009624 transform_expansion = transform->minlen;
9625 break;
9626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009628
9629 block_size = mbedtls_cipher_get_block_size(
9630 &transform->cipher_ctx_enc );
9631
Hanno Becker3136ede2018-08-17 15:28:19 +01009632 /* Expansion due to the addition of the MAC. */
9633 transform_expansion += transform->maclen;
9634
9635 /* Expansion due to the addition of CBC padding;
9636 * Theoretically up to 256 bytes, but we never use
9637 * more than the block size of the underlying cipher. */
9638 transform_expansion += block_size;
9639
9640 /* For TLS 1.1 or higher, an explicit IV is added
9641 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009642#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9643 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009644 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009645#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009646
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009647 break;
9648
9649 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009652 }
9653
Hanno Beckera0e20d02019-05-15 14:03:01 +01009654#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009655 if( transform->out_cid_len != 0 )
9656 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009657#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009658
Hanno Becker5903de42019-05-03 14:46:38 +01009659 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009660}
9661
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009662#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9663size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9664{
9665 size_t max_len;
9666
9667 /*
9668 * Assume mfl_code is correct since it was checked when set
9669 */
Angus Grattond8213d02016-05-25 20:56:48 +10009670 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009671
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009672 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009673 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009674 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009675 {
Angus Grattond8213d02016-05-25 20:56:48 +10009676 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009677 }
9678
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009679 /* During a handshake, use the value being negotiated */
9680 if( ssl->session_negotiate != NULL &&
9681 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9682 {
9683 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9684 }
9685
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009686 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009687}
9688#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9689
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009690#if defined(MBEDTLS_SSL_PROTO_DTLS)
9691static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9692{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009693 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9694 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9695 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9696 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9697 return ( 0 );
9698
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009699 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9700 return( ssl->mtu );
9701
9702 if( ssl->mtu == 0 )
9703 return( ssl->handshake->mtu );
9704
9705 return( ssl->mtu < ssl->handshake->mtu ?
9706 ssl->mtu : ssl->handshake->mtu );
9707}
9708#endif /* MBEDTLS_SSL_PROTO_DTLS */
9709
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009710int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9711{
9712 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9713
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009714#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9715 !defined(MBEDTLS_SSL_PROTO_DTLS)
9716 (void) ssl;
9717#endif
9718
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009719#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9720 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9721
9722 if( max_len > mfl )
9723 max_len = mfl;
9724#endif
9725
9726#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009727 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009728 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009729 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009730 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9731 const size_t overhead = (size_t) ret;
9732
9733 if( ret < 0 )
9734 return( ret );
9735
9736 if( mtu <= overhead )
9737 {
9738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9739 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9740 }
9741
9742 if( max_len > mtu - overhead )
9743 max_len = mtu - overhead;
9744 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009745#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009746
Hanno Becker0defedb2018-08-10 12:35:02 +01009747#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9748 !defined(MBEDTLS_SSL_PROTO_DTLS)
9749 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009750#endif
9751
9752 return( (int) max_len );
9753}
9754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009755#if defined(MBEDTLS_X509_CRT_PARSE_C)
9756const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009757{
9758 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009759 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009760
Hanno Beckere6824572019-02-07 13:18:46 +00009761#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009762 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009763#else
9764 return( NULL );
9765#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009766}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009770int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9771 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009772{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009773 if( ssl == NULL ||
9774 dst == NULL ||
9775 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009776 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009779 }
9780
Hanno Becker52055ae2019-02-06 14:30:46 +00009781 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009782}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009784
Paul Bakker5121ce52009-01-03 21:22:43 +00009785/*
Paul Bakker1961b702013-01-25 14:49:24 +01009786 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009789{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009791
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009792 if( ssl == NULL || ssl->conf == NULL )
9793 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009796 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009798#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009800 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009802#endif
9803
Paul Bakker1961b702013-01-25 14:49:24 +01009804 return( ret );
9805}
9806
9807/*
9808 * Perform the SSL handshake
9809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009811{
9812 int ret = 0;
9813
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009814 if( ssl == NULL || ssl->conf == NULL )
9815 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009822
9823 if( ret != 0 )
9824 break;
9825 }
9826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009828
9829 return( ret );
9830}
9831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832#if defined(MBEDTLS_SSL_RENEGOTIATION)
9833#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009834/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009835 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009838{
9839 int ret;
9840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009842
9843 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9845 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009846
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009847 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009848 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009850 return( ret );
9851 }
9852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009854
9855 return( 0 );
9856}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009858
9859/*
9860 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861 * - any side: calling mbedtls_ssl_renegotiate(),
9862 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9863 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009864 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009865 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009869{
9870 int ret;
9871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009873
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009874 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9875 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009876
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009877 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9878 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009880 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009881 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009882 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009883 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009884 ssl->handshake->out_msg_seq = 1;
9885 else
9886 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009887 }
9888#endif
9889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9891 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009895 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009896 return( ret );
9897 }
9898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009900
9901 return( 0 );
9902}
9903
9904/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009905 * Renegotiate current connection on client,
9906 * or request renegotiation on server
9907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009908int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009910 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009911
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009912 if( ssl == NULL || ssl->conf == NULL )
9913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009915#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009916 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009917 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9920 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009923
9924 /* Did we already try/start sending HelloRequest? */
9925 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009926 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009927
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009928 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009929 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009933 /*
9934 * On client, either start the renegotiation process or,
9935 * if already in progress, continue the handshake
9936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9940 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009941
9942 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009945 return( ret );
9946 }
9947 }
9948 else
9949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009953 return( ret );
9954 }
9955 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009957
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009958 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009959}
9960
9961/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009962 * Check record counters and renegotiate if they're above the limit.
9963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009965{
Andres AG2196c7f2016-12-15 17:01:16 +00009966 size_t ep_len = ssl_ep_len( ssl );
9967 int in_ctr_cmp;
9968 int out_ctr_cmp;
9969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009970 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9971 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009972 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009973 {
9974 return( 0 );
9975 }
9976
Andres AG2196c7f2016-12-15 17:01:16 +00009977 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9978 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009979 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009980 ssl->conf->renego_period + ep_len, 8 - ep_len );
9981
9982 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009983 {
9984 return( 0 );
9985 }
9986
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009989}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009991
9992/*
9993 * Receive application data decrypted from the SSL layer
9994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009996{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009997 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009998 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009999
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010000 if( ssl == NULL || ssl->conf == NULL )
10001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010006 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010008 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010009 return( ret );
10010
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010011 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010013 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010014 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010015 return( ret );
10016 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010017 }
10018#endif
10019
Hanno Becker4a810fb2017-05-24 16:27:30 +010010020 /*
10021 * Check if renegotiation is necessary and/or handshake is
10022 * in process. If yes, perform/continue, and fall through
10023 * if an unexpected packet is received while the client
10024 * is waiting for the ServerHello.
10025 *
10026 * (There is no equivalent to the last condition on
10027 * the server-side as it is not treated as within
10028 * a handshake while waiting for the ClientHello
10029 * after a renegotiation request.)
10030 */
10031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010032#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010033 ret = ssl_check_ctr_renegotiate( ssl );
10034 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10035 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010037 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010038 return( ret );
10039 }
10040#endif
10041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010042 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010044 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010045 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10046 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010049 return( ret );
10050 }
10051 }
10052
Hanno Beckere41158b2017-10-23 13:30:32 +010010053 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010054 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010055 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010056 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010057 if( ssl->f_get_timer != NULL &&
10058 ssl->f_get_timer( ssl->p_timer ) == -1 )
10059 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010060 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010061 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010062
Hanno Becker327c93b2018-08-15 13:56:18 +010010063 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010064 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010065 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10066 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010067
Hanno Becker4a810fb2017-05-24 16:27:30 +010010068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10069 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010070 }
10071
10072 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010073 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010074 {
10075 /*
10076 * OpenSSL sends empty messages to randomize the IV
10077 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010078 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010081 return( 0 );
10082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010084 return( ret );
10085 }
10086 }
10087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010091
Hanno Becker4a810fb2017-05-24 16:27:30 +010010092 /*
10093 * - For client-side, expect SERVER_HELLO_REQUEST.
10094 * - For server-side, expect CLIENT_HELLO.
10095 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10096 */
10097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010099 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010101 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010104
10105 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010106#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010107 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010108 {
10109 continue;
10110 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010111#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010112 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010113 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010114#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010115
Hanno Becker4a810fb2017-05-24 16:27:30 +010010116#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010117 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010118 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010121
10122 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010123#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010124 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010125 {
10126 continue;
10127 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010128#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010130 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010131#endif /* MBEDTLS_SSL_SRV_C */
10132
Hanno Becker21df7f92017-10-17 11:03:26 +010010133#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010134 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010135 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10136 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10137 ssl->conf->allow_legacy_renegotiation ==
10138 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10139 {
10140 /*
10141 * Accept renegotiation request
10142 */
Paul Bakker48916f92012-09-16 19:57:18 +000010143
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010144 /* DTLS clients need to know renego is server-initiated */
10145#if defined(MBEDTLS_SSL_PROTO_DTLS)
10146 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10147 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10148 {
10149 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10150 }
10151#endif
10152 ret = ssl_start_renegotiation( ssl );
10153 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10154 ret != 0 )
10155 {
10156 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10157 return( ret );
10158 }
10159 }
10160 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010161#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010162 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010163 /*
10164 * Refuse renegotiation
10165 */
10166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169#if defined(MBEDTLS_SSL_PROTO_SSL3)
10170 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010171 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010172 /* SSLv3 does not have a "no_renegotiation" warning, so
10173 we send a fatal alert and abort the connection. */
10174 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10175 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10176 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010177 }
10178 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010179#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10180#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10181 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10182 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10185 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10186 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010187 {
10188 return( ret );
10189 }
Paul Bakker48916f92012-09-16 19:57:18 +000010190 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010191 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10193 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010197 }
Paul Bakker48916f92012-09-16 19:57:18 +000010198 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010199
Hanno Becker90333da2017-10-10 11:27:13 +010010200 /* At this point, we don't know whether the renegotiation has been
10201 * completed or not. The cases to consider are the following:
10202 * 1) The renegotiation is complete. In this case, no new record
10203 * has been read yet.
10204 * 2) The renegotiation is incomplete because the client received
10205 * an application data record while awaiting the ServerHello.
10206 * 3) The renegotiation is incomplete because the client received
10207 * a non-handshake, non-application data message while awaiting
10208 * the ServerHello.
10209 * In each of these case, looping will be the proper action:
10210 * - For 1), the next iteration will read a new record and check
10211 * if it's application data.
10212 * - For 2), the loop condition isn't satisfied as application data
10213 * is present, hence continue is the same as break
10214 * - For 3), the loop condition is satisfied and read_record
10215 * will re-deliver the message that was held back by the client
10216 * when expecting the ServerHello.
10217 */
10218 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010219 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010220#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010222 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010223 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010224 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010225 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010228 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010229 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010230 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010231 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10236 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010239 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010240 }
10241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10245 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010246 }
10247
10248 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010249
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010250 /* We're going to return something now, cancel timer,
10251 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010252 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010253 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010254
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010255#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010256 /* If we requested renego but received AppData, resend HelloRequest.
10257 * Do it now, after setting in_offt, to avoid taking this branch
10258 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010259#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010260 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010262 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010263 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010265 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010266 return( ret );
10267 }
10268 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010269#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010270#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010271 }
10272
10273 n = ( len < ssl->in_msglen )
10274 ? len : ssl->in_msglen;
10275
10276 memcpy( buf, ssl->in_offt, n );
10277 ssl->in_msglen -= n;
10278
10279 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010280 {
10281 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010282 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010283 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010284 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010285 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010286 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010287 /* more data available */
10288 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010289 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010292
Paul Bakker23986e52011-04-24 08:57:21 +000010293 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010294}
10295
10296/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010297 * Send application data to be encrypted by the SSL layer, taking care of max
10298 * fragment length and buffer size.
10299 *
10300 * According to RFC 5246 Section 6.2.1:
10301 *
10302 * Zero-length fragments of Application data MAY be sent as they are
10303 * potentially useful as a traffic analysis countermeasure.
10304 *
10305 * Therefore, it is possible that the input message length is 0 and the
10306 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010307 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010308static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010309 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010310{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010311 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10312 const size_t max_len = (size_t) ret;
10313
10314 if( ret < 0 )
10315 {
10316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10317 return( ret );
10318 }
10319
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010320 if( len > max_len )
10321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010323 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010326 "maximum fragment length: %d > %d",
10327 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010329 }
10330 else
10331#endif
10332 len = max_len;
10333 }
Paul Bakker887bd502011-06-08 13:10:54 +000010334
Paul Bakker5121ce52009-01-03 21:22:43 +000010335 if( ssl->out_left != 0 )
10336 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010337 /*
10338 * The user has previously tried to send the data and
10339 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10340 * written. In this case, we expect the high-level write function
10341 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010346 return( ret );
10347 }
10348 }
Paul Bakker887bd502011-06-08 13:10:54 +000010349 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010350 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010351 /*
10352 * The user is trying to send a message the first time, so we need to
10353 * copy the data into the internal buffers and setup the data structure
10354 * to keep track of partial writes
10355 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010356 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010357 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010358 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010359
Hanno Becker67bc7c32018-08-06 11:33:50 +010010360 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010362 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010363 return( ret );
10364 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010365 }
10366
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010367 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010368}
10369
10370/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010371 * Write application data, doing 1/n-1 splitting if necessary.
10372 *
10373 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010374 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010375 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010377#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010378static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010379 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010380{
10381 int ret;
10382
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010383 if( ssl->conf->cbc_record_splitting ==
10384 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010385 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010386 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10387 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10388 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010389 {
10390 return( ssl_write_real( ssl, buf, len ) );
10391 }
10392
10393 if( ssl->split_done == 0 )
10394 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010395 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010396 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010397 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010398 }
10399
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010400 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10401 return( ret );
10402 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010403
10404 return( ret + 1 );
10405}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010406#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010407
10408/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010409 * Write application data (public-facing wrapper)
10410 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010411int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010412{
10413 int ret;
10414
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010416
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010417 if( ssl == NULL || ssl->conf == NULL )
10418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10419
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010420#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010421 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10422 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010423 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010424 return( ret );
10425 }
10426#endif
10427
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010428 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010429 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010430 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010431 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010433 return( ret );
10434 }
10435 }
10436
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010437#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010438 ret = ssl_write_split( ssl, buf, len );
10439#else
10440 ret = ssl_write_real( ssl, buf, len );
10441#endif
10442
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010444
10445 return( ret );
10446}
10447
10448/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010449 * Notify the peer that the connection is being closed
10450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010452{
10453 int ret;
10454
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010455 if( ssl == NULL || ssl->conf == NULL )
10456 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010459
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010460 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010463 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010465 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10466 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10467 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010469 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010470 return( ret );
10471 }
10472 }
10473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010475
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010476 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010477}
10478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010480{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010481 if( transform == NULL )
10482 return;
10483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010484#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010485 deflateEnd( &transform->ctx_deflate );
10486 inflateEnd( &transform->ctx_inflate );
10487#endif
10488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010489 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10490 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010491
Hanno Beckerd56ed242018-01-03 15:32:51 +000010492#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010493 mbedtls_md_free( &transform->md_ctx_enc );
10494 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010495#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010496
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010497 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010498}
10499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500#if defined(MBEDTLS_X509_CRT_PARSE_C)
10501static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010503 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010504
10505 while( cur != NULL )
10506 {
10507 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010508 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010509 cur = next;
10510 }
10511}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010512#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010513
Hanno Becker0271f962018-08-16 13:23:47 +010010514#if defined(MBEDTLS_SSL_PROTO_DTLS)
10515
10516static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10517{
10518 unsigned offset;
10519 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10520
10521 if( hs == NULL )
10522 return;
10523
Hanno Becker283f5ef2018-08-24 09:34:47 +010010524 ssl_free_buffered_record( ssl );
10525
Hanno Becker0271f962018-08-16 13:23:47 +010010526 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010527 ssl_buffering_free_slot( ssl, offset );
10528}
10529
10530static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10531 uint8_t slot )
10532{
10533 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10534 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010535
10536 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10537 return;
10538
Hanno Beckere605b192018-08-21 15:59:07 +010010539 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010540 {
Hanno Beckere605b192018-08-21 15:59:07 +010010541 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010542 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010543 mbedtls_free( hs_buf->data );
10544 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010545 }
10546}
10547
10548#endif /* MBEDTLS_SSL_PROTO_DTLS */
10549
Gilles Peskine9b562d52018-04-25 20:32:43 +020010550void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010551{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010552 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10553
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010554 if( handshake == NULL )
10555 return;
10556
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010557#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10558 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10559 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010560 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010561 handshake->async_in_progress = 0;
10562 }
10563#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10564
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010565#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10566 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10567 mbedtls_md5_free( &handshake->fin_md5 );
10568 mbedtls_sha1_free( &handshake->fin_sha1 );
10569#endif
10570#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10571#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010572#if defined(MBEDTLS_USE_PSA_CRYPTO)
10573 psa_hash_abort( &handshake->fin_sha256_psa );
10574#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010575 mbedtls_sha256_free( &handshake->fin_sha256 );
10576#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010577#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010578#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010579#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010580 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010581#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010582 mbedtls_sha512_free( &handshake->fin_sha512 );
10583#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010584#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010585#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010587#if defined(MBEDTLS_DHM_C)
10588 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010589#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010590#if defined(MBEDTLS_ECDH_C)
10591 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010592#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010593#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010594 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010595#if defined(MBEDTLS_SSL_CLI_C)
10596 mbedtls_free( handshake->ecjpake_cache );
10597 handshake->ecjpake_cache = NULL;
10598 handshake->ecjpake_cache_len = 0;
10599#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010600#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010601
Janos Follath4ae5c292016-02-10 11:27:43 +000010602#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10603 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010604 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010605 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010606#endif
10607
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010608#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10609 if( handshake->psk != NULL )
10610 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010611 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010612 mbedtls_free( handshake->psk );
10613 }
10614#endif
10615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010616#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10617 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010618 /*
10619 * Free only the linked list wrapper, not the keys themselves
10620 * since the belong to the SNI callback
10621 */
10622 if( handshake->sni_key_cert != NULL )
10623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010625
10626 while( cur != NULL )
10627 {
10628 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010630 cur = next;
10631 }
10632 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010633#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010634
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010635#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010636 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010637 if( handshake->ecrs_peer_cert != NULL )
10638 {
10639 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10640 mbedtls_free( handshake->ecrs_peer_cert );
10641 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010642#endif
10643
Hanno Becker75173122019-02-06 16:18:31 +000010644#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10645 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10646 mbedtls_pk_free( &handshake->peer_pubkey );
10647#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649#if defined(MBEDTLS_SSL_PROTO_DTLS)
10650 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010651 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010652 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010653#endif
10654
Hanno Becker4a63ed42019-01-08 11:39:35 +000010655#if defined(MBEDTLS_ECDH_C) && \
10656 defined(MBEDTLS_USE_PSA_CRYPTO)
10657 psa_destroy_key( handshake->ecdh_psa_privkey );
10658#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10659
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010660 mbedtls_platform_zeroize( handshake,
10661 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010662}
10663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010665{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010666 if( session == NULL )
10667 return;
10668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010669#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010670 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010671#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010672
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010673#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010674 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010675#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010676
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010677 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010678}
10679
Paul Bakker5121ce52009-01-03 21:22:43 +000010680/*
10681 * Free an SSL context
10682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010684{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010685 if( ssl == NULL )
10686 return;
10687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010689
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010690 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010691 {
Angus Grattond8213d02016-05-25 20:56:48 +100010692 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010693 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010694 }
10695
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010696 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010697 {
Angus Grattond8213d02016-05-25 20:56:48 +100010698 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010700 }
10701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010703 if( ssl->compress_buf != NULL )
10704 {
Angus Grattond8213d02016-05-25 20:56:48 +100010705 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010706 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010707 }
10708#endif
10709
Paul Bakker48916f92012-09-16 19:57:18 +000010710 if( ssl->transform )
10711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010712 mbedtls_ssl_transform_free( ssl->transform );
10713 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010714 }
10715
10716 if( ssl->handshake )
10717 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010718 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010719 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10720 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010722 mbedtls_free( ssl->handshake );
10723 mbedtls_free( ssl->transform_negotiate );
10724 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010725 }
10726
Paul Bakkerc0463502013-02-14 11:19:38 +010010727 if( ssl->session )
10728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729 mbedtls_ssl_session_free( ssl->session );
10730 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010731 }
10732
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010733#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010734 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010735 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010736 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010737 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010738 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010739#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010741#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10742 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10745 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010746 }
10747#endif
10748
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010749#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010750 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010751#endif
10752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010754
Paul Bakker86f04f42013-02-14 11:20:09 +010010755 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010756 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010757}
10758
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010759/*
10760 * Initialze mbedtls_ssl_config
10761 */
10762void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10763{
10764 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10765}
10766
Simon Butcherc97b6972015-12-27 23:48:17 +000010767#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010768static int ssl_preset_default_hashes[] = {
10769#if defined(MBEDTLS_SHA512_C)
10770 MBEDTLS_MD_SHA512,
10771 MBEDTLS_MD_SHA384,
10772#endif
10773#if defined(MBEDTLS_SHA256_C)
10774 MBEDTLS_MD_SHA256,
10775 MBEDTLS_MD_SHA224,
10776#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010777#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010778 MBEDTLS_MD_SHA1,
10779#endif
10780 MBEDTLS_MD_NONE
10781};
Simon Butcherc97b6972015-12-27 23:48:17 +000010782#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010783
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010784static int ssl_preset_suiteb_ciphersuites[] = {
10785 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10786 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10787 0
10788};
10789
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010790#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010791static int ssl_preset_suiteb_hashes[] = {
10792 MBEDTLS_MD_SHA256,
10793 MBEDTLS_MD_SHA384,
10794 MBEDTLS_MD_NONE
10795};
10796#endif
10797
10798#if defined(MBEDTLS_ECP_C)
10799static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010800#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010801 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010802#endif
10803#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010804 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010805#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010806 MBEDTLS_ECP_DP_NONE
10807};
10808#endif
10809
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010810/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010811 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010812 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010813int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010814 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010815{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010816#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010817 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010818#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010819
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010820 /* Use the functions here so that they are covered in tests,
10821 * but otherwise access member directly for efficiency */
10822 mbedtls_ssl_conf_endpoint( conf, endpoint );
10823 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010824
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010825 /*
10826 * Things that are common to all presets
10827 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010828#if defined(MBEDTLS_SSL_CLI_C)
10829 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10830 {
10831 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10832#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10833 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10834#endif
10835 }
10836#endif
10837
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010838#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010839 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010840#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010841
10842#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10843 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10844#endif
10845
10846#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10847 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10848#endif
10849
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010850#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10851 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10852#endif
10853
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010854#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010855 conf->f_cookie_write = ssl_cookie_write_dummy;
10856 conf->f_cookie_check = ssl_cookie_check_dummy;
10857#endif
10858
10859#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10860 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10861#endif
10862
Janos Follath088ce432017-04-10 12:42:31 +010010863#if defined(MBEDTLS_SSL_SRV_C)
10864 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10865#endif
10866
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010867#if defined(MBEDTLS_SSL_PROTO_DTLS)
10868 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10869 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10870#endif
10871
10872#if defined(MBEDTLS_SSL_RENEGOTIATION)
10873 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010874 memset( conf->renego_period, 0x00, 2 );
10875 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010876#endif
10877
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010878#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10879 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10880 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010881 const unsigned char dhm_p[] =
10882 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10883 const unsigned char dhm_g[] =
10884 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10885
Hanno Beckera90658f2017-10-04 15:29:08 +010010886 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10887 dhm_p, sizeof( dhm_p ),
10888 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010889 {
10890 return( ret );
10891 }
10892 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010893#endif
10894
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010895 /*
10896 * Preset-specific defaults
10897 */
10898 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010899 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010900 /*
10901 * NSA Suite B
10902 */
10903 case MBEDTLS_SSL_PRESET_SUITEB:
10904 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10905 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10906 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10907 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10908
10909 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10910 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10911 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10912 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10913 ssl_preset_suiteb_ciphersuites;
10914
10915#if defined(MBEDTLS_X509_CRT_PARSE_C)
10916 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010917#endif
10918
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010919#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010920 conf->sig_hashes = ssl_preset_suiteb_hashes;
10921#endif
10922
10923#if defined(MBEDTLS_ECP_C)
10924 conf->curve_list = ssl_preset_suiteb_curves;
10925#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010926 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010927
10928 /*
10929 * Default
10930 */
10931 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010932 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10933 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10934 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10935 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10936 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10937 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10938 MBEDTLS_SSL_MIN_MINOR_VERSION :
10939 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010940 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10941 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10942
10943#if defined(MBEDTLS_SSL_PROTO_DTLS)
10944 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10945 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10946#endif
10947
10948 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10949 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10950 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10951 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10952 mbedtls_ssl_list_ciphersuites();
10953
10954#if defined(MBEDTLS_X509_CRT_PARSE_C)
10955 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10956#endif
10957
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010958#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010959 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010960#endif
10961
10962#if defined(MBEDTLS_ECP_C)
10963 conf->curve_list = mbedtls_ecp_grp_id_list();
10964#endif
10965
10966#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10967 conf->dhm_min_bitlen = 1024;
10968#endif
10969 }
10970
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010971 return( 0 );
10972}
10973
10974/*
10975 * Free mbedtls_ssl_config
10976 */
10977void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10978{
10979#if defined(MBEDTLS_DHM_C)
10980 mbedtls_mpi_free( &conf->dhm_P );
10981 mbedtls_mpi_free( &conf->dhm_G );
10982#endif
10983
10984#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10985 if( conf->psk != NULL )
10986 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010987 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010988 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010989 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010990 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010991 }
10992
10993 if( conf->psk_identity != NULL )
10994 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010995 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010996 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010997 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010998 conf->psk_identity_len = 0;
10999 }
11000#endif
11001
11002#if defined(MBEDTLS_X509_CRT_PARSE_C)
11003 ssl_key_cert_free( conf->key_cert );
11004#endif
11005
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011006 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011007}
11008
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011009#if defined(MBEDTLS_PK_C) && \
11010 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011011/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011012 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011014unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011016#if defined(MBEDTLS_RSA_C)
11017 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
11018 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011019#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020#if defined(MBEDTLS_ECDSA_C)
11021 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
11022 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011023#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011025}
11026
Hanno Becker7e5437a2017-04-28 17:15:26 +010011027unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11028{
11029 switch( type ) {
11030 case MBEDTLS_PK_RSA:
11031 return( MBEDTLS_SSL_SIG_RSA );
11032 case MBEDTLS_PK_ECDSA:
11033 case MBEDTLS_PK_ECKEY:
11034 return( MBEDTLS_SSL_SIG_ECDSA );
11035 default:
11036 return( MBEDTLS_SSL_SIG_ANON );
11037 }
11038}
11039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011040mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011041{
11042 switch( sig )
11043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011044#if defined(MBEDTLS_RSA_C)
11045 case MBEDTLS_SSL_SIG_RSA:
11046 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011047#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011048#if defined(MBEDTLS_ECDSA_C)
11049 case MBEDTLS_SSL_SIG_ECDSA:
11050 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011051#endif
11052 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011053 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011054 }
11055}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011056#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011057
Hanno Becker7e5437a2017-04-28 17:15:26 +010011058#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11059 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11060
11061/* Find an entry in a signature-hash set matching a given hash algorithm. */
11062mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11063 mbedtls_pk_type_t sig_alg )
11064{
11065 switch( sig_alg )
11066 {
11067 case MBEDTLS_PK_RSA:
11068 return( set->rsa );
11069 case MBEDTLS_PK_ECDSA:
11070 return( set->ecdsa );
11071 default:
11072 return( MBEDTLS_MD_NONE );
11073 }
11074}
11075
11076/* Add a signature-hash-pair to a signature-hash set */
11077void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11078 mbedtls_pk_type_t sig_alg,
11079 mbedtls_md_type_t md_alg )
11080{
11081 switch( sig_alg )
11082 {
11083 case MBEDTLS_PK_RSA:
11084 if( set->rsa == MBEDTLS_MD_NONE )
11085 set->rsa = md_alg;
11086 break;
11087
11088 case MBEDTLS_PK_ECDSA:
11089 if( set->ecdsa == MBEDTLS_MD_NONE )
11090 set->ecdsa = md_alg;
11091 break;
11092
11093 default:
11094 break;
11095 }
11096}
11097
11098/* Allow exactly one hash algorithm for each signature. */
11099void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11100 mbedtls_md_type_t md_alg )
11101{
11102 set->rsa = md_alg;
11103 set->ecdsa = md_alg;
11104}
11105
11106#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11107 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11108
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011109/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011110 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011112mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011113{
11114 switch( hash )
11115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011116#if defined(MBEDTLS_MD5_C)
11117 case MBEDTLS_SSL_HASH_MD5:
11118 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011120#if defined(MBEDTLS_SHA1_C)
11121 case MBEDTLS_SSL_HASH_SHA1:
11122 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011124#if defined(MBEDTLS_SHA256_C)
11125 case MBEDTLS_SSL_HASH_SHA224:
11126 return( MBEDTLS_MD_SHA224 );
11127 case MBEDTLS_SSL_HASH_SHA256:
11128 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011129#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011130#if defined(MBEDTLS_SHA512_C)
11131 case MBEDTLS_SSL_HASH_SHA384:
11132 return( MBEDTLS_MD_SHA384 );
11133 case MBEDTLS_SSL_HASH_SHA512:
11134 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011135#endif
11136 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011137 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011138 }
11139}
11140
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011141/*
11142 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11143 */
11144unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11145{
11146 switch( md )
11147 {
11148#if defined(MBEDTLS_MD5_C)
11149 case MBEDTLS_MD_MD5:
11150 return( MBEDTLS_SSL_HASH_MD5 );
11151#endif
11152#if defined(MBEDTLS_SHA1_C)
11153 case MBEDTLS_MD_SHA1:
11154 return( MBEDTLS_SSL_HASH_SHA1 );
11155#endif
11156#if defined(MBEDTLS_SHA256_C)
11157 case MBEDTLS_MD_SHA224:
11158 return( MBEDTLS_SSL_HASH_SHA224 );
11159 case MBEDTLS_MD_SHA256:
11160 return( MBEDTLS_SSL_HASH_SHA256 );
11161#endif
11162#if defined(MBEDTLS_SHA512_C)
11163 case MBEDTLS_MD_SHA384:
11164 return( MBEDTLS_SSL_HASH_SHA384 );
11165 case MBEDTLS_MD_SHA512:
11166 return( MBEDTLS_SSL_HASH_SHA512 );
11167#endif
11168 default:
11169 return( MBEDTLS_SSL_HASH_NONE );
11170 }
11171}
11172
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011173#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011174/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011175 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011176 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011177 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011178int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011180 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011181
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011182 if( ssl->conf->curve_list == NULL )
11183 return( -1 );
11184
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011185 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011186 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011187 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011188
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011189 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011190}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011191#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011192
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011193#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011194/*
11195 * Check if a hash proposed by the peer is in our list.
11196 * Return 0 if we're willing to use it, -1 otherwise.
11197 */
11198int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11199 mbedtls_md_type_t md )
11200{
11201 const int *cur;
11202
11203 if( ssl->conf->sig_hashes == NULL )
11204 return( -1 );
11205
11206 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11207 if( *cur == (int) md )
11208 return( 0 );
11209
11210 return( -1 );
11211}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011212#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011214#if defined(MBEDTLS_X509_CRT_PARSE_C)
11215int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11216 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011217 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011218 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011219{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011220 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011221#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011222 int usage = 0;
11223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011224#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011225 const char *ext_oid;
11226 size_t ext_len;
11227#endif
11228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011229#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11230 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011231 ((void) cert);
11232 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011233 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011234#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011236#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11237 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011238 {
11239 /* Server part of the key exchange */
11240 switch( ciphersuite->key_exchange )
11241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011242 case MBEDTLS_KEY_EXCHANGE_RSA:
11243 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011244 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011245 break;
11246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011247 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11248 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11249 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11250 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011251 break;
11252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011253 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11254 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011255 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011256 break;
11257
11258 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011259 case MBEDTLS_KEY_EXCHANGE_NONE:
11260 case MBEDTLS_KEY_EXCHANGE_PSK:
11261 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11262 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011263 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011264 usage = 0;
11265 }
11266 }
11267 else
11268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011269 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11270 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011271 }
11272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011273 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011274 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011275 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011276 ret = -1;
11277 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011278#else
11279 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011280#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011282#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11283 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011285 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11286 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011287 }
11288 else
11289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011290 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11291 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011292 }
11293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011294 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011295 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011296 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011297 ret = -1;
11298 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011299#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011300
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011301 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011303#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011304
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011305/*
11306 * Convert version numbers to/from wire format
11307 * and, for DTLS, to/from TLS equivalent.
11308 *
11309 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011310 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011311 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11312 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011314void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011315 unsigned char ver[2] )
11316{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011317#if defined(MBEDTLS_SSL_PROTO_DTLS)
11318 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011320 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011321 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11322
11323 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11324 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11325 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011326 else
11327#else
11328 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011329#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011330 {
11331 ver[0] = (unsigned char) major;
11332 ver[1] = (unsigned char) minor;
11333 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011334}
11335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011336void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011337 const unsigned char ver[2] )
11338{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011339#if defined(MBEDTLS_SSL_PROTO_DTLS)
11340 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011341 {
11342 *major = 255 - ver[0] + 2;
11343 *minor = 255 - ver[1] + 1;
11344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011345 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011346 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11347 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011348 else
11349#else
11350 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011351#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011352 {
11353 *major = ver[0];
11354 *minor = ver[1];
11355 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011356}
11357
Simon Butcher99000142016-10-13 17:21:01 +010011358int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11359{
11360#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11361 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11362 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11363
11364 switch( md )
11365 {
11366#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11367#if defined(MBEDTLS_MD5_C)
11368 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011369 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011370#endif
11371#if defined(MBEDTLS_SHA1_C)
11372 case MBEDTLS_SSL_HASH_SHA1:
11373 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11374 break;
11375#endif
11376#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11377#if defined(MBEDTLS_SHA512_C)
11378 case MBEDTLS_SSL_HASH_SHA384:
11379 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11380 break;
11381#endif
11382#if defined(MBEDTLS_SHA256_C)
11383 case MBEDTLS_SSL_HASH_SHA256:
11384 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11385 break;
11386#endif
11387 default:
11388 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11389 }
11390
11391 return 0;
11392#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11393 (void) ssl;
11394 (void) md;
11395
11396 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11397#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11398}
11399
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011400#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11401 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11402int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11403 unsigned char *output,
11404 unsigned char *data, size_t data_len )
11405{
11406 int ret = 0;
11407 mbedtls_md5_context mbedtls_md5;
11408 mbedtls_sha1_context mbedtls_sha1;
11409
11410 mbedtls_md5_init( &mbedtls_md5 );
11411 mbedtls_sha1_init( &mbedtls_sha1 );
11412
11413 /*
11414 * digitally-signed struct {
11415 * opaque md5_hash[16];
11416 * opaque sha_hash[20];
11417 * };
11418 *
11419 * md5_hash
11420 * MD5(ClientHello.random + ServerHello.random
11421 * + ServerParams);
11422 * sha_hash
11423 * SHA(ClientHello.random + ServerHello.random
11424 * + ServerParams);
11425 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011426 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011427 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011429 goto exit;
11430 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011431 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011432 ssl->handshake->randbytes, 64 ) ) != 0 )
11433 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011435 goto exit;
11436 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011437 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011438 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011440 goto exit;
11441 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011442 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011443 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011445 goto exit;
11446 }
11447
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011448 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011449 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011451 goto exit;
11452 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011453 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011454 ssl->handshake->randbytes, 64 ) ) != 0 )
11455 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011457 goto exit;
11458 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011459 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011460 data_len ) ) != 0 )
11461 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011463 goto exit;
11464 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011465 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011466 output + 16 ) ) != 0 )
11467 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011469 goto exit;
11470 }
11471
11472exit:
11473 mbedtls_md5_free( &mbedtls_md5 );
11474 mbedtls_sha1_free( &mbedtls_sha1 );
11475
11476 if( ret != 0 )
11477 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11478 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11479
11480 return( ret );
11481
11482}
11483#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11484 MBEDTLS_SSL_PROTO_TLS1_1 */
11485
11486#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11487 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011488
11489#if defined(MBEDTLS_USE_PSA_CRYPTO)
11490int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11491 unsigned char *hash, size_t *hashlen,
11492 unsigned char *data, size_t data_len,
11493 mbedtls_md_type_t md_alg )
11494{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011495 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011496 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011497 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11498
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011499 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011500
11501 if( ( status = psa_hash_setup( &hash_operation,
11502 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011503 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011504 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011505 goto exit;
11506 }
11507
Andrzej Kurek814feff2019-01-14 04:35:19 -050011508 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11509 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011510 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011511 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011512 goto exit;
11513 }
11514
Andrzej Kurek814feff2019-01-14 04:35:19 -050011515 if( ( status = psa_hash_update( &hash_operation,
11516 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011517 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011518 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011519 goto exit;
11520 }
11521
Andrzej Kurek814feff2019-01-14 04:35:19 -050011522 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11523 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011524 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011525 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011526 goto exit;
11527 }
11528
11529exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011530 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011531 {
11532 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11533 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011534 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011535 {
11536 case PSA_ERROR_NOT_SUPPORTED:
11537 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011538 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011539 case PSA_ERROR_BUFFER_TOO_SMALL:
11540 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11541 case PSA_ERROR_INSUFFICIENT_MEMORY:
11542 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11543 default:
11544 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11545 }
11546 }
11547 return( 0 );
11548}
11549
11550#else
11551
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011552int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011553 unsigned char *hash, size_t *hashlen,
11554 unsigned char *data, size_t data_len,
11555 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011556{
11557 int ret = 0;
11558 mbedtls_md_context_t ctx;
11559 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011560 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011561
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011563
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011564 mbedtls_md_init( &ctx );
11565
11566 /*
11567 * digitally-signed struct {
11568 * opaque client_random[32];
11569 * opaque server_random[32];
11570 * ServerDHParams params;
11571 * };
11572 */
11573 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11574 {
11575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11576 goto exit;
11577 }
11578 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11579 {
11580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11581 goto exit;
11582 }
11583 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11584 {
11585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11586 goto exit;
11587 }
11588 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11589 {
11590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11591 goto exit;
11592 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011593 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011594 {
11595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11596 goto exit;
11597 }
11598
11599exit:
11600 mbedtls_md_free( &ctx );
11601
11602 if( ret != 0 )
11603 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11604 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11605
11606 return( ret );
11607}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011608#endif /* MBEDTLS_USE_PSA_CRYPTO */
11609
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011610#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11611 MBEDTLS_SSL_PROTO_TLS1_2 */
11612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011613#endif /* MBEDTLS_SSL_TLS_C */