blob: a78d6e570f50faea5b43fd5a6b6fc8b50a0800c1 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Hanno Becker2a43f6f2018-08-10 11:12:52 +010062static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010063static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010064
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010070 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010071#else
72 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010073#endif
74 return( 0 );
75}
76
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077/*
78 * Start a timer.
79 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020083 if( ssl->f_set_timer == NULL )
84 return;
85
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
87 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088}
89
90/*
91 * Return -1 is timer is expired, 0 if it isn't.
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020094{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020095 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020096 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020097
98 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020099 {
100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200102 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103
104 return( 0 );
105}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200106
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100107static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
108 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100109static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100110
Hanno Beckercfe45792019-07-03 16:13:00 +0100111#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +0100112static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
113 unsigned char *buf,
114 size_t len,
115 mbedtls_record *rec );
116
Hanno Beckercfe45792019-07-03 16:13:00 +0100117int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
118 unsigned char *buf,
119 size_t buflen )
120{
Hanno Becker54229812019-07-12 14:40:00 +0100121 int ret = 0;
122 mbedtls_record rec;
123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
124 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
125
126 /* We don't support record checking in TLS because
127 * (a) there doesn't seem to be a usecase for it, and
128 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
129 * and we'd need to backup the transform here.
130 */
131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
132 {
133 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
134 goto exit;
135 }
136#if defined(MBEDTLS_SSL_PROTO_DTLS)
137 else
138 {
139 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
140 if( ret != 0 )
141 {
142 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
143 goto exit;
144 }
145
146 if( ssl->transform_in != NULL )
147 {
148 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
149 if( ret != 0 )
150 {
151 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
152 goto exit;
153 }
154 }
155 }
156#endif /* MBEDTLS_SSL_PROTO_DTLS */
157
158exit:
159 /* On success, we have decrypted the buffer in-place, so make
160 * sure we don't leak any plaintext data. */
161 mbedtls_platform_zeroize( buf, buflen );
162
163 /* For the purpose of this API, treat messages with unexpected CID
164 * as well as such from future epochs as unexpected. */
165 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
166 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
167 {
168 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
169 }
170
171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
172 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100173}
174#endif /* MBEDTLS_SSL_RECORD_CHECKING */
175
Hanno Becker67bc7c32018-08-06 11:33:50 +0100176#define SSL_DONT_FORCE_FLUSH 0
177#define SSL_FORCE_FLUSH 1
178
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200179#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100180
Hanno Beckera0e20d02019-05-15 14:03:01 +0100181#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100182/* Top-level Connection ID API */
183
Hanno Becker8367ccc2019-05-14 11:30:10 +0100184int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
185 size_t len,
186 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100187{
188 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
190
Hanno Becker611ac772019-05-14 11:45:26 +0100191 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
192 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
193 {
194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
195 }
196
197 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100198 conf->cid_len = len;
199 return( 0 );
200}
201
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100202int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
203 int enable,
204 unsigned char const *own_cid,
205 size_t own_cid_len )
206{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100207 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
209
Hanno Beckerca092242019-04-25 16:01:49 +0100210 ssl->negotiate_cid = enable;
211 if( enable == MBEDTLS_SSL_CID_DISABLED )
212 {
213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
214 return( 0 );
215 }
216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100217 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100218
Hanno Beckerad4a1372019-05-03 13:06:44 +0100219 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100220 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
222 (unsigned) own_cid_len,
223 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
225 }
226
227 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100228 /* Truncation is not an issue here because
229 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
230 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100231
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100232 return( 0 );
233}
234
235int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
236 int *enabled,
237 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
238 size_t *peer_cid_len )
239{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100240 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100241
Hanno Becker76a79ab2019-05-03 14:38:32 +0100242 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
243 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
244 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100246 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100247
Hanno Beckerc5f24222019-05-03 12:54:52 +0100248 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
249 * were used, but client and server requested the empty CID.
250 * This is indistinguishable from not using the CID extension
251 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100252 if( ssl->transform_in->in_cid_len == 0 &&
253 ssl->transform_in->out_cid_len == 0 )
254 {
255 return( 0 );
256 }
257
Hanno Becker615ef172019-05-22 16:50:35 +0100258 if( peer_cid_len != NULL )
259 {
260 *peer_cid_len = ssl->transform_in->out_cid_len;
261 if( peer_cid != NULL )
262 {
263 memcpy( peer_cid, ssl->transform_in->out_cid,
264 ssl->transform_in->out_cid_len );
265 }
266 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100267
268 *enabled = MBEDTLS_SSL_CID_ENABLED;
269
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100270 return( 0 );
271}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100272#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100273
Hanno Beckerd5847772018-08-28 10:09:23 +0100274/* Forward declarations for functions related to message buffering. */
275static void ssl_buffering_free( mbedtls_ssl_context *ssl );
276static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
277 uint8_t slot );
278static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
279static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
280static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
281static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100282static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
283 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100284static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100285
Hanno Beckera67dee22018-08-22 10:05:20 +0100286static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100287static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100288{
Hanno Becker11682cc2018-08-22 14:41:02 +0100289 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100290
291 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100292 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100293
294 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
295}
296
Hanno Becker67bc7c32018-08-06 11:33:50 +0100297static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
298{
Hanno Becker11682cc2018-08-22 14:41:02 +0100299 size_t const bytes_written = ssl->out_left;
300 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100301
302 /* Double-check that the write-index hasn't gone
303 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100304 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100305 {
306 /* Should never happen... */
307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
308 }
309
310 return( (int) ( mtu - bytes_written ) );
311}
312
313static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
314{
315 int ret;
316 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400317 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100318
319#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
320 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
321
322 if( max_len > mfl )
323 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100324
325 /* By the standard (RFC 6066 Sect. 4), the MFL extension
326 * only limits the maximum record payload size, so in theory
327 * we would be allowed to pack multiple records of payload size
328 * MFL into a single datagram. However, this would mean that there's
329 * no way to explicitly communicate MTU restrictions to the peer.
330 *
331 * The following reduction of max_len makes sure that we never
332 * write datagrams larger than MFL + Record Expansion Overhead.
333 */
334 if( max_len <= ssl->out_left )
335 return( 0 );
336
337 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100338#endif
339
340 ret = ssl_get_remaining_space_in_datagram( ssl );
341 if( ret < 0 )
342 return( ret );
343 remaining = (size_t) ret;
344
345 ret = mbedtls_ssl_get_record_expansion( ssl );
346 if( ret < 0 )
347 return( ret );
348 expansion = (size_t) ret;
349
350 if( remaining <= expansion )
351 return( 0 );
352
353 remaining -= expansion;
354 if( remaining >= max_len )
355 remaining = max_len;
356
357 return( (int) remaining );
358}
359
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200360/*
361 * Double the retransmit timeout value, within the allowed range,
362 * returning -1 if the maximum value has already been reached.
363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200365{
366 uint32_t new_timeout;
367
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200368 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200369 return( -1 );
370
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200371 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
372 * in the following way: after the initial transmission and a first
373 * retransmission, back off to a temporary estimated MTU of 508 bytes.
374 * This value is guaranteed to be deliverable (if not guaranteed to be
375 * delivered) of any compliant IPv4 (and IPv6) network, and should work
376 * on most non-IP stacks too. */
377 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400378 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200379 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
381 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200382
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200383 new_timeout = 2 * ssl->handshake->retransmit_timeout;
384
385 /* Avoid arithmetic overflow and range overflow */
386 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200387 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200388 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200389 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200390 }
391
392 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200394 ssl->handshake->retransmit_timeout ) );
395
396 return( 0 );
397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200400{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200401 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200403 ssl->handshake->retransmit_timeout ) );
404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200408/*
409 * Convert max_fragment_length codes to length.
410 * RFC 6066 says:
411 * enum{
412 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
413 * } MaxFragmentLength;
414 * and we add 0 -> extension unused
415 */
Angus Grattond8213d02016-05-25 20:56:48 +1000416static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200417{
Angus Grattond8213d02016-05-25 20:56:48 +1000418 switch( mfl )
419 {
420 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
421 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
422 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
423 return 512;
424 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
425 return 1024;
426 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
427 return 2048;
428 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
429 return 4096;
430 default:
431 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
432 }
433}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200435
Hanno Becker52055ae2019-02-06 14:30:46 +0000436int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
437 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200438{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ssl_session_free( dst );
440 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000443
444#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200445 if( src->peer_cert != NULL )
446 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200447 int ret;
448
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200449 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200450 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200451 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200456 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200459 dst->peer_cert = NULL;
460 return( ret );
461 }
462 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000463#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000464 if( src->peer_cert_digest != NULL )
465 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000466 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000467 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000468 if( dst->peer_cert_digest == NULL )
469 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
470
471 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
472 src->peer_cert_digest_len );
473 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000474 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000475 }
476#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200479
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200480#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200481 if( src->ticket != NULL )
482 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200483 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200484 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200485 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200486
487 memcpy( dst->ticket, src->ticket, src->ticket_len );
488 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200489#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200490
491 return( 0 );
492}
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
495int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200496 const unsigned char *key_enc, const unsigned char *key_dec,
497 size_t keylen,
498 const unsigned char *iv_enc, const unsigned char *iv_dec,
499 size_t ivlen,
500 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200501 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
503int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
504int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
505int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
506int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
507#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000508
Paul Bakker5121ce52009-01-03 21:22:43 +0000509/*
510 * Key material generation
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200513static int ssl3_prf( const unsigned char *secret, size_t slen,
514 const char *label,
515 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000516 unsigned char *dstbuf, size_t dlen )
517{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100518 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000519 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200520 mbedtls_md5_context md5;
521 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000522 unsigned char padding[16];
523 unsigned char sha1sum[20];
524 ((void)label);
525
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200526 mbedtls_md5_init( &md5 );
527 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200528
Paul Bakker5f70b252012-09-13 14:23:06 +0000529 /*
530 * SSLv3:
531 * block =
532 * MD5( secret + SHA1( 'A' + secret + random ) ) +
533 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
534 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
535 * ...
536 */
537 for( i = 0; i < dlen / 16; i++ )
538 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200539 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000540
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100541 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100542 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100543 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100544 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100545 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100546 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100547 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100548 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100549 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100550 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000551
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100552 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100553 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100554 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100555 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100556 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100557 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100558 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100559 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000560 }
561
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100562exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200563 mbedtls_md5_free( &md5 );
564 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000565
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500566 mbedtls_platform_zeroize( padding, sizeof( padding ) );
567 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000568
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100569 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200574static int tls1_prf( const unsigned char *secret, size_t slen,
575 const char *label,
576 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000577 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578{
Paul Bakker23986e52011-04-24 08:57:21 +0000579 size_t nb, hs;
580 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200581 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300582 unsigned char *tmp;
583 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 const mbedtls_md_info_t *md_info;
586 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100587 int ret;
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Ron Eldor3b350852019-05-07 18:31:49 +0300591 tmp_len = 20 + strlen( label ) + rlen;
592 tmp = mbedtls_calloc( 1, tmp_len );
593 if( tmp == NULL )
594 {
595 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
596 goto exit;
597 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 hs = ( slen + 1 ) / 2;
600 S1 = secret;
601 S2 = secret + slen - hs;
602
603 nb = strlen( label );
604 memcpy( tmp + 20, label, nb );
605 memcpy( tmp + 20 + nb, random, rlen );
606 nb += rlen;
607
608 /*
609 * First compute P_md5(secret,label+random)[0..dlen]
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300612 {
613 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
614 goto exit;
615 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300618 {
619 goto exit;
620 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
623 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
624 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626 for( i = 0; i < dlen; i += 16 )
627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_md_hmac_reset ( &md_ctx );
629 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
630 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
634 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
637
638 for( j = 0; j < k; j++ )
639 dstbuf[i + j] = h_i[j];
640 }
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100643
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 /*
645 * XOR out with P_sha1(secret,label+random)[0..dlen]
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300648 {
649 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
650 goto exit;
651 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300654 {
655 goto exit;
656 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
659 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
660 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
662 for( i = 0; i < dlen; i += 20 )
663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_md_hmac_reset ( &md_ctx );
665 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
666 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_hmac_reset ( &md_ctx );
669 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
670 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
673
674 for( j = 0; j < k; j++ )
675 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
676 }
677
Ron Eldor3b350852019-05-07 18:31:49 +0300678exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100680
Ron Eldor3b350852019-05-07 18:31:49 +0300681 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500682 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Ron Eldor3b350852019-05-07 18:31:49 +0300684 mbedtls_free( tmp );
685 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500690#if defined(MBEDTLS_USE_PSA_CRYPTO)
691static int tls_prf_generic( mbedtls_md_type_t md_type,
692 const unsigned char *secret, size_t slen,
693 const char *label,
694 const unsigned char *random, size_t rlen,
695 unsigned char *dstbuf, size_t dlen )
696{
697 psa_status_t status;
698 psa_algorithm_t alg;
Janos Follath53b8ec22019-08-08 10:28:27 +0100699 psa_key_attributes_t key_attributes;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500700 psa_key_handle_t master_slot;
Janos Follathda6ac012019-08-16 13:47:29 +0100701 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100702 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500703
Andrzej Kurekc929a822019-01-14 03:51:11 -0500704 if( md_type == MBEDTLS_MD_SHA384 )
705 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
706 else
707 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
708
Janos Follath53b8ec22019-08-08 10:28:27 +0100709 key_attributes = psa_key_attributes_init();
710 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
711 psa_set_key_algorithm( &key_attributes, alg );
712 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500713
Janos Follath53b8ec22019-08-08 10:28:27 +0100714 status = psa_import_key( &key_attributes, secret, slen, &master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500715 if( status != PSA_SUCCESS )
716 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
717
Janos Follathda6ac012019-08-16 13:47:29 +0100718 status = psa_key_derivation( &derivation,
Andrzej Kurekc929a822019-01-14 03:51:11 -0500719 master_slot, alg,
720 random, rlen,
721 (unsigned char const *) label,
722 (size_t) strlen( label ),
723 dlen );
724 if( status != PSA_SUCCESS )
725 {
Janos Follathda6ac012019-08-16 13:47:29 +0100726 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500727 psa_destroy_key( master_slot );
728 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
729 }
730
Janos Follathda6ac012019-08-16 13:47:29 +0100731 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500732 if( status != PSA_SUCCESS )
733 {
Janos Follathda6ac012019-08-16 13:47:29 +0100734 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500735 psa_destroy_key( master_slot );
736 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
737 }
738
Janos Follathda6ac012019-08-16 13:47:29 +0100739 status = psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500740 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500741 {
742 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500743 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500744 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500745
746 status = psa_destroy_key( master_slot );
747 if( status != PSA_SUCCESS )
748 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
749
Andrzej Kurek33171262019-01-15 03:25:18 -0500750 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500751}
752
753#else /* MBEDTLS_USE_PSA_CRYPTO */
754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100756 const unsigned char *secret, size_t slen,
757 const char *label,
758 const unsigned char *random, size_t rlen,
759 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000760{
761 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100762 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300763 unsigned char *tmp;
764 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
Ron Eldor3b350852019-05-07 18:31:49 +0300777 tmp_len = md_len + strlen( label ) + rlen;
778 tmp = mbedtls_calloc( 1, tmp_len );
779 if( tmp == NULL )
780 {
781 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
782 goto exit;
783 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000784
785 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100786 memcpy( tmp + md_len, label, nb );
787 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000788 nb += rlen;
789
790 /*
791 * Compute P_<hash>(secret, label + random)[0..dlen]
792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300794 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
797 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
798 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100799
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100800 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_md_hmac_reset ( &md_ctx );
803 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
804 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_hmac_reset ( &md_ctx );
807 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
808 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000809
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100810 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000811
812 for( j = 0; j < k; j++ )
813 dstbuf[i + j] = h_i[j];
814 }
815
Ron Eldor3b350852019-05-07 18:31:49 +0300816exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100818
Ron Eldor3b350852019-05-07 18:31:49 +0300819 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500820 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000821
Ron Eldor3b350852019-05-07 18:31:49 +0300822 mbedtls_free( tmp );
823
824 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000825}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500826#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100828static int tls_prf_sha256( const unsigned char *secret, size_t slen,
829 const char *label,
830 const unsigned char *random, size_t rlen,
831 unsigned char *dstbuf, size_t dlen )
832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100834 label, random, rlen, dstbuf, dlen ) );
835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200839static int tls_prf_sha384( const unsigned char *secret, size_t slen,
840 const char *label,
841 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000842 unsigned char *dstbuf, size_t dlen )
843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100845 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_SHA512_C */
848#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
853 defined(MBEDTLS_SSL_PROTO_TLS1_1)
854static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#endif
Paul Bakker380da532012-04-18 16:10:25 +0000856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_SSL_PROTO_SSL3)
858static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
859static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200860#endif
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
863static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
864static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200865#endif
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
868#if defined(MBEDTLS_SHA256_C)
869static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
870static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
871static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_SHA512_C)
875static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
876static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
877static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000880
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100881#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100882 defined(MBEDTLS_USE_PSA_CRYPTO)
883static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
884{
885 if( ssl->conf->f_psk != NULL )
886 {
887 /* If we've used a callback to select the PSK,
888 * the static configuration is irrelevant. */
889 if( ssl->handshake->psk_opaque != 0 )
890 return( 1 );
891
892 return( 0 );
893 }
894
895 if( ssl->conf->psk_opaque != 0 )
896 return( 1 );
897
898 return( 0 );
899}
900#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100901 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100902
Ron Eldorcf280092019-05-14 20:19:13 +0300903#if defined(MBEDTLS_SSL_EXPORT_KEYS)
904static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
905{
906#if defined(MBEDTLS_SSL_PROTO_SSL3)
907 if( tls_prf == ssl3_prf )
908 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300909 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300910 }
911 else
912#endif
913#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
914 if( tls_prf == tls1_prf )
915 {
916 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
917 }
918 else
919#endif
920#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
921#if defined(MBEDTLS_SHA512_C)
922 if( tls_prf == tls_prf_sha384 )
923 {
924 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
925 }
926 else
927#endif
928#if defined(MBEDTLS_SHA256_C)
929 if( tls_prf == tls_prf_sha256 )
930 {
931 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
932 }
933 else
934#endif
935#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
936 return( MBEDTLS_SSL_TLS_PRF_NONE );
937}
938#endif /* MBEDTLS_SSL_EXPORT_KEYS */
939
Ron Eldor51d3ab52019-05-12 14:54:30 +0300940int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
941 const unsigned char *secret, size_t slen,
942 const char *label,
943 const unsigned char *random, size_t rlen,
944 unsigned char *dstbuf, size_t dlen )
945{
946 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
947
948 switch( prf )
949 {
950#if defined(MBEDTLS_SSL_PROTO_SSL3)
951 case MBEDTLS_SSL_TLS_PRF_SSL3:
952 tls_prf = ssl3_prf;
953 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300954#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300955#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
956 case MBEDTLS_SSL_TLS_PRF_TLS1:
957 tls_prf = tls1_prf;
958 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300959#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
960
961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300962#if defined(MBEDTLS_SHA512_C)
963 case MBEDTLS_SSL_TLS_PRF_SHA384:
964 tls_prf = tls_prf_sha384;
965 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300966#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300967#if defined(MBEDTLS_SHA256_C)
968 case MBEDTLS_SSL_TLS_PRF_SHA256:
969 tls_prf = tls_prf_sha256;
970 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300971#endif /* MBEDTLS_SHA256_C */
972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300973 default:
974 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
975 }
976
977 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
978}
979
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200980/*
981 * This function will ultimetaly only be responsible for populating a
982 * transform structure from data passed as explicit parameters.
983 *
984 * For now however it's doing rather more in a rather less explicit way.
985 */
986static int ssl_populate_transform( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000987{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200988 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000989#if defined(MBEDTLS_USE_PSA_CRYPTO)
990 int psa_fallthrough;
991#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 unsigned char keyblk[256];
994 unsigned char *key1;
995 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100996 unsigned char *mac_enc;
997 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000998 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200999 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001000 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001001 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 const mbedtls_cipher_info_t *cipher_info;
1003 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_ssl_session *session = ssl->session_negotiate;
1006 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
1007 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
Jaeden Amero2de07f12019-06-05 13:32:08 +01001011#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1012 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001013 transform->encrypt_then_mac = session->encrypt_then_mac;
1014#endif
1015 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001016
1017 ciphersuite_info = handshake->ciphersuite_info;
1018 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001019 if( cipher_info == NULL )
1020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001022 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001024 }
1025
Hanno Beckere694c3e2017-12-27 21:34:08 +00001026 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001027 if( md_info == NULL )
1028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001030 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001032 }
1033
Hanno Beckera0e20d02019-05-15 14:03:01 +01001034#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001035 /* Copy own and peer's CID if the use of the CID
1036 * extension has been negotiated. */
1037 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1038 {
1039 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001040
Hanno Becker05154c32019-05-03 15:23:51 +01001041 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001042 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001043 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001044 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001045
1046 transform->out_cid_len = ssl->handshake->peer_cid_len;
1047 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1048 ssl->handshake->peer_cid_len );
1049 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1050 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001051 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001052#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001053
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 * Swap the client and server random values.
1056 */
Paul Bakker48916f92012-09-16 19:57:18 +00001057 memcpy( tmp, handshake->randbytes, 64 );
1058 memcpy( handshake->randbytes, tmp + 32, 32 );
1059 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001060 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001061
1062 /*
1063 * SSLv3:
1064 * key block =
1065 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1066 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1067 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1068 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1069 * ...
1070 *
1071 * TLSv1:
1072 * key block = PRF( master, "key expansion", randbytes )
1073 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001074 ret = handshake->tls_prf( session->master, 48, "key expansion",
1075 handshake->randbytes, 64, keyblk, 256 );
1076 if( ret != 0 )
1077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001079 return( ret );
1080 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1083 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1084 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1085 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1086 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 /*
1089 * Determine the appropriate key, IV and MAC length.
1090 */
Paul Bakker68884e32013-01-07 18:20:04 +01001091
Hanno Becker88aaf652017-12-27 08:17:40 +00001092 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001093
Hanno Becker8031d062018-01-03 15:32:31 +00001094#if defined(MBEDTLS_GCM_C) || \
1095 defined(MBEDTLS_CCM_C) || \
1096 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001098 cipher_info->mode == MBEDTLS_MODE_CCM ||
1099 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001101 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001102
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001103 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001104 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001105 transform->taglen =
1106 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001107
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001108 /* All modes haves 96-bit IVs;
1109 * GCM and CCM has 4 implicit and 8 explicit bytes
1110 * ChachaPoly has all 12 bytes implicit
1111 */
Paul Bakker68884e32013-01-07 18:20:04 +01001112 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001113 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1114 transform->fixed_ivlen = 12;
1115 else
1116 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001117
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001118 /* Minimum length of encrypted record */
1119 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001120 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001121 }
1122 else
Hanno Becker8031d062018-01-03 15:32:31 +00001123#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1124#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1125 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1126 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001127 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001128 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1130 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001133 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001134 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001136 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001137 mac_key_len = mbedtls_md_get_size( md_info );
1138 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001141 /*
1142 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1143 * (rfc 6066 page 13 or rfc 2104 section 4),
1144 * so we only need to adjust the length here.
1145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001149
1150#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1151 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001152 * HMAC implementation which also truncates the key
1153 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001154 mac_key_len = transform->maclen;
1155#endif
1156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001158
1159 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001160 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001162 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001164 transform->minlen = transform->maclen;
1165 else
Paul Bakker68884e32013-01-07 18:20:04 +01001166 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001167 /*
1168 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001169 * 1. if EtM is in use: one block plus MAC
1170 * otherwise: * first multiple of blocklen greater than maclen
1171 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1174 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001175 {
1176 transform->minlen = transform->maclen
1177 + cipher_info->block_size;
1178 }
1179 else
1180#endif
1181 {
1182 transform->minlen = transform->maclen
1183 + cipher_info->block_size
1184 - transform->maclen % cipher_info->block_size;
1185 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1188 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1189 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001190 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001191 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1194 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1195 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001196 {
1197 transform->minlen += transform->ivlen;
1198 }
1199 else
1200#endif
1201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001203 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1204 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001205 }
Paul Bakker68884e32013-01-07 18:20:04 +01001206 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 }
Hanno Becker8031d062018-01-03 15:32:31 +00001208 else
1209#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1210 {
1211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1212 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
Hanno Becker88aaf652017-12-27 08:17:40 +00001215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1216 (unsigned) keylen,
1217 (unsigned) transform->minlen,
1218 (unsigned) transform->ivlen,
1219 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220
1221 /*
1222 * Finally setup the cipher contexts, IVs and MAC secrets.
1223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001225 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001227 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001228 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
Paul Bakker68884e32013-01-07 18:20:04 +01001230 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001231 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233 /*
1234 * This is not used in TLS v1.1.
1235 */
Paul Bakker48916f92012-09-16 19:57:18 +00001236 iv_copy_len = ( transform->fixed_ivlen ) ?
1237 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001238 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1239 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001240 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 }
1242 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#endif /* MBEDTLS_SSL_CLI_C */
1244#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001245 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001247 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001248 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Hanno Becker81c7b182017-11-09 18:39:33 +00001250 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001251 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001253 /*
1254 * This is not used in TLS v1.1.
1255 */
Paul Bakker48916f92012-09-16 19:57:18 +00001256 iv_copy_len = ( transform->fixed_ivlen ) ?
1257 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001258 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1259 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001260 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001262 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001266 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1267 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001268 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Hanno Beckerd56ed242018-01-03 15:32:51 +00001270#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#if defined(MBEDTLS_SSL_PROTO_SSL3)
1272 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001273 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001274 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001277 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1278 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001279 }
1280
Hanno Becker81c7b182017-11-09 18:39:33 +00001281 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1282 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001283 }
1284 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1286#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1287 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1288 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001289 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001290 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1291 For AEAD-based ciphersuites, there is nothing to do here. */
1292 if( mac_key_len != 0 )
1293 {
1294 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1295 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1296 }
Paul Bakker68884e32013-01-07 18:20:04 +01001297 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001298 else
1299#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001302 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1303 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001304 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001305#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1308 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001309 {
1310 int ret = 0;
1311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001313
Hanno Becker88aaf652017-12-27 08:17:40 +00001314 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001315 transform->iv_enc, transform->iv_dec,
1316 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001317 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001318 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001321 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1322 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001323 }
1324 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001325#else
1326 ((void) mac_dec);
1327 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001329
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001330#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1331 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001332 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001333 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1334 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001335 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001336 iv_copy_len );
1337 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001338
1339 if( ssl->conf->f_export_keys_ext != NULL )
1340 {
1341 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1342 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001343 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001344 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001345 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001346 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001347 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001348 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001349#endif
1350
Hanno Beckerf704bef2018-11-16 15:21:18 +00001351#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001352
1353 /* Only use PSA-based ciphers for TLS-1.2.
1354 * That's relevant at least for TLS-1.0, where
1355 * we assume that mbedtls_cipher_crypt() updates
1356 * the structure field for the IV, which the PSA-based
1357 * implementation currently doesn't. */
1358#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1359 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001360 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001361 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001362 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001363 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1364 {
1365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001366 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001367 }
1368
1369 if( ret == 0 )
1370 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001371 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001372 psa_fallthrough = 0;
1373 }
1374 else
1375 {
1376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1377 psa_fallthrough = 1;
1378 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001379 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001380 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001381 psa_fallthrough = 1;
1382#else
1383 psa_fallthrough = 1;
1384#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001385
Hanno Beckercb1cc802018-11-17 22:27:38 +00001386 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001387#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001388 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001389 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001392 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001393 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001394
Hanno Beckerf704bef2018-11-16 15:21:18 +00001395#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001396 /* Only use PSA-based ciphers for TLS-1.2.
1397 * That's relevant at least for TLS-1.0, where
1398 * we assume that mbedtls_cipher_crypt() updates
1399 * the structure field for the IV, which the PSA-based
1400 * implementation currently doesn't. */
1401#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1402 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001403 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001404 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001405 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001406 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1407 {
1408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001409 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001410 }
1411
1412 if( ret == 0 )
1413 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001414 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001415 psa_fallthrough = 0;
1416 }
1417 else
1418 {
1419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1420 psa_fallthrough = 1;
1421 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001422 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001423 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001424 psa_fallthrough = 1;
1425#else
1426 psa_fallthrough = 1;
1427#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001428
Hanno Beckercb1cc802018-11-17 22:27:38 +00001429 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001430#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001431 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001432 cipher_info ) ) != 0 )
1433 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001435 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001436 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001439 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001443 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001444 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001447 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001451 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001452 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_CIPHER_MODE_CBC)
1455 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1458 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001461 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001462 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1465 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001468 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001469 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001475 // Initialize compression
1476 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001478 {
Paul Bakker16770332013-10-11 09:59:44 +02001479 if( ssl->compress_buf == NULL )
1480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001482 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001483 if( ssl->compress_buf == NULL )
1484 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001486 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001487 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1488 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001489 }
1490 }
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001493
Paul Bakker48916f92012-09-16 19:57:18 +00001494 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1495 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001496
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001497 if( deflateInit( &transform->ctx_deflate,
1498 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001499 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001502 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1503 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001504 }
1505 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001509end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001510 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1511 mbedtls_platform_zeroize( handshake->randbytes,
1512 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001513 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514}
1515
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001516/*
1517 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
1518 *
1519 * Inputs:
1520 * - SSL/TLS minor version
1521 * - hash associated with the ciphersuite (only used by TLS 1.2)
1522 *
1523 * Ouputs:
1524 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1525 */
1526static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1527 int minor_ver,
1528 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001529{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001530#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1531 (void) hash;
1532#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001533
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001534#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001535 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001536 {
1537 handshake->tls_prf = ssl3_prf;
1538 handshake->calc_verify = ssl_calc_verify_ssl;
1539 handshake->calc_finished = ssl_calc_finished_ssl;
1540 }
1541 else
1542#endif
1543#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001544 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001545 {
1546 handshake->tls_prf = tls1_prf;
1547 handshake->calc_verify = ssl_calc_verify_tls;
1548 handshake->calc_finished = ssl_calc_finished_tls;
1549 }
1550 else
1551#endif
1552#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1553#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001554 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1555 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001556 {
1557 handshake->tls_prf = tls_prf_sha384;
1558 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1559 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1560 }
1561 else
1562#endif
1563#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001564 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001565 {
1566 handshake->tls_prf = tls_prf_sha256;
1567 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1568 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1569 }
1570 else
1571#endif
1572#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1573 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1575 }
1576
1577 return( 0 );
1578}
1579
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001580/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001581 * Compute master secret if needed
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001582 */
1583static int ssl_compute_master( mbedtls_ssl_context *ssl )
1584{
1585 int ret;
1586 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1587 mbedtls_ssl_session *session = ssl->session_negotiate;
1588 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
1589
1590 /* cf. RFC 5246, Section 8.1:
1591 * "The master secret is always exactly 48 bytes in length." */
1592 size_t const master_secret_len = 48;
1593
1594#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1595 unsigned char session_hash[48];
1596#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1597
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001598 /* The label for the KDF used for key expansion.
1599 * This is either "master secret" or "extended master secret"
1600 * depending on whether the Extended Master Secret extension
1601 * is used. */
1602 char const *lbl = "master secret";
1603
1604 /* The salt for the KDF used for key expansion.
1605 * - If the Extended Master Secret extension is not used,
1606 * this is ClientHello.Random + ServerHello.Random
1607 * (see Sect. 8.1 in RFC 5246).
1608 * - If the Extended Master Secret extension is used,
1609 * this is the transcript of the handshake so far.
1610 * (see Sect. 4 in RFC 7627). */
1611 unsigned char const *salt = handshake->randbytes;
1612 size_t salt_len = 64;
1613
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001614 if( handshake->resume != 0 )
1615 {
1616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001617 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001618 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001619
1620#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001621 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
1622 {
1623 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001624
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001625 lbl = "extended master secret";
1626 salt = session_hash;
1627 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001628#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001629 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1630 {
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001631#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001632 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1633 {
1634 salt_len = 48;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001635 }
1636 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001637#endif /* MBEDTLS_SHA512_C */
1638 salt_len = 32;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001639 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001640 else
1641#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1642 salt_len = 36;
1643
1644 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
1645 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001646#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1647
1648#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001649defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1650 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1651 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1652 ssl_use_opaque_psk( ssl ) == 1 )
1653 {
1654 /* Perform PSK-to-MS expansion in a single step. */
1655 psa_status_t status;
1656 psa_algorithm_t alg;
1657 psa_key_handle_t psk;
1658 psa_key_derivation_operation_t derivation =
1659 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001660
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001662
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001663 psk = ssl->conf->psk_opaque;
1664 if( ssl->handshake->psk_opaque != 0 )
1665 psk = ssl->handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001666
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001667 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1668 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001669 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001670 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1671
1672 status = psa_key_derivation( &derivation, psk, alg,
1673 salt, salt_len,
1674 (unsigned char const *) lbl,
1675 (size_t) strlen( lbl ),
1676 master_secret_len );
1677 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001678 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001679 psa_key_derivation_abort( &derivation );
1680 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001681 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001682
1683 status = psa_key_derivation_output_bytes( &derivation,
1684 session->master,
1685 master_secret_len );
1686 if( status != PSA_SUCCESS )
1687 {
1688 psa_key_derivation_abort( &derivation );
1689 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1690 }
1691
1692 status = psa_key_derivation_abort( &derivation );
1693 if( status != PSA_SUCCESS )
1694 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1695 }
1696 else
1697#endif
1698 {
1699 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1700 lbl, salt, salt_len,
1701 session->master,
1702 master_secret_len );
1703 if( ret != 0 )
1704 {
1705 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1706 return( ret );
1707 }
1708
1709 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1710 handshake->premaster,
1711 handshake->pmslen );
1712
1713 mbedtls_platform_zeroize( handshake->premaster,
1714 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001715 }
1716
1717 return( 0 );
1718}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001719
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001720int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1721{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001722 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001723 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1724 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001725
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001726 ret = ssl_set_handshake_prfs( ssl->handshake,
1727 ssl->minor_ver,
1728 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001729 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001730 {
1731 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001732 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001733 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001734
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001735 ret = ssl_compute_master( ssl );
1736 if( ret != 0 )
1737 {
1738 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1739 return( ret );
1740 }
1741
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001742 return( ssl_populate_transform( ssl ) );
1743}
1744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745#if defined(MBEDTLS_SSL_PROTO_SSL3)
1746void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001747{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001748 mbedtls_md5_context md5;
1749 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001750 unsigned char pad_1[48];
1751 unsigned char pad_2[48];
1752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001755 mbedtls_md5_init( &md5 );
1756 mbedtls_sha1_init( &sha1 );
1757
1758 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1759 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001760
Paul Bakker380da532012-04-18 16:10:25 +00001761 memset( pad_1, 0x36, 48 );
1762 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001763
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001764 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1765 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1766 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001768 mbedtls_md5_starts_ret( &md5 );
1769 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1770 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1771 mbedtls_md5_update_ret( &md5, hash, 16 );
1772 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001773
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001774 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1775 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1776 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001777
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001778 mbedtls_sha1_starts_ret( &sha1 );
1779 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1780 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1781 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1782 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001786
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001787 mbedtls_md5_free( &md5 );
1788 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001789
Paul Bakker380da532012-04-18 16:10:25 +00001790 return;
1791}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1795void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001796{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001797 mbedtls_md5_context md5;
1798 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001801
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001802 mbedtls_md5_init( &md5 );
1803 mbedtls_sha1_init( &sha1 );
1804
1805 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1806 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001807
Andrzej Kurekeb342242019-01-29 09:14:33 -05001808 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001809 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001813
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001814 mbedtls_md5_free( &md5 );
1815 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001816
Paul Bakker380da532012-04-18 16:10:25 +00001817 return;
1818}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1822#if defined(MBEDTLS_SHA256_C)
1823void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001824{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001825#if defined(MBEDTLS_USE_PSA_CRYPTO)
1826 size_t hash_size;
1827 psa_status_t status;
1828 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1829
1830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1831 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1832 if( status != PSA_SUCCESS )
1833 {
1834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1835 return;
1836 }
1837
1838 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1839 if( status != PSA_SUCCESS )
1840 {
1841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1842 return;
1843 }
1844 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1846#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001847 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001848
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001849 mbedtls_sha256_init( &sha256 );
1850
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001852
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001853 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001854 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001858
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001859 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001860#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001861 return;
1862}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865#if defined(MBEDTLS_SHA512_C)
1866void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001867{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001868#if defined(MBEDTLS_USE_PSA_CRYPTO)
1869 size_t hash_size;
1870 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001871 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001872
1873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001874 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001875 if( status != PSA_SUCCESS )
1876 {
1877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1878 return;
1879 }
1880
Andrzej Kurek972fba52019-01-30 03:29:12 -05001881 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001882 if( status != PSA_SUCCESS )
1883 {
1884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1885 return;
1886 }
1887 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1889#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001890 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001891
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001892 mbedtls_sha512_init( &sha512 );
1893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001895
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001896 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001897 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001901
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001902 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001903#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 return;
1905}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906#endif /* MBEDTLS_SHA512_C */
1907#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1910int 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 +02001911{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001912 unsigned char *p = ssl->handshake->premaster;
1913 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001914 const unsigned char *psk = ssl->conf->psk;
1915 size_t psk_len = ssl->conf->psk_len;
1916
1917 /* If the psk callback was called, use its result */
1918 if( ssl->handshake->psk != NULL )
1919 {
1920 psk = ssl->handshake->psk;
1921 psk_len = ssl->handshake->psk_len;
1922 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001923
1924 /*
1925 * PMS = struct {
1926 * opaque other_secret<0..2^16-1>;
1927 * opaque psk<0..2^16-1>;
1928 * };
1929 * with "other_secret" depending on the particular key exchange
1930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1932 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001933 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001934 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001936
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001937 *(p++) = (unsigned char)( psk_len >> 8 );
1938 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001939
1940 if( end < p || (size_t)( end - p ) < psk_len )
1941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1942
1943 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001944 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001945 }
1946 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1948#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1949 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001950 {
1951 /*
1952 * other_secret already set by the ClientKeyExchange message,
1953 * and is 48 bytes long
1954 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001955 if( end - p < 2 )
1956 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1957
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001958 *p++ = 0;
1959 *p++ = 48;
1960 p += 48;
1961 }
1962 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1964#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1965 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001966 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001967 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001968 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001969
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001970 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001972 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001973 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001976 return( ret );
1977 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001978 *(p++) = (unsigned char)( len >> 8 );
1979 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001980 p += len;
1981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001983 }
1984 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1986#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1987 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001988 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001989 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001990 size_t zlen;
1991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001993 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001994 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001997 return( ret );
1998 }
1999
2000 *(p++) = (unsigned char)( zlen >> 8 );
2001 *(p++) = (unsigned char)( zlen );
2002 p += zlen;
2003
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002004 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2005 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002006 }
2007 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002012 }
2013
2014 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002015 if( end - p < 2 )
2016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002017
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002018 *(p++) = (unsigned char)( psk_len >> 8 );
2019 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002020
2021 if( end < p || (size_t)( end - p ) < psk_len )
2022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2023
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002024 memcpy( p, psk, psk_len );
2025 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002026
2027 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2028
2029 return( 0 );
2030}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002034/*
2035 * SSLv3.0 MAC functions
2036 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002037#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002038static void ssl_mac( mbedtls_md_context_t *md_ctx,
2039 const unsigned char *secret,
2040 const unsigned char *buf, size_t len,
2041 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002042 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002043{
2044 unsigned char header[11];
2045 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002046 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2048 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002049
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002050 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002052 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002053 else
Paul Bakker68884e32013-01-07 18:20:04 +01002054 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002055
2056 memcpy( header, ctr, 8 );
2057 header[ 8] = (unsigned char) type;
2058 header[ 9] = (unsigned char)( len >> 8 );
2059 header[10] = (unsigned char)( len );
2060
Paul Bakker68884e32013-01-07 18:20:04 +01002061 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 mbedtls_md_starts( md_ctx );
2063 mbedtls_md_update( md_ctx, secret, md_size );
2064 mbedtls_md_update( md_ctx, padding, padlen );
2065 mbedtls_md_update( md_ctx, header, 11 );
2066 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002067 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002068
Paul Bakker68884e32013-01-07 18:20:04 +01002069 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 mbedtls_md_starts( md_ctx );
2071 mbedtls_md_update( md_ctx, secret, md_size );
2072 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002073 mbedtls_md_update( md_ctx, out, md_size );
2074 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002077
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002078/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002079 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002080#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002081 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2082 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2083 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2084/* This function makes sure every byte in the memory region is accessed
2085 * (in ascending addresses order) */
2086static void ssl_read_memory( unsigned char *p, size_t len )
2087{
2088 unsigned char acc = 0;
2089 volatile unsigned char force;
2090
2091 for( ; len != 0; p++, len-- )
2092 acc ^= *p;
2093
2094 force = acc;
2095 (void) force;
2096}
2097#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2098
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002099/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002101 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002102
Hanno Beckera0e20d02019-05-15 14:03:01 +01002103#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002104/* This functions transforms a DTLS plaintext fragment and a record content
2105 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002106 *
2107 * struct {
2108 * opaque content[DTLSPlaintext.length];
2109 * ContentType real_type;
2110 * uint8 zeros[length_of_padding];
2111 * } DTLSInnerPlaintext;
2112 *
2113 * Input:
2114 * - `content`: The beginning of the buffer holding the
2115 * plaintext to be wrapped.
2116 * - `*content_size`: The length of the plaintext in Bytes.
2117 * - `max_len`: The number of Bytes available starting from
2118 * `content`. This must be `>= *content_size`.
2119 * - `rec_type`: The desired record content type.
2120 *
2121 * Output:
2122 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2123 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2124 *
2125 * Returns:
2126 * - `0` on success.
2127 * - A negative error code if `max_len` didn't offer enough space
2128 * for the expansion.
2129 */
2130static int ssl_cid_build_inner_plaintext( unsigned char *content,
2131 size_t *content_size,
2132 size_t remaining,
2133 uint8_t rec_type )
2134{
2135 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002136 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2137 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2138 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002139
2140 /* Write real content type */
2141 if( remaining == 0 )
2142 return( -1 );
2143 content[ len ] = rec_type;
2144 len++;
2145 remaining--;
2146
2147 if( remaining < pad )
2148 return( -1 );
2149 memset( content + len, 0, pad );
2150 len += pad;
2151 remaining -= pad;
2152
2153 *content_size = len;
2154 return( 0 );
2155}
2156
Hanno Becker07dc97d2019-05-20 15:08:01 +01002157/* This function parses a DTLSInnerPlaintext structure.
2158 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002159static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2160 size_t *content_size,
2161 uint8_t *rec_type )
2162{
2163 size_t remaining = *content_size;
2164
2165 /* Determine length of padding by skipping zeroes from the back. */
2166 do
2167 {
2168 if( remaining == 0 )
2169 return( -1 );
2170 remaining--;
2171 } while( content[ remaining ] == 0 );
2172
2173 *content_size = remaining;
2174 *rec_type = content[ remaining ];
2175
2176 return( 0 );
2177}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002178#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002179
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002180/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002181 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002182static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002183 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002184 mbedtls_record *rec )
2185{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002186 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002187 *
2188 * additional_data = seq_num + TLSCompressed.type +
2189 * TLSCompressed.version + TLSCompressed.length;
2190 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002191 * For the CID extension, this is extended as follows
2192 * (quoting draft-ietf-tls-dtls-connection-id-05,
2193 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002194 *
2195 * additional_data = seq_num + DTLSPlaintext.type +
2196 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002197 * cid +
2198 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002199 * length_of_DTLSInnerPlaintext;
2200 */
2201
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002202 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2203 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002204 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002205
Hanno Beckera0e20d02019-05-15 14:03:01 +01002206#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002207 if( rec->cid_len != 0 )
2208 {
2209 memcpy( add_data + 11, rec->cid, rec->cid_len );
2210 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2211 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2212 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2213 *add_data_len = 13 + 1 + rec->cid_len;
2214 }
2215 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002216#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002217 {
2218 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2219 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2220 *add_data_len = 13;
2221 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002222}
2223
Hanno Beckera18d1322018-01-03 14:27:32 +00002224int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2225 mbedtls_ssl_transform *transform,
2226 mbedtls_record *rec,
2227 int (*f_rng)(void *, unsigned char *, size_t),
2228 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002231 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002232 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002233 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002234 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002235 size_t post_avail;
2236
2237 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002238#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002239 ((void) ssl);
2240#endif
2241
2242 /* The PRNG is used for dynamic IV generation that's used
2243 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2244#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2245 ( defined(MBEDTLS_AES_C) || \
2246 defined(MBEDTLS_ARIA_C) || \
2247 defined(MBEDTLS_CAMELLIA_C) ) && \
2248 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2249 ((void) f_rng);
2250 ((void) p_rng);
2251#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002255 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002256 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2258 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2259 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002260 if( rec == NULL
2261 || rec->buf == NULL
2262 || rec->buf_len < rec->data_offset
2263 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002264#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002265 || rec->cid_len != 0
2266#endif
2267 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002268 {
2269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002271 }
2272
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002273 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002274 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002276 data, rec->data_len );
2277
2278 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2279
2280 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2281 {
2282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2283 (unsigned) rec->data_len,
2284 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2286 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002287
Hanno Beckera0e20d02019-05-15 14:03:01 +01002288#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002289 /*
2290 * Add CID information
2291 */
2292 rec->cid_len = transform->out_cid_len;
2293 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2294 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002295
2296 if( rec->cid_len != 0 )
2297 {
2298 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002299 * Wrap plaintext into DTLSInnerPlaintext structure.
2300 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002301 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002302 * Note that this changes `rec->data_len`, and hence
2303 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002304 */
2305 if( ssl_cid_build_inner_plaintext( data,
2306 &rec->data_len,
2307 post_avail,
2308 rec->type ) != 0 )
2309 {
2310 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2311 }
2312
2313 rec->type = MBEDTLS_SSL_MSG_CID;
2314 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002315#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002316
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002317 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2318
Paul Bakker5121ce52009-01-03 21:22:43 +00002319 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002320 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 */
Hanno Becker52344c22018-01-03 15:24:20 +00002322#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 if( mode == MBEDTLS_MODE_STREAM ||
2324 ( mode == MBEDTLS_MODE_CBC
2325#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002326 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002327#endif
2328 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002329 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002330 if( post_avail < transform->maclen )
2331 {
2332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2333 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2334 }
2335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002337 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002338 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002339 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002340 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2341 data, rec->data_len, rec->ctr, rec->type, mac );
2342 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002343 }
2344 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002345#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2347 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002348 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002349 {
Hanno Becker992b6872017-11-09 18:57:39 +00002350 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2351
Hanno Beckercab87e62019-04-29 13:52:53 +01002352 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002353
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002354 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002355 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002356 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2357 data, rec->data_len );
2358 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2359 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2360
2361 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002362 }
2363 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002364#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2367 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002368 }
2369
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002370 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2371 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002372
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002373 rec->data_len += transform->maclen;
2374 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002375 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002376 }
Hanno Becker52344c22018-01-03 15:24:20 +00002377#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002379 /*
2380 * Encrypt
2381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2383 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002385 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002386 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002388 "including %d bytes of padding",
2389 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002390
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002391 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2392 transform->iv_enc, transform->ivlen,
2393 data, rec->data_len,
2394 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002397 return( ret );
2398 }
2399
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002400 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2403 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 }
Paul Bakker68884e32013-01-07 18:20:04 +01002406 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002408
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002409#if defined(MBEDTLS_GCM_C) || \
2410 defined(MBEDTLS_CCM_C) || \
2411 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002413 mode == MBEDTLS_MODE_CCM ||
2414 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002415 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002416 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002417 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002418 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002419
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002420 /* Check that there's space for both the authentication tag
2421 * and the explicit IV before and after the record content. */
2422 if( post_avail < transform->taglen ||
2423 rec->data_offset < explicit_iv_len )
2424 {
2425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2426 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2427 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002428
Paul Bakker68884e32013-01-07 18:20:04 +01002429 /*
2430 * Generate IV
2431 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002432 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2433 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002434 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002435 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002436 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2437 explicit_iv_len );
2438 /* Prefix record content with explicit IV. */
2439 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002440 }
2441 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2442 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002443 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002444 unsigned char i;
2445
2446 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2447
2448 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002449 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002450 }
2451 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002452 {
2453 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002456 }
2457
Hanno Beckercab87e62019-04-29 13:52:53 +01002458 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002459
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002460 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2461 iv, transform->ivlen );
2462 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002463 data - explicit_iv_len, explicit_iv_len );
2464 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002465 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002467 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002468 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002469
Paul Bakker68884e32013-01-07 18:20:04 +01002470 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002471 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002472 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002473
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002474 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002475 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002476 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002477 data, rec->data_len, /* source */
2478 data, &rec->data_len, /* destination */
2479 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002482 return( ret );
2483 }
2484
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002485 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2486 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002487
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002488 rec->data_len += transform->taglen + explicit_iv_len;
2489 rec->data_offset -= explicit_iv_len;
2490 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002491 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2495#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002496 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002499 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002500 size_t padlen, i;
2501 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002502
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002503 /* Currently we're always using minimal padding
2504 * (up to 255 bytes would be allowed). */
2505 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2506 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 padlen = 0;
2508
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002509 /* Check there's enough space in the buffer for the padding. */
2510 if( post_avail < padlen + 1 )
2511 {
2512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2513 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2514 }
2515
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002517 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002519 rec->data_len += padlen + 1;
2520 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002523 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002524 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2525 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002526 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002527 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002528 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002529 if( f_rng == NULL )
2530 {
2531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2532 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2533 }
2534
2535 if( rec->data_offset < transform->ivlen )
2536 {
2537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2538 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2539 }
2540
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002541 /*
2542 * Generate IV
2543 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002544 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002545 if( ret != 0 )
2546 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002547
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002548 memcpy( data - transform->ivlen, transform->iv_enc,
2549 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002550
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002551 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002555 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002556 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002557 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002559 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2560 transform->iv_enc,
2561 transform->ivlen,
2562 data, rec->data_len,
2563 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002566 return( ret );
2567 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002568
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002569 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002573 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002576 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002577 {
2578 /*
2579 * Save IV in SSL3 and TLS1
2580 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002581 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2582 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002584 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002585#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002586 {
2587 data -= transform->ivlen;
2588 rec->data_offset -= transform->ivlen;
2589 rec->data_len += transform->ivlen;
2590 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002593 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002594 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002595 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2596
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002597 /*
2598 * MAC(MAC_write_key, seq_num +
2599 * TLSCipherText.type +
2600 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002601 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002602 * IV + // except for TLS 1.0
2603 * ENC(content + padding + padding_length));
2604 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002605
2606 if( post_avail < transform->maclen)
2607 {
2608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2609 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2610 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002611
Hanno Beckercab87e62019-04-29 13:52:53 +01002612 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002615 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002616 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002617
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002618 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002619 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002620 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2621 data, rec->data_len );
2622 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2623 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002624
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002625 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002626
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002627 rec->data_len += transform->maclen;
2628 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002629 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002630 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002633 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002635 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2638 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002639 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002641 /* Make extra sure authentication was performed, exactly once */
2642 if( auth_done != 1 )
2643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2645 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002646 }
2647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
2650 return( 0 );
2651}
2652
Hanno Becker605949f2019-07-12 08:23:59 +01002653int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002654 mbedtls_ssl_transform *transform,
2655 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002656{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002657 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002659 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002660#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002661 size_t padlen = 0, correct = 1;
2662#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002663 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002664 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002665 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002666
Hanno Beckera18d1322018-01-03 14:27:32 +00002667#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002668 ((void) ssl);
2669#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002672 if( rec == NULL ||
2673 rec->buf == NULL ||
2674 rec->buf_len < rec->data_offset ||
2675 rec->buf_len - rec->data_offset < rec->data_len )
2676 {
2677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002679 }
2680
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002681 data = rec->buf + rec->data_offset;
2682 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
Hanno Beckera0e20d02019-05-15 14:03:01 +01002684#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002685 /*
2686 * Match record's CID with incoming CID.
2687 */
Hanno Becker938489a2019-05-08 13:02:22 +01002688 if( rec->cid_len != transform->in_cid_len ||
2689 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2690 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002691 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002692 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002693#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2696 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002697 {
2698 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002699 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2700 transform->iv_dec,
2701 transform->ivlen,
2702 data, rec->data_len,
2703 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002706 return( ret );
2707 }
2708
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002709 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002713 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 }
Paul Bakker68884e32013-01-07 18:20:04 +01002715 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002717#if defined(MBEDTLS_GCM_C) || \
2718 defined(MBEDTLS_CCM_C) || \
2719 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002721 mode == MBEDTLS_MODE_CCM ||
2722 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002723 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002724 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002725 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002726
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002727 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002728 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002729 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002730
2731 /* Check that there's enough space for the explicit IV
2732 * (at the beginning of the record) and the MAC (at the
2733 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002734 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002737 "+ taglen (%d)", rec->data_len,
2738 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002740 }
Paul Bakker68884e32013-01-07 18:20:04 +01002741
Hanno Beckerd96a6522019-07-10 13:55:25 +01002742#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002743 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2744 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002745 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002746
Hanno Beckerd96a6522019-07-10 13:55:25 +01002747 /* Fixed */
2748 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2749 /* Explicit */
2750 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002751 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002752 else
2753#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2754#if defined(MBEDTLS_CHACHAPOLY_C)
2755 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002756 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002757 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002758 unsigned char i;
2759
2760 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2761
2762 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002763 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002764 }
2765 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002766#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002767 {
2768 /* Reminder if we ever add an AEAD mode with a different size */
2769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2771 }
2772
Hanno Beckerd96a6522019-07-10 13:55:25 +01002773 /* Group changes to data, data_len, and add_data, because
2774 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002775 data += explicit_iv_len;
2776 rec->data_offset += explicit_iv_len;
2777 rec->data_len -= explicit_iv_len + transform->taglen;
2778
Hanno Beckercab87e62019-04-29 13:52:53 +01002779 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002780 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002781 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002782
Hanno Beckerd96a6522019-07-10 13:55:25 +01002783 /* Because of the check above, we know that there are
2784 * explicit_iv_len Bytes preceeding data, and taglen
2785 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002786 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002787 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002788
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002789 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002790 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002791 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002792
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002793 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002794 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002795 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002796 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2797 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002798 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002799 data, rec->data_len,
2800 data, &olen,
2801 data + rec->data_len,
2802 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2807 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002808
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002809 return( ret );
2810 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002811 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002812
Hanno Beckerd96a6522019-07-10 13:55:25 +01002813 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002814 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2817 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002818 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002819 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2822#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002823 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002826 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002827
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 /*
Paul Bakker45829992013-01-03 14:52:21 +01002829 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002832 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2833 {
2834 /* The ciphertext is prefixed with the CBC IV. */
2835 minlen += transform->ivlen;
2836 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002837#endif
Paul Bakker45829992013-01-03 14:52:21 +01002838
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002839 /* Size considerations:
2840 *
2841 * - The CBC cipher text must not be empty and hence
2842 * at least of size transform->ivlen.
2843 *
2844 * Together with the potential IV-prefix, this explains
2845 * the first of the two checks below.
2846 *
2847 * - The record must contain a MAC, either in plain or
2848 * encrypted, depending on whether Encrypt-then-MAC
2849 * is used or not.
2850 * - If it is, the message contains the IV-prefix,
2851 * the CBC ciphertext, and the MAC.
2852 * - If it is not, the padded plaintext, and hence
2853 * the CBC ciphertext, has at least length maclen + 1
2854 * because there is at least the padding length byte.
2855 *
2856 * As the CBC ciphertext is not empty, both cases give the
2857 * lower bound minlen + maclen + 1 on the record size, which
2858 * we test for in the second check below.
2859 */
2860 if( rec->data_len < minlen + transform->ivlen ||
2861 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002864 "+ 1 ) ( + expl IV )", rec->data_len,
2865 transform->ivlen,
2866 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002868 }
2869
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002870 /*
2871 * Authenticate before decrypt if enabled
2872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002874 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002875 {
Hanno Becker992b6872017-11-09 18:57:39 +00002876 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002879
Hanno Beckerd96a6522019-07-10 13:55:25 +01002880 /* Update data_len in tandem with add_data.
2881 *
2882 * The subtraction is safe because of the previous check
2883 * data_len >= minlen + maclen + 1.
2884 *
2885 * Afterwards, we know that data + data_len is followed by at
2886 * least maclen Bytes, which justifies the call to
2887 * mbedtls_ssl_safer_memcmp() below.
2888 *
2889 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002891 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002892
Hanno Beckerd96a6522019-07-10 13:55:25 +01002893 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002894 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2895 add_data_len );
2896 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2897 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002898 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2899 data, rec->data_len );
2900 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2901 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002902
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002903 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2904 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002905 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002906 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002907
Hanno Beckerd96a6522019-07-10 13:55:25 +01002908 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002909 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2910 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002914 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002915 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002916 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002918
2919 /*
2920 * Check length sanity
2921 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002922
2923 /* We know from above that data_len > minlen >= 0,
2924 * so the following check in particular implies that
2925 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002926 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002929 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002931 }
2932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002934 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002935 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002936 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002937 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002938 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002939 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002940 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002941
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002942 data += transform->ivlen;
2943 rec->data_offset += transform->ivlen;
2944 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002945 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002947
Hanno Beckerd96a6522019-07-10 13:55:25 +01002948 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2949
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002950 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2951 transform->iv_dec, transform->ivlen,
2952 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002955 return( ret );
2956 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002957
Hanno Beckerd96a6522019-07-10 13:55:25 +01002958 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002959 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2962 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002963 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002966 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002967 {
2968 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002969 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
2970 * records is equivalent to CBC decryption of the concatenation
2971 * of the records; in other words, IVs are maintained across
2972 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02002973 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002974 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2975 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002976 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002977#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002979 /* Safe since data_len >= minlen + maclen + 1, so after having
2980 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01002981 * data_len > 0 (because of data_len % ivlen == 0, it's actually
2982 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002983 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002984
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002985 if( auth_done == 1 )
2986 {
2987 correct *= ( rec->data_len >= padlen + 1 );
2988 padlen *= ( rec->data_len >= padlen + 1 );
2989 }
2990 else
Paul Bakker45829992013-01-03 14:52:21 +01002991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002993 if( rec->data_len < transform->maclen + padlen + 1 )
2994 {
2995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2996 rec->data_len,
2997 transform->maclen,
2998 padlen + 1 ) );
2999 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003000#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003001
3002 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3003 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003004 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003005
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003006 padlen++;
3007
3008 /* Regardless of the validity of the padding,
3009 * we have data_len >= padlen here. */
3010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003012 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003014 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016#if defined(MBEDTLS_SSL_DEBUG_ALL)
3017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003018 "should be no more than %d",
3019 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003020#endif
Paul Bakker45829992013-01-03 14:52:21 +01003021 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003022 }
3023 }
3024 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3026#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3027 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003028 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003029 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003030 /* The padding check involves a series of up to 256
3031 * consecutive memory reads at the end of the record
3032 * plaintext buffer. In order to hide the length and
3033 * validity of the padding, always perform exactly
3034 * `min(256,plaintext_len)` reads (but take into account
3035 * only the last `padlen` bytes for the padding check). */
3036 size_t pad_count = 0;
3037 size_t real_count = 0;
3038 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003039
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003040 /* Index of first padding byte; it has been ensured above
3041 * that the subtraction is safe. */
3042 size_t const padding_idx = rec->data_len - padlen;
3043 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3044 size_t const start_idx = rec->data_len - num_checks;
3045 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003046
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003047 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003048 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003049 real_count |= ( idx >= padding_idx );
3050 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003051 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003052 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003055 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003057#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003058 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003059 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003060 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3062 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3065 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003066 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003067
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003068 /* If the padding was found to be invalid, padlen == 0
3069 * and the subtraction is safe. If the padding was found valid,
3070 * padlen hasn't been changed and the previous assertion
3071 * data_len >= padlen still holds. */
3072 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003073 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003074 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003076 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003080 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003081
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003082#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003084 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003085#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
3087 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003088 * Authenticate if not done yet.
3089 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 */
Hanno Becker52344c22018-01-03 15:24:20 +00003091#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003092 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003093 {
Hanno Becker992b6872017-11-09 18:57:39 +00003094 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003095
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003096 /* If the initial value of padlen was such that
3097 * data_len < maclen + padlen + 1, then padlen
3098 * got reset to 1, and the initial check
3099 * data_len >= minlen + maclen + 1
3100 * guarantees that at this point we still
3101 * have at least data_len >= maclen.
3102 *
3103 * If the initial value of padlen was such that
3104 * data_len >= maclen + padlen + 1, then we have
3105 * subtracted either padlen + 1 (if the padding was correct)
3106 * or 0 (if the padding was incorrect) since then,
3107 * hence data_len >= maclen in any case.
3108 */
3109 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003110 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003113 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003114 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003115 ssl_mac( &transform->md_ctx_dec,
3116 transform->mac_dec,
3117 data, rec->data_len,
3118 rec->ctr, rec->type,
3119 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003120 }
3121 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3123#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3124 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003125 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003126 {
3127 /*
3128 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003129 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003130 *
3131 * Known timing attacks:
3132 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3133 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003134 * To compensate for different timings for the MAC calculation
3135 * depending on how much padding was removed (which is determined
3136 * by padlen), process extra_run more blocks through the hash
3137 * function.
3138 *
3139 * The formula in the paper is
3140 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3141 * where L1 is the size of the header plus the decrypted message
3142 * plus CBC padding and L2 is the size of the header plus the
3143 * decrypted message. This is for an underlying hash function
3144 * with 64-byte blocks.
3145 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3146 * correctly. We round down instead of up, so -56 is the correct
3147 * value for our calculations instead of -55.
3148 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003149 * Repeat the formula rather than defining a block_size variable.
3150 * This avoids requiring division by a variable at runtime
3151 * (which would be marginally less efficient and would require
3152 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003153 */
3154 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003155 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003156
3157 /*
3158 * The next two sizes are the minimum and maximum values of
3159 * in_msglen over all padlen values.
3160 *
3161 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003162 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003163 *
3164 * Note that max_len + maclen is never more than the buffer
3165 * length, as we previously did in_msglen -= maclen too.
3166 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003167 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003168 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3169
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003170 memset( tmp, 0, sizeof( tmp ) );
3171
3172 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003173 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003174#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3175 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003176 case MBEDTLS_MD_MD5:
3177 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003178 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003179 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003180 extra_run =
3181 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3182 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003183 break;
3184#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003185#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003186 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003187 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003188 extra_run =
3189 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3190 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003191 break;
3192#endif
3193 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3196 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003197
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003198 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003199
Hanno Beckercab87e62019-04-29 13:52:53 +01003200 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3201 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003202 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3203 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003204 /* Make sure we access everything even when padlen > 0. This
3205 * makes the synchronisation requirements for just-in-time
3206 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003207 ssl_read_memory( data + rec->data_len, padlen );
3208 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003209
3210 /* Call mbedtls_md_process at least once due to cache attacks
3211 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003212 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003213 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003214
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003215 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003216
3217 /* Make sure we access all the memory that could contain the MAC,
3218 * before we check it in the next code block. This makes the
3219 * synchronisation requirements for just-in-time Prime+Probe
3220 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003221 ssl_read_memory( data + min_len,
3222 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003223 }
3224 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3226 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3229 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003230 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003231
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003232#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003233 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3234 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003235#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003236
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003237 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3238 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240#if defined(MBEDTLS_SSL_DEBUG_ALL)
3241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003242#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003243 correct = 0;
3244 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003245 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003246 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003247
3248 /*
3249 * Finally check the correct flag
3250 */
3251 if( correct == 0 )
3252 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003253#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003254
3255 /* Make extra sure authentication was performed, exactly once */
3256 if( auth_done != 1 )
3257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3259 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003260 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003261
Hanno Beckera0e20d02019-05-15 14:03:01 +01003262#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003263 if( rec->cid_len != 0 )
3264 {
3265 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3266 &rec->type );
3267 if( ret != 0 )
3268 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3269 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003270#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
3274 return( 0 );
3275}
3276
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003277#undef MAC_NONE
3278#undef MAC_PLAINTEXT
3279#undef MAC_CIPHERTEXT
3280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003282/*
3283 * Compression/decompression functions
3284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003286{
3287 int ret;
3288 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003289 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003290 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003291 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003294
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003295 if( len_pre == 0 )
3296 return( 0 );
3297
Paul Bakker2770fbd2012-07-03 13:30:23 +00003298 memcpy( msg_pre, ssl->out_msg, len_pre );
3299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003301 ssl->out_msglen ) );
3302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003304 ssl->out_msg, ssl->out_msglen );
3305
Paul Bakker48916f92012-09-16 19:57:18 +00003306 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3307 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3308 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003309 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003310
Paul Bakker48916f92012-09-16 19:57:18 +00003311 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003312 if( ret != Z_OK )
3313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3315 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003316 }
3317
Angus Grattond8213d02016-05-25 20:56:48 +10003318 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003319 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003322 ssl->out_msglen ) );
3323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003325 ssl->out_msg, ssl->out_msglen );
3326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003328
3329 return( 0 );
3330}
3331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003333{
3334 int ret;
3335 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003336 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003337 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003338 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003341
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003342 if( len_pre == 0 )
3343 return( 0 );
3344
Paul Bakker2770fbd2012-07-03 13:30:23 +00003345 memcpy( msg_pre, ssl->in_msg, len_pre );
3346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003348 ssl->in_msglen ) );
3349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003351 ssl->in_msg, ssl->in_msglen );
3352
Paul Bakker48916f92012-09-16 19:57:18 +00003353 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3354 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3355 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003356 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003357 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003358
Paul Bakker48916f92012-09-16 19:57:18 +00003359 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003360 if( ret != Z_OK )
3361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3363 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003364 }
3365
Angus Grattond8213d02016-05-25 20:56:48 +10003366 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003367 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003370 ssl->in_msglen ) );
3371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003373 ssl->in_msg, ssl->in_msglen );
3374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003376
3377 return( 0 );
3378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3382static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384#if defined(MBEDTLS_SSL_PROTO_DTLS)
3385static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003386{
3387 /* If renegotiation is not enforced, retransmit until we would reach max
3388 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003389 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003390 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003391 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003392 unsigned char doublings = 1;
3393
3394 while( ratio != 0 )
3395 {
3396 ++doublings;
3397 ratio >>= 1;
3398 }
3399
3400 if( ++ssl->renego_records_seen > doublings )
3401 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003403 return( 0 );
3404 }
3405 }
3406
3407 return( ssl_write_hello_request( ssl ) );
3408}
3409#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003411
Paul Bakker5121ce52009-01-03 21:22:43 +00003412/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003413 * Fill the input message buffer by appending data to it.
3414 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003415 *
3416 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3417 * available (from this read and/or a previous one). Otherwise, an error code
3418 * is returned (possibly EOF or WANT_READ).
3419 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003420 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3421 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3422 * since we always read a whole datagram at once.
3423 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003424 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003425 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003428{
Paul Bakker23986e52011-04-24 08:57:21 +00003429 int ret;
3430 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003433
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003434 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003437 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003439 }
3440
Angus Grattond8213d02016-05-25 20:56:48 +10003441 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3444 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003445 }
3446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003448 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003450 uint32_t timeout;
3451
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003452 /* Just to be sure */
3453 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3454 {
3455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3456 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3458 }
3459
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003460 /*
3461 * The point is, we need to always read a full datagram at once, so we
3462 * sometimes read more then requested, and handle the additional data.
3463 * It could be the rest of the current record (while fetching the
3464 * header) and/or some other records in the same datagram.
3465 */
3466
3467 /*
3468 * Move to the next record in the already read datagram if applicable
3469 */
3470 if( ssl->next_record_offset != 0 )
3471 {
3472 if( ssl->in_left < ssl->next_record_offset )
3473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003476 }
3477
3478 ssl->in_left -= ssl->next_record_offset;
3479
3480 if( ssl->in_left != 0 )
3481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003483 ssl->next_record_offset ) );
3484 memmove( ssl->in_hdr,
3485 ssl->in_hdr + ssl->next_record_offset,
3486 ssl->in_left );
3487 }
3488
3489 ssl->next_record_offset = 0;
3490 }
3491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003493 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003494
3495 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003496 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003497 */
3498 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003501 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003502 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003503
3504 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003505 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003506 * are not at the beginning of a new record, the caller did something
3507 * wrong.
3508 */
3509 if( ssl->in_left != 0 )
3510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3512 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003513 }
3514
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003515 /*
3516 * Don't even try to read if time's out already.
3517 * This avoids by-passing the timer when repeatedly receiving messages
3518 * that will end up being dropped.
3519 */
3520 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003521 {
3522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003523 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003524 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003525 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003526 {
Angus Grattond8213d02016-05-25 20:56:48 +10003527 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003530 timeout = ssl->handshake->retransmit_timeout;
3531 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003532 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003535
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003536 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003537 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3538 timeout );
3539 else
3540 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003543
3544 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003546 }
3547
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003548 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003551 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003554 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003555 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003558 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003559 }
3560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003564 return( ret );
3565 }
3566
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003567 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003568 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003570 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003572 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003573 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003576 return( ret );
3577 }
3578
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003579 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003580 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003582 }
3583
Paul Bakker5121ce52009-01-03 21:22:43 +00003584 if( ret < 0 )
3585 return( ret );
3586
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003587 ssl->in_left = ret;
3588 }
3589 else
3590#endif
3591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003593 ssl->in_left, nb_want ) );
3594
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003595 while( ssl->in_left < nb_want )
3596 {
3597 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003598
3599 if( ssl_check_timer( ssl ) != 0 )
3600 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3601 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003602 {
3603 if( ssl->f_recv_timeout != NULL )
3604 {
3605 ret = ssl->f_recv_timeout( ssl->p_bio,
3606 ssl->in_hdr + ssl->in_left, len,
3607 ssl->conf->read_timeout );
3608 }
3609 else
3610 {
3611 ret = ssl->f_recv( ssl->p_bio,
3612 ssl->in_hdr + ssl->in_left, len );
3613 }
3614 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003617 ssl->in_left, nb_want ) );
3618 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003619
3620 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003622
3623 if( ret < 0 )
3624 return( ret );
3625
mohammad160352aecb92018-03-28 23:41:40 -07003626 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003627 {
Darryl Green11999bb2018-03-13 15:22:58 +00003628 MBEDTLS_SSL_DEBUG_MSG( 1,
3629 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003630 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003631 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3632 }
3633
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003634 ssl->in_left += ret;
3635 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003636 }
3637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003639
3640 return( 0 );
3641}
3642
3643/*
3644 * Flush any data not yet written
3645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003647{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003648 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003649 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003652
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003653 if( ssl->f_send == NULL )
3654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003656 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003658 }
3659
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003660 /* Avoid incrementing counter if data is flushed */
3661 if( ssl->out_left == 0 )
3662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003664 return( 0 );
3665 }
3666
Paul Bakker5121ce52009-01-03 21:22:43 +00003667 while( ssl->out_left > 0 )
3668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003670 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003671
Hanno Becker2b1e3542018-08-06 11:19:13 +01003672 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003673 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003676
3677 if( ret <= 0 )
3678 return( ret );
3679
mohammad160352aecb92018-03-28 23:41:40 -07003680 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003681 {
Darryl Green11999bb2018-03-13 15:22:58 +00003682 MBEDTLS_SSL_DEBUG_MSG( 1,
3683 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003684 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3686 }
3687
Paul Bakker5121ce52009-01-03 21:22:43 +00003688 ssl->out_left -= ret;
3689 }
3690
Hanno Becker2b1e3542018-08-06 11:19:13 +01003691#if defined(MBEDTLS_SSL_PROTO_DTLS)
3692 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003693 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003694 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003695 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003696 else
3697#endif
3698 {
3699 ssl->out_hdr = ssl->out_buf + 8;
3700 }
3701 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003704
3705 return( 0 );
3706}
3707
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003708/*
3709 * Functions to handle the DTLS retransmission state machine
3710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003712/*
3713 * Append current handshake message to current outgoing flight
3714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3719 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3720 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003721
3722 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003723 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003724 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003727 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003728 }
3729
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003730 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003731 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003734 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003735 }
3736
3737 /* Copy current handshake message with headers */
3738 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3739 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003740 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003741 msg->next = NULL;
3742
3743 /* Append to the current flight */
3744 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003745 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003746 else
3747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003749 while( cur->next != NULL )
3750 cur = cur->next;
3751 cur->next = msg;
3752 }
3753
Hanno Becker3b235902018-08-06 09:54:53 +01003754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003755 return( 0 );
3756}
3757
3758/*
3759 * Free the current flight of handshake messages
3760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003762{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 mbedtls_ssl_flight_item *cur = flight;
3764 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003765
3766 while( cur != NULL )
3767 {
3768 next = cur->next;
3769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770 mbedtls_free( cur->p );
3771 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003772
3773 cur = next;
3774 }
3775}
3776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3778static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003779#endif
3780
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003781/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003782 * Swap transform_out and out_ctr with the alternative ones
3783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003785{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003787 unsigned char tmp_out_ctr[8];
3788
3789 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003792 return;
3793 }
3794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003796
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003797 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003798 tmp_transform = ssl->transform_out;
3799 ssl->transform_out = ssl->handshake->alt_transform_out;
3800 ssl->handshake->alt_transform_out = tmp_transform;
3801
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003802 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003803 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3804 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003805 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003806
3807 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003808 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3811 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3816 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003817 }
3818 }
3819#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003820}
3821
3822/*
3823 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003824 */
3825int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3826{
3827 int ret = 0;
3828
3829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3830
3831 ret = mbedtls_ssl_flight_transmit( ssl );
3832
3833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3834
3835 return( ret );
3836}
3837
3838/*
3839 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003840 *
3841 * Need to remember the current message in case flush_output returns
3842 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003843 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003844 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003845int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003846{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003847 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003851 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003853
3854 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003855 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003856 ssl_swap_epochs( ssl );
3857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003859 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003860
3861 while( ssl->handshake->cur_msg != NULL )
3862 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003863 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003864 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003865
Hanno Beckere1dcb032018-08-17 16:47:58 +01003866 int const is_finished =
3867 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3868 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3869
Hanno Becker04da1892018-08-14 13:22:10 +01003870 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3871 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3872
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003873 /* Swap epochs before sending Finished: we can't do it after
3874 * sending ChangeCipherSpec, in case write returns WANT_READ.
3875 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003876 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003877 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003879 ssl_swap_epochs( ssl );
3880 }
3881
Hanno Becker67bc7c32018-08-06 11:33:50 +01003882 ret = ssl_get_remaining_payload_in_datagram( ssl );
3883 if( ret < 0 )
3884 return( ret );
3885 max_frag_len = (size_t) ret;
3886
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003887 /* CCS is copied as is, while HS messages may need fragmentation */
3888 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3889 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003890 if( max_frag_len == 0 )
3891 {
3892 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3893 return( ret );
3894
3895 continue;
3896 }
3897
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003898 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003899 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003900 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003901
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003902 /* Update position inside current message */
3903 ssl->handshake->cur_msg_p += cur->len;
3904 }
3905 else
3906 {
3907 const unsigned char * const p = ssl->handshake->cur_msg_p;
3908 const size_t hs_len = cur->len - 12;
3909 const size_t frag_off = p - ( cur->p + 12 );
3910 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003911 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003912
Hanno Beckere1dcb032018-08-17 16:47:58 +01003913 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003914 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003915 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003916 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003917
Hanno Becker67bc7c32018-08-06 11:33:50 +01003918 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3919 return( ret );
3920
3921 continue;
3922 }
3923 max_hs_frag_len = max_frag_len - 12;
3924
3925 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3926 max_hs_frag_len : rem_len;
3927
3928 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003929 {
3930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003931 (unsigned) cur_hs_frag_len,
3932 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003933 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003934
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003935 /* Messages are stored with handshake headers as if not fragmented,
3936 * copy beginning of headers then fill fragmentation fields.
3937 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3938 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003939
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003940 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3941 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3942 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3943
Hanno Becker67bc7c32018-08-06 11:33:50 +01003944 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3945 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3946 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003947
3948 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3949
Hanno Becker3f7b9732018-08-28 09:53:25 +01003950 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003951 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3952 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003953 ssl->out_msgtype = cur->type;
3954
3955 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003956 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003957 }
3958
3959 /* If done with the current message move to the next one if any */
3960 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3961 {
3962 if( cur->next != NULL )
3963 {
3964 ssl->handshake->cur_msg = cur->next;
3965 ssl->handshake->cur_msg_p = cur->next->p + 12;
3966 }
3967 else
3968 {
3969 ssl->handshake->cur_msg = NULL;
3970 ssl->handshake->cur_msg_p = NULL;
3971 }
3972 }
3973
3974 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003975 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003978 return( ret );
3979 }
3980 }
3981
Hanno Becker67bc7c32018-08-06 11:33:50 +01003982 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3983 return( ret );
3984
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003985 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3987 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003988 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003991 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3992 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003993
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003995
3996 return( 0 );
3997}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003998
3999/*
4000 * To be called when the last message of an incoming flight is received.
4001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004003{
4004 /* We won't need to resend that one any more */
4005 ssl_flight_free( ssl->handshake->flight );
4006 ssl->handshake->flight = NULL;
4007 ssl->handshake->cur_msg = NULL;
4008
4009 /* The next incoming flight will start with this msg_seq */
4010 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4011
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004012 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004013 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004014
Hanno Becker0271f962018-08-16 13:23:47 +01004015 /* Clear future message buffering structure. */
4016 ssl_buffering_free( ssl );
4017
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004018 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004019 ssl_set_timer( ssl, 0 );
4020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4022 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004024 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004025 }
4026 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004028}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004029
4030/*
4031 * To be called when the last message of an outgoing flight is send.
4032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004034{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004035 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004036 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4039 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004041 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004042 }
4043 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004047
Paul Bakker5121ce52009-01-03 21:22:43 +00004048/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004049 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004050 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004051
4052/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004053 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004054 *
4055 * - fill in handshake headers
4056 * - update handshake checksum
4057 * - DTLS: save message for resending
4058 * - then pass to the record layer
4059 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004060 * DTLS: except for HelloRequest, messages are only queued, and will only be
4061 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004062 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004063 * Inputs:
4064 * - ssl->out_msglen: 4 + actual handshake message len
4065 * (4 is the size of handshake headers for TLS)
4066 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4067 * - ssl->out_msg + 4: the handshake message body
4068 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004069 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004070 * - ssl->out_msglen: the length of the record contents
4071 * (including handshake headers but excluding record headers)
4072 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004073 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004074int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004075{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004076 int ret;
4077 const size_t hs_len = ssl->out_msglen - 4;
4078 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004079
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4081
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004082 /*
4083 * Sanity checks
4084 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004085 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004086 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4087 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004088 /* In SSLv3, the client might send a NoCertificate alert. */
4089#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4090 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4091 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4092 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4093#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4094 {
4095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4096 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4097 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004098 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004099
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004100 /* Whenever we send anything different from a
4101 * HelloRequest we should be in a handshake - double check. */
4102 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4103 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004104 ssl->handshake == NULL )
4105 {
4106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4107 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4108 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004111 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004112 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004113 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004114 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4116 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004117 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004118#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004119
Hanno Beckerb50a2532018-08-06 11:52:54 +01004120 /* Double-check that we did not exceed the bounds
4121 * of the outgoing record buffer.
4122 * This should never fail as the various message
4123 * writing functions must obey the bounds of the
4124 * outgoing record buffer, but better be safe.
4125 *
4126 * Note: We deliberately do not check for the MTU or MFL here.
4127 */
4128 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4129 {
4130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4131 "size %u, maximum %u",
4132 (unsigned) ssl->out_msglen,
4133 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4134 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4135 }
4136
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004137 /*
4138 * Fill handshake headers
4139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004141 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004142 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4143 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4144 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004145
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004146 /*
4147 * DTLS has additional fields in the Handshake layer,
4148 * between the length field and the actual payload:
4149 * uint16 message_seq;
4150 * uint24 fragment_offset;
4151 * uint24 fragment_length;
4152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004154 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004155 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004156 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004157 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004158 {
4159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4160 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004161 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004162 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4164 }
4165
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004166 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004167 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004168
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004169 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004170 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004171 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004172 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4173 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4174 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004175 }
4176 else
4177 {
4178 ssl->out_msg[4] = 0;
4179 ssl->out_msg[5] = 0;
4180 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004181
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004182 /* Handshake hashes are computed without fragmentation,
4183 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004184 memset( ssl->out_msg + 6, 0x00, 3 );
4185 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004186 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004188
Hanno Becker0207e532018-08-28 10:28:28 +01004189 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004190 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4191 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004192 }
4193
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004194 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004196 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004197 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4198 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004199 {
4200 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004203 return( ret );
4204 }
4205 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004206 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004207#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004208 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004209 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004210 {
4211 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4212 return( ret );
4213 }
4214 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004215
4216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4217
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004218 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004219}
4220
4221/*
4222 * Record layer functions
4223 */
4224
4225/*
4226 * Write current record.
4227 *
4228 * Uses:
4229 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4230 * - ssl->out_msglen: length of the record content (excl headers)
4231 * - ssl->out_msg: record content
4232 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004233int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004234{
4235 int ret, done = 0;
4236 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004237 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004238
4239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004242 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004244 {
4245 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004248 return( ret );
4249 }
4250
4251 len = ssl->out_msglen;
4252 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4256 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260 ret = mbedtls_ssl_hw_record_write( ssl );
4261 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4264 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004265 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004266
4267 if( ret == 0 )
4268 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004269 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004271 if( !done )
4272 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004273 unsigned i;
4274 size_t protected_record_size;
4275
Hanno Becker6430faf2019-05-08 11:57:13 +01004276 /* Skip writing the record content type to after the encryption,
4277 * as it may change when using the CID extension. */
4278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004280 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004281
Hanno Becker19859472018-08-06 09:40:20 +01004282 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004283 ssl->out_len[0] = (unsigned char)( len >> 8 );
4284 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004285
Paul Bakker48916f92012-09-16 19:57:18 +00004286 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004287 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004288 mbedtls_record rec;
4289
4290 rec.buf = ssl->out_iv;
4291 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4292 ( ssl->out_iv - ssl->out_buf );
4293 rec.data_len = ssl->out_msglen;
4294 rec.data_offset = ssl->out_msg - rec.buf;
4295
4296 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4297 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4298 ssl->conf->transport, rec.ver );
4299 rec.type = ssl->out_msgtype;
4300
Hanno Beckera0e20d02019-05-15 14:03:01 +01004301#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004302 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004303 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004304#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004305
Hanno Beckera18d1322018-01-03 14:27:32 +00004306 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004307 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004310 return( ret );
4311 }
4312
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004313 if( rec.data_offset != 0 )
4314 {
4315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4316 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4317 }
4318
Hanno Becker6430faf2019-05-08 11:57:13 +01004319 /* Update the record content type and CID. */
4320 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004321#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004322 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004323#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004324 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004325 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4326 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004327 }
4328
Hanno Becker5903de42019-05-03 14:46:38 +01004329 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004330
4331#if defined(MBEDTLS_SSL_PROTO_DTLS)
4332 /* In case of DTLS, double-check that we don't exceed
4333 * the remaining space in the datagram. */
4334 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4335 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004336 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004337 if( ret < 0 )
4338 return( ret );
4339
4340 if( protected_record_size > (size_t) ret )
4341 {
4342 /* Should never happen */
4343 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4344 }
4345 }
4346#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004347
Hanno Becker6430faf2019-05-08 11:57:13 +01004348 /* Now write the potentially updated record content type. */
4349 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004352 "version = [%d:%d], msglen = %d",
4353 ssl->out_hdr[0], ssl->out_hdr[1],
4354 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004357 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004358
4359 ssl->out_left += protected_record_size;
4360 ssl->out_hdr += protected_record_size;
4361 ssl_update_out_pointers( ssl, ssl->transform_out );
4362
Hanno Becker04484622018-08-06 09:49:38 +01004363 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4364 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4365 break;
4366
4367 /* The loop goes to its end iff the counter is wrapping */
4368 if( i == ssl_ep_len( ssl ) )
4369 {
4370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4371 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4372 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 }
4374
Hanno Becker67bc7c32018-08-06 11:33:50 +01004375#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004376 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4377 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004378 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004379 size_t remaining;
4380 ret = ssl_get_remaining_payload_in_datagram( ssl );
4381 if( ret < 0 )
4382 {
4383 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4384 ret );
4385 return( ret );
4386 }
4387
4388 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004389 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004390 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004391 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004392 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004393 else
4394 {
Hanno Becker513815a2018-08-20 11:56:09 +01004395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004396 }
4397 }
4398#endif /* MBEDTLS_SSL_PROTO_DTLS */
4399
4400 if( ( flush == SSL_FORCE_FLUSH ) &&
4401 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004404 return( ret );
4405 }
4406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004408
4409 return( 0 );
4410}
4411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004413
4414static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4415{
4416 if( ssl->in_msglen < ssl->in_hslen ||
4417 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4418 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4419 {
4420 return( 1 );
4421 }
4422 return( 0 );
4423}
Hanno Becker44650b72018-08-16 12:51:11 +01004424
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004425static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004426{
4427 return( ( ssl->in_msg[9] << 16 ) |
4428 ( ssl->in_msg[10] << 8 ) |
4429 ssl->in_msg[11] );
4430}
4431
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004432static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004433{
4434 return( ( ssl->in_msg[6] << 16 ) |
4435 ( ssl->in_msg[7] << 8 ) |
4436 ssl->in_msg[8] );
4437}
4438
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004439static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004440{
4441 uint32_t msg_len, frag_off, frag_len;
4442
4443 msg_len = ssl_get_hs_total_len( ssl );
4444 frag_off = ssl_get_hs_frag_off( ssl );
4445 frag_len = ssl_get_hs_frag_len( ssl );
4446
4447 if( frag_off > msg_len )
4448 return( -1 );
4449
4450 if( frag_len > msg_len - frag_off )
4451 return( -1 );
4452
4453 if( frag_len + 12 > ssl->in_msglen )
4454 return( -1 );
4455
4456 return( 0 );
4457}
4458
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004459/*
4460 * Mark bits in bitmask (used for DTLS HS reassembly)
4461 */
4462static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4463{
4464 unsigned int start_bits, end_bits;
4465
4466 start_bits = 8 - ( offset % 8 );
4467 if( start_bits != 8 )
4468 {
4469 size_t first_byte_idx = offset / 8;
4470
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004471 /* Special case */
4472 if( len <= start_bits )
4473 {
4474 for( ; len != 0; len-- )
4475 mask[first_byte_idx] |= 1 << ( start_bits - len );
4476
4477 /* Avoid potential issues with offset or len becoming invalid */
4478 return;
4479 }
4480
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004481 offset += start_bits; /* Now offset % 8 == 0 */
4482 len -= start_bits;
4483
4484 for( ; start_bits != 0; start_bits-- )
4485 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4486 }
4487
4488 end_bits = len % 8;
4489 if( end_bits != 0 )
4490 {
4491 size_t last_byte_idx = ( offset + len ) / 8;
4492
4493 len -= end_bits; /* Now len % 8 == 0 */
4494
4495 for( ; end_bits != 0; end_bits-- )
4496 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4497 }
4498
4499 memset( mask + offset / 8, 0xFF, len / 8 );
4500}
4501
4502/*
4503 * Check that bitmask is full
4504 */
4505static int ssl_bitmask_check( unsigned char *mask, size_t len )
4506{
4507 size_t i;
4508
4509 for( i = 0; i < len / 8; i++ )
4510 if( mask[i] != 0xFF )
4511 return( -1 );
4512
4513 for( i = 0; i < len % 8; i++ )
4514 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4515 return( -1 );
4516
4517 return( 0 );
4518}
4519
Hanno Becker56e205e2018-08-16 09:06:12 +01004520/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004521static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004522 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004523{
Hanno Becker56e205e2018-08-16 09:06:12 +01004524 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004525
Hanno Becker56e205e2018-08-16 09:06:12 +01004526 alloc_len = 12; /* Handshake header */
4527 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004528
Hanno Beckerd07df862018-08-16 09:14:58 +01004529 if( add_bitmap )
4530 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004531
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004532 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004533}
Hanno Becker56e205e2018-08-16 09:06:12 +01004534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004536
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004537static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004538{
4539 return( ( ssl->in_msg[1] << 16 ) |
4540 ( ssl->in_msg[2] << 8 ) |
4541 ssl->in_msg[3] );
4542}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004543
Simon Butcher99000142016-10-13 17:21:01 +01004544int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004549 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004551 }
4552
Hanno Becker12555c62018-08-16 12:47:53 +01004553 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004556 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004557 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004560 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004561 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004562 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004563 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004564
Hanno Becker44650b72018-08-16 12:51:11 +01004565 if( ssl_check_hs_header( ssl ) != 0 )
4566 {
4567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4568 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4569 }
4570
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004571 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004572 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4573 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4574 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4575 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004576 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004577 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4578 {
4579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4580 recv_msg_seq,
4581 ssl->handshake->in_msg_seq ) );
4582 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4583 }
4584
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004585 /* Retransmit only on last message from previous flight, to avoid
4586 * too many retransmissions.
4587 * Besides, No sane server ever retransmits HelloVerifyRequest */
4588 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004592 "message_seq = %d, start_of_flight = %d",
4593 recv_msg_seq,
4594 ssl->handshake->in_flight_start_seq ) );
4595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004599 return( ret );
4600 }
4601 }
4602 else
4603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004605 "message_seq = %d, expected = %d",
4606 recv_msg_seq,
4607 ssl->handshake->in_msg_seq ) );
4608 }
4609
Hanno Becker90333da2017-10-10 11:27:13 +01004610 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004611 }
4612 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004613
Hanno Becker6d97ef52018-08-16 13:09:04 +01004614 /* Message reassembly is handled alongside buffering of future
4615 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004616 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004617 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004618 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004621 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004622 }
4623 }
4624 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004625#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004626 /* With TLS we don't handle fragmentation (for now) */
4627 if( ssl->in_msglen < ssl->in_hslen )
4628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4630 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004631 }
4632
Simon Butcher99000142016-10-13 17:21:01 +01004633 return( 0 );
4634}
4635
4636void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4637{
Hanno Becker0271f962018-08-16 13:23:47 +01004638 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004639
Hanno Becker0271f962018-08-16 13:23:47 +01004640 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004641 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004642 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004643 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004644
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004645 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004647 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004648 ssl->handshake != NULL )
4649 {
Hanno Becker0271f962018-08-16 13:23:47 +01004650 unsigned offset;
4651 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004652
Hanno Becker0271f962018-08-16 13:23:47 +01004653 /* Increment handshake sequence number */
4654 hs->in_msg_seq++;
4655
4656 /*
4657 * Clear up handshake buffering and reassembly structure.
4658 */
4659
4660 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004661 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004662
4663 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004664 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4665 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004666 offset++, hs_buf++ )
4667 {
4668 *hs_buf = *(hs_buf + 1);
4669 }
4670
4671 /* Create a fresh last entry */
4672 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004673 }
4674#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004675}
4676
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004677/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004678 * DTLS anti-replay: RFC 6347 4.1.2.6
4679 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004680 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4681 * Bit n is set iff record number in_window_top - n has been seen.
4682 *
4683 * Usually, in_window_top is the last record number seen and the lsb of
4684 * in_window is set. The only exception is the initial state (record number 0
4685 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004687#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4688static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004689{
4690 ssl->in_window_top = 0;
4691 ssl->in_window = 0;
4692}
4693
4694static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4695{
4696 return( ( (uint64_t) buf[0] << 40 ) |
4697 ( (uint64_t) buf[1] << 32 ) |
4698 ( (uint64_t) buf[2] << 24 ) |
4699 ( (uint64_t) buf[3] << 16 ) |
4700 ( (uint64_t) buf[4] << 8 ) |
4701 ( (uint64_t) buf[5] ) );
4702}
4703
4704/*
4705 * Return 0 if sequence number is acceptable, -1 otherwise
4706 */
Hanno Becker0183d692019-07-12 08:50:37 +01004707int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004708{
4709 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4710 uint64_t bit;
4711
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004712 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004713 return( 0 );
4714
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004715 if( rec_seqnum > ssl->in_window_top )
4716 return( 0 );
4717
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004718 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004719
4720 if( bit >= 64 )
4721 return( -1 );
4722
4723 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4724 return( -1 );
4725
4726 return( 0 );
4727}
4728
4729/*
4730 * Update replay window on new validated record
4731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004732void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004733{
4734 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4735
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004736 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004737 return;
4738
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004739 if( rec_seqnum > ssl->in_window_top )
4740 {
4741 /* Update window_top and the contents of the window */
4742 uint64_t shift = rec_seqnum - ssl->in_window_top;
4743
4744 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004745 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004746 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004747 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004748 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004749 ssl->in_window |= 1;
4750 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004751
4752 ssl->in_window_top = rec_seqnum;
4753 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004754 else
4755 {
4756 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004757 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004758
4759 if( bit < 64 ) /* Always true, but be extra sure */
4760 ssl->in_window |= (uint64_t) 1 << bit;
4761 }
4762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004764
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004765#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004766/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004767static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4768
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004769/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004770 * Without any SSL context, check if a datagram looks like a ClientHello with
4771 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004772 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004773 *
4774 * - if cookie is valid, return 0
4775 * - if ClientHello looks superficially valid but cookie is not,
4776 * fill obuf and set olen, then
4777 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4778 * - otherwise return a specific error code
4779 */
4780static int ssl_check_dtls_clihlo_cookie(
4781 mbedtls_ssl_cookie_write_t *f_cookie_write,
4782 mbedtls_ssl_cookie_check_t *f_cookie_check,
4783 void *p_cookie,
4784 const unsigned char *cli_id, size_t cli_id_len,
4785 const unsigned char *in, size_t in_len,
4786 unsigned char *obuf, size_t buf_len, size_t *olen )
4787{
4788 size_t sid_len, cookie_len;
4789 unsigned char *p;
4790
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004791 /*
4792 * Structure of ClientHello with record and handshake headers,
4793 * and expected values. We don't need to check a lot, more checks will be
4794 * done when actually parsing the ClientHello - skipping those checks
4795 * avoids code duplication and does not make cookie forging any easier.
4796 *
4797 * 0-0 ContentType type; copied, must be handshake
4798 * 1-2 ProtocolVersion version; copied
4799 * 3-4 uint16 epoch; copied, must be 0
4800 * 5-10 uint48 sequence_number; copied
4801 * 11-12 uint16 length; (ignored)
4802 *
4803 * 13-13 HandshakeType msg_type; (ignored)
4804 * 14-16 uint24 length; (ignored)
4805 * 17-18 uint16 message_seq; copied
4806 * 19-21 uint24 fragment_offset; copied, must be 0
4807 * 22-24 uint24 fragment_length; (ignored)
4808 *
4809 * 25-26 ProtocolVersion client_version; (ignored)
4810 * 27-58 Random random; (ignored)
4811 * 59-xx SessionID session_id; 1 byte len + sid_len content
4812 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4813 * ...
4814 *
4815 * Minimum length is 61 bytes.
4816 */
4817 if( in_len < 61 ||
4818 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4819 in[3] != 0 || in[4] != 0 ||
4820 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4821 {
4822 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4823 }
4824
4825 sid_len = in[59];
4826 if( sid_len > in_len - 61 )
4827 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4828
4829 cookie_len = in[60 + sid_len];
4830 if( cookie_len > in_len - 60 )
4831 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4832
4833 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4834 cli_id, cli_id_len ) == 0 )
4835 {
4836 /* Valid cookie */
4837 return( 0 );
4838 }
4839
4840 /*
4841 * If we get here, we've got an invalid cookie, let's prepare HVR.
4842 *
4843 * 0-0 ContentType type; copied
4844 * 1-2 ProtocolVersion version; copied
4845 * 3-4 uint16 epoch; copied
4846 * 5-10 uint48 sequence_number; copied
4847 * 11-12 uint16 length; olen - 13
4848 *
4849 * 13-13 HandshakeType msg_type; hello_verify_request
4850 * 14-16 uint24 length; olen - 25
4851 * 17-18 uint16 message_seq; copied
4852 * 19-21 uint24 fragment_offset; copied
4853 * 22-24 uint24 fragment_length; olen - 25
4854 *
4855 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4856 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4857 *
4858 * Minimum length is 28.
4859 */
4860 if( buf_len < 28 )
4861 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4862
4863 /* Copy most fields and adapt others */
4864 memcpy( obuf, in, 25 );
4865 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4866 obuf[25] = 0xfe;
4867 obuf[26] = 0xff;
4868
4869 /* Generate and write actual cookie */
4870 p = obuf + 28;
4871 if( f_cookie_write( p_cookie,
4872 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4873 {
4874 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4875 }
4876
4877 *olen = p - obuf;
4878
4879 /* Go back and fill length fields */
4880 obuf[27] = (unsigned char)( *olen - 28 );
4881
4882 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4883 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4884 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4885
4886 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4887 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4888
4889 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4890}
4891
4892/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004893 * Handle possible client reconnect with the same UDP quadruplet
4894 * (RFC 6347 Section 4.2.8).
4895 *
4896 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4897 * that looks like a ClientHello.
4898 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004899 * - if the input looks like a ClientHello without cookies,
4900 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004901 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004902 * - if the input looks like a ClientHello with a valid cookie,
4903 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004904 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004905 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004906 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004907 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004908 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4909 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004910 */
4911static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4912{
4913 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004914 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004915
Hanno Becker2fddd372019-07-10 14:37:41 +01004916 if( ssl->conf->f_cookie_write == NULL ||
4917 ssl->conf->f_cookie_check == NULL )
4918 {
4919 /* If we can't use cookies to verify reachability of the peer,
4920 * drop the record. */
4921 return( 0 );
4922 }
4923
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004924 ret = ssl_check_dtls_clihlo_cookie(
4925 ssl->conf->f_cookie_write,
4926 ssl->conf->f_cookie_check,
4927 ssl->conf->p_cookie,
4928 ssl->cli_id, ssl->cli_id_len,
4929 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004930 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004931
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004932 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4933
4934 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004935 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004936 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004937 * If the error is permanent we'll catch it later,
4938 * if it's not, then hopefully it'll work next time. */
4939 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004940 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004941 }
4942
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004943 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004944 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004945 /* Got a valid cookie, partially reset context */
4946 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4947 {
4948 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4949 return( ret );
4950 }
4951
4952 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004953 }
4954
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004955 return( ret );
4956}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004957#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004958
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004959static int ssl_check_record_type( uint8_t record_type )
4960{
4961 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4962 record_type != MBEDTLS_SSL_MSG_ALERT &&
4963 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4964 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4965 {
4966 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4967 }
4968
4969 return( 0 );
4970}
4971
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004972/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004973 * ContentType type;
4974 * ProtocolVersion version;
4975 * uint16 epoch; // DTLS only
4976 * uint48 sequence_number; // DTLS only
4977 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004978 *
4979 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004980 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004981 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4982 *
4983 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004984 * 1. proceed with the record if this function returns 0
4985 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4986 * 3. return CLIENT_RECONNECT if this function return that value
4987 * 4. drop the whole datagram if this function returns anything else.
4988 * Point 2 is needed when the peer is resending, and we have already received
4989 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004990 */
Hanno Becker331de3d2019-07-12 11:10:16 +01004991static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01004992 unsigned char *buf,
4993 size_t len,
4994 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00004995{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004996 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004997
Hanno Beckere5e7e782019-07-11 12:29:35 +01004998 size_t const rec_hdr_type_offset = 0;
4999 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005000
Hanno Beckere5e7e782019-07-11 12:29:35 +01005001 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5002 rec_hdr_type_len;
5003 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005004
Hanno Beckere5e7e782019-07-11 12:29:35 +01005005 size_t const rec_hdr_ctr_len = 8;
5006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005007 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005008 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5009 rec_hdr_version_len;
5010
Hanno Beckera0e20d02019-05-15 14:03:01 +01005011#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005012 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5013 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005014 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005015#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5016#endif /* MBEDTLS_SSL_PROTO_DTLS */
5017
5018 size_t rec_hdr_len_offset; /* To be determined */
5019 size_t const rec_hdr_len_len = 2;
5020
5021 /*
5022 * Check minimum lengths for record header.
5023 */
5024
5025#if defined(MBEDTLS_SSL_PROTO_DTLS)
5026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5027 {
5028 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5029 }
5030 else
5031#endif /* MBEDTLS_SSL_PROTO_DTLS */
5032 {
5033 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5034 }
5035
5036 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5037 {
5038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5039 (unsigned) len,
5040 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5041 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5042 }
5043
5044 /*
5045 * Parse and validate record content type
5046 */
5047
5048 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005049
5050 /* Check record content type */
5051#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5052 rec->cid_len = 0;
5053
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005054 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005055 ssl->conf->cid_len != 0 &&
5056 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005057 {
5058 /* Shift pointers to account for record header including CID
5059 * struct {
5060 * ContentType special_type = tls12_cid;
5061 * ProtocolVersion version;
5062 * uint16 epoch;
5063 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005064 * opaque cid[cid_length]; // Additional field compared to
5065 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005066 * uint16 length;
5067 * opaque enc_content[DTLSCiphertext.length];
5068 * } DTLSCiphertext;
5069 */
5070
5071 /* So far, we only support static CID lengths
5072 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005073 rec_hdr_cid_len = ssl->conf->cid_len;
5074 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005075
Hanno Beckere5e7e782019-07-11 12:29:35 +01005076 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005077 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5079 (unsigned) len,
5080 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005081 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005082 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005083
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005084 /* configured CID len is guaranteed at most 255, see
5085 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5086 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005087 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005088 }
5089 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005090#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005091 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005092 if( ssl_check_record_type( rec->type ) )
5093 {
Hanno Becker54229812019-07-12 14:40:00 +01005094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5095 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005096 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5097 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005098 }
5099
Hanno Beckere5e7e782019-07-11 12:29:35 +01005100 /*
5101 * Parse and validate record version
5102 */
5103
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005104 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5105 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005106 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5107 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005108 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005109
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005110 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5113 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005114 }
5115
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005116 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5119 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005120 }
5121
Hanno Beckere5e7e782019-07-11 12:29:35 +01005122 /*
5123 * Parse/Copy record sequence number.
5124 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005125
Hanno Beckere5e7e782019-07-11 12:29:35 +01005126#if defined(MBEDTLS_SSL_PROTO_DTLS)
5127 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005128 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005129 /* Copy explicit record sequence number from input buffer. */
5130 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5131 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005132 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005133 else
5134#endif /* MBEDTLS_SSL_PROTO_DTLS */
5135 {
5136 /* Copy implicit record sequence number from SSL context structure. */
5137 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5138 }
Paul Bakker40e46942009-01-03 21:51:57 +00005139
Hanno Beckere5e7e782019-07-11 12:29:35 +01005140 /*
5141 * Parse record length.
5142 */
5143
Hanno Beckere5e7e782019-07-11 12:29:35 +01005144 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005145 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5146 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005147 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005148
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005150 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005151 rec->type,
5152 major_ver, minor_ver, rec->data_len ) );
5153
5154 rec->buf = buf;
5155 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005156
Hanno Beckerd417cc92019-07-26 08:20:27 +01005157 if( rec->data_len == 0 )
5158 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005159
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005160 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005161 * DTLS-related tests.
5162 * Check epoch before checking length constraint because
5163 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5164 * message gets duplicated before the corresponding Finished message,
5165 * the second ChangeCipherSpec should be discarded because it belongs
5166 * to an old epoch, but not because its length is shorter than
5167 * the minimum record length for packets using the new record transform.
5168 * Note that these two kinds of failures are handled differently,
5169 * as an unexpected record is silently skipped but an invalid
5170 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005171 */
5172#if defined(MBEDTLS_SSL_PROTO_DTLS)
5173 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5174 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005175 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005176
Hanno Becker955a5c92019-07-10 17:12:07 +01005177 /* Check that the datagram is large enough to contain a record
5178 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005179 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005180 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5182 (unsigned) len,
5183 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005184 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5185 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005186
Hanno Becker37cfe732019-07-10 17:20:01 +01005187 /* Records from other, non-matching epochs are silently discarded.
5188 * (The case of same-port Client reconnects must be considered in
5189 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005190 if( rec_epoch != ssl->in_epoch )
5191 {
5192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5193 "expected %d, received %d",
5194 ssl->in_epoch, rec_epoch ) );
5195
Hanno Becker552f7472019-07-19 10:59:12 +01005196 /* Records from the next epoch are considered for buffering
5197 * (concretely: early Finished messages). */
5198 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005199 {
Hanno Becker552f7472019-07-19 10:59:12 +01005200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5201 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005202 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005203
Hanno Becker2fddd372019-07-10 14:37:41 +01005204 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005205 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005206#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005207 /* For records from the correct epoch, check whether their
5208 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005209 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005210 {
5211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5212 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5213 }
5214#endif
5215 }
5216#endif /* MBEDTLS_SSL_PROTO_DTLS */
5217
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005218 return( 0 );
5219}
Paul Bakker5121ce52009-01-03 21:22:43 +00005220
Paul Bakker5121ce52009-01-03 21:22:43 +00005221
Hanno Becker2fddd372019-07-10 14:37:41 +01005222#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5223static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5224{
5225 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5226
5227 /*
5228 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5229 * access the first byte of record content (handshake type), as we
5230 * have an active transform (possibly iv_len != 0), so use the
5231 * fact that the record header len is 13 instead.
5232 */
5233 if( rec_epoch == 0 &&
5234 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5235 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5236 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5237 ssl->in_left > 13 &&
5238 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5239 {
5240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5241 "from the same port" ) );
5242 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005243 }
5244
5245 return( 0 );
5246}
Hanno Becker2fddd372019-07-10 14:37:41 +01005247#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005248
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005249/*
5250 * If applicable, decrypt (and decompress) record content
5251 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005252static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5253 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005254{
5255 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005257 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005258 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5261 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005265 ret = mbedtls_ssl_hw_record_read( ssl );
5266 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005268 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5269 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005270 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005271
5272 if( ret == 0 )
5273 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005274 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005276 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005277 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005278 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005279
Hanno Beckera18d1322018-01-03 14:27:32 +00005280 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005281 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005284
Hanno Beckera0e20d02019-05-15 14:03:01 +01005285#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005286 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5287 ssl->conf->ignore_unexpected_cid
5288 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5289 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005291 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005292 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005293#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005294
Paul Bakker5121ce52009-01-03 21:22:43 +00005295 return( ret );
5296 }
5297
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005298 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005299 {
5300 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005301 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005302 }
5303
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005304 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005305 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005306
Hanno Beckera0e20d02019-05-15 14:03:01 +01005307#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005308 /* We have already checked the record content type
5309 * in ssl_parse_record_header(), failing or silently
5310 * dropping the record in the case of an unknown type.
5311 *
5312 * Since with the use of CIDs, the record content type
5313 * might change during decryption, re-check the record
5314 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005315 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005316 {
5317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5318 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5319 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005320#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005321
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005322 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005323 {
5324#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5325 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005326 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005327 {
5328 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5330 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5331 }
5332#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5333
5334 ssl->nb_zero++;
5335
5336 /*
5337 * Three or more empty messages may be a DoS attack
5338 * (excessive CPU consumption).
5339 */
5340 if( ssl->nb_zero > 3 )
5341 {
5342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005343 "messages, possible DoS attack" ) );
5344 /* Treat the records as if they were not properly authenticated,
5345 * thereby failing the connection if we see more than allowed
5346 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005347 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5348 }
5349 }
5350 else
5351 ssl->nb_zero = 0;
5352
5353#if defined(MBEDTLS_SSL_PROTO_DTLS)
5354 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5355 {
5356 ; /* in_ctr read from peer, not maintained internally */
5357 }
5358 else
5359#endif
5360 {
5361 unsigned i;
5362 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5363 if( ++ssl->in_ctr[i - 1] != 0 )
5364 break;
5365
5366 /* The loop goes to its end iff the counter is wrapping */
5367 if( i == ssl_ep_len( ssl ) )
5368 {
5369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5370 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5371 }
5372 }
5373
Paul Bakker5121ce52009-01-03 21:22:43 +00005374 }
5375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005377 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005379 {
5380 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005383 return( ret );
5384 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005385 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005389 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005392 }
5393#endif
5394
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005395 /* Check actual (decrypted) record content length against
5396 * configured maximum. */
5397 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5398 {
5399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5400 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5401 }
5402
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005403 return( 0 );
5404}
5405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005407
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005408/*
5409 * Read a record.
5410 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005411 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5412 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5413 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005414 */
Hanno Becker1097b342018-08-15 14:09:41 +01005415
5416/* Helper functions for mbedtls_ssl_read_record(). */
5417static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005418static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5419static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005420
Hanno Becker327c93b2018-08-15 13:56:18 +01005421int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005422 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005423{
5424 int ret;
5425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005427
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005428 if( ssl->keep_current_message == 0 )
5429 {
5430 do {
Simon Butcher99000142016-10-13 17:21:01 +01005431
Hanno Becker26994592018-08-15 14:14:59 +01005432 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005433 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005434 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005435
Hanno Beckere74d5562018-08-15 14:26:08 +01005436 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005437 {
Hanno Becker40f50842018-08-15 14:48:01 +01005438#if defined(MBEDTLS_SSL_PROTO_DTLS)
5439 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005440
Hanno Becker40f50842018-08-15 14:48:01 +01005441 /* We only check for buffered messages if the
5442 * current datagram is fully consumed. */
5443 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005444 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005445 {
Hanno Becker40f50842018-08-15 14:48:01 +01005446 if( ssl_load_buffered_message( ssl ) == 0 )
5447 have_buffered = 1;
5448 }
5449
5450 if( have_buffered == 0 )
5451#endif /* MBEDTLS_SSL_PROTO_DTLS */
5452 {
5453 ret = ssl_get_next_record( ssl );
5454 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5455 continue;
5456
5457 if( ret != 0 )
5458 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005459 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005460 return( ret );
5461 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005462 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005463 }
5464
5465 ret = mbedtls_ssl_handle_message_type( ssl );
5466
Hanno Becker40f50842018-08-15 14:48:01 +01005467#if defined(MBEDTLS_SSL_PROTO_DTLS)
5468 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5469 {
5470 /* Buffer future message */
5471 ret = ssl_buffer_message( ssl );
5472 if( ret != 0 )
5473 return( ret );
5474
5475 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5476 }
5477#endif /* MBEDTLS_SSL_PROTO_DTLS */
5478
Hanno Becker90333da2017-10-10 11:27:13 +01005479 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5480 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005481
5482 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005483 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005484 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005485 return( ret );
5486 }
5487
Hanno Becker327c93b2018-08-15 13:56:18 +01005488 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005489 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005490 {
5491 mbedtls_ssl_update_handshake_status( ssl );
5492 }
Simon Butcher99000142016-10-13 17:21:01 +01005493 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005494 else
Simon Butcher99000142016-10-13 17:21:01 +01005495 {
Hanno Becker02f59072018-08-15 14:00:24 +01005496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005497 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005498 }
5499
5500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5501
5502 return( 0 );
5503}
5504
Hanno Becker40f50842018-08-15 14:48:01 +01005505#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005506static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005507{
Hanno Becker40f50842018-08-15 14:48:01 +01005508 if( ssl->in_left > ssl->next_record_offset )
5509 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005510
Hanno Becker40f50842018-08-15 14:48:01 +01005511 return( 0 );
5512}
5513
5514static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5515{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005516 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005517 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005518 int ret = 0;
5519
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005520 if( hs == NULL )
5521 return( -1 );
5522
Hanno Beckere00ae372018-08-20 09:39:42 +01005523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5524
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005525 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5526 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5527 {
5528 /* Check if we have seen a ChangeCipherSpec before.
5529 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005530 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005531 {
5532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5533 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005534 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005535 }
5536
Hanno Becker39b8bc92018-08-28 17:17:13 +01005537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005538 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5539 ssl->in_msglen = 1;
5540 ssl->in_msg[0] = 1;
5541
5542 /* As long as they are equal, the exact value doesn't matter. */
5543 ssl->in_left = 0;
5544 ssl->next_record_offset = 0;
5545
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005546 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005547 goto exit;
5548 }
Hanno Becker37f95322018-08-16 13:55:32 +01005549
Hanno Beckerb8f50142018-08-28 10:01:34 +01005550#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005551 /* Debug only */
5552 {
5553 unsigned offset;
5554 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5555 {
5556 hs_buf = &hs->buffering.hs[offset];
5557 if( hs_buf->is_valid == 1 )
5558 {
5559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5560 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005561 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005562 }
5563 }
5564 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005565#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005566
5567 /* Check if we have buffered and/or fully reassembled the
5568 * next handshake message. */
5569 hs_buf = &hs->buffering.hs[0];
5570 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5571 {
5572 /* Synthesize a record containing the buffered HS message. */
5573 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5574 ( hs_buf->data[2] << 8 ) |
5575 hs_buf->data[3];
5576
5577 /* Double-check that we haven't accidentally buffered
5578 * a message that doesn't fit into the input buffer. */
5579 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5580 {
5581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5582 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5583 }
5584
5585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5586 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5587 hs_buf->data, msg_len + 12 );
5588
5589 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5590 ssl->in_hslen = msg_len + 12;
5591 ssl->in_msglen = msg_len + 12;
5592 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5593
5594 ret = 0;
5595 goto exit;
5596 }
5597 else
5598 {
5599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5600 hs->in_msg_seq ) );
5601 }
5602
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005603 ret = -1;
5604
5605exit:
5606
5607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5608 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005609}
5610
Hanno Beckera02b0b42018-08-21 17:20:27 +01005611static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5612 size_t desired )
5613{
5614 int offset;
5615 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5617 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005618
Hanno Becker01315ea2018-08-21 17:22:17 +01005619 /* Get rid of future records epoch first, if such exist. */
5620 ssl_free_buffered_record( ssl );
5621
5622 /* Check if we have enough space available now. */
5623 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5624 hs->buffering.total_bytes_buffered ) )
5625 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005627 return( 0 );
5628 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005629
Hanno Becker4f432ad2018-08-28 10:02:32 +01005630 /* We don't have enough space to buffer the next expected handshake
5631 * message. Remove buffers used for future messages to gain space,
5632 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005633 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5634 offset >= 0; offset-- )
5635 {
5636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5637 offset ) );
5638
Hanno Beckerb309b922018-08-23 13:18:05 +01005639 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005640
5641 /* Check if we have enough space available now. */
5642 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5643 hs->buffering.total_bytes_buffered ) )
5644 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005646 return( 0 );
5647 }
5648 }
5649
5650 return( -1 );
5651}
5652
Hanno Becker40f50842018-08-15 14:48:01 +01005653static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5654{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005655 int ret = 0;
5656 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5657
5658 if( hs == NULL )
5659 return( 0 );
5660
5661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5662
5663 switch( ssl->in_msgtype )
5664 {
5665 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005667
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005668 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005669 break;
5670
5671 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005672 {
5673 unsigned recv_msg_seq_offset;
5674 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5675 mbedtls_ssl_hs_buffer *hs_buf;
5676 size_t msg_len = ssl->in_hslen - 12;
5677
5678 /* We should never receive an old handshake
5679 * message - double-check nonetheless. */
5680 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5681 {
5682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5684 }
5685
5686 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5687 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5688 {
5689 /* Silently ignore -- message too far in the future */
5690 MBEDTLS_SSL_DEBUG_MSG( 2,
5691 ( "Ignore future HS message with sequence number %u, "
5692 "buffering window %u - %u",
5693 recv_msg_seq, ssl->handshake->in_msg_seq,
5694 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5695
5696 goto exit;
5697 }
5698
5699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5700 recv_msg_seq, recv_msg_seq_offset ) );
5701
5702 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5703
5704 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005705 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005706 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005707 size_t reassembly_buf_sz;
5708
Hanno Becker37f95322018-08-16 13:55:32 +01005709 hs_buf->is_fragmented =
5710 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5711
5712 /* We copy the message back into the input buffer
5713 * after reassembly, so check that it's not too large.
5714 * This is an implementation-specific limitation
5715 * and not one from the standard, hence it is not
5716 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005717 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005718 {
5719 /* Ignore message */
5720 goto exit;
5721 }
5722
Hanno Beckere0b150f2018-08-21 15:51:03 +01005723 /* Check if we have enough space to buffer the message. */
5724 if( hs->buffering.total_bytes_buffered >
5725 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5726 {
5727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5728 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5729 }
5730
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005731 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5732 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005733
5734 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5735 hs->buffering.total_bytes_buffered ) )
5736 {
5737 if( recv_msg_seq_offset > 0 )
5738 {
5739 /* If we can't buffer a future message because
5740 * of space limitations -- ignore. */
5741 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",
5742 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5743 (unsigned) hs->buffering.total_bytes_buffered ) );
5744 goto exit;
5745 }
Hanno Beckere1801392018-08-21 16:51:05 +01005746 else
5747 {
5748 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",
5749 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5750 (unsigned) hs->buffering.total_bytes_buffered ) );
5751 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005752
Hanno Beckera02b0b42018-08-21 17:20:27 +01005753 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005754 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005755 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",
5756 (unsigned) msg_len,
5757 (unsigned) reassembly_buf_sz,
5758 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005759 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005760 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5761 goto exit;
5762 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005763 }
5764
5765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5766 msg_len ) );
5767
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005768 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5769 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005770 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005771 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005772 goto exit;
5773 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005774 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005775
5776 /* Prepare final header: copy msg_type, length and message_seq,
5777 * then add standardised fragment_offset and fragment_length */
5778 memcpy( hs_buf->data, ssl->in_msg, 6 );
5779 memset( hs_buf->data + 6, 0, 3 );
5780 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5781
5782 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005783
5784 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005785 }
5786 else
5787 {
5788 /* Make sure msg_type and length are consistent */
5789 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5790 {
5791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5792 /* Ignore */
5793 goto exit;
5794 }
5795 }
5796
Hanno Becker4422bbb2018-08-20 09:40:19 +01005797 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005798 {
5799 size_t frag_len, frag_off;
5800 unsigned char * const msg = hs_buf->data + 12;
5801
5802 /*
5803 * Check and copy current fragment
5804 */
5805
5806 /* Validation of header fields already done in
5807 * mbedtls_ssl_prepare_handshake_record(). */
5808 frag_off = ssl_get_hs_frag_off( ssl );
5809 frag_len = ssl_get_hs_frag_len( ssl );
5810
5811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5812 frag_off, frag_len ) );
5813 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5814
5815 if( hs_buf->is_fragmented )
5816 {
5817 unsigned char * const bitmask = msg + msg_len;
5818 ssl_bitmask_set( bitmask, frag_off, frag_len );
5819 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5820 msg_len ) == 0 );
5821 }
5822 else
5823 {
5824 hs_buf->is_complete = 1;
5825 }
5826
5827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5828 hs_buf->is_complete ? "" : "not yet " ) );
5829 }
5830
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005831 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005832 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005833
5834 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005835 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005836 break;
5837 }
5838
5839exit:
5840
5841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5842 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005843}
5844#endif /* MBEDTLS_SSL_PROTO_DTLS */
5845
Hanno Becker1097b342018-08-15 14:09:41 +01005846static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005847{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005848 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005849 * Consume last content-layer message and potentially
5850 * update in_msglen which keeps track of the contents'
5851 * consumption state.
5852 *
5853 * (1) Handshake messages:
5854 * Remove last handshake message, move content
5855 * and adapt in_msglen.
5856 *
5857 * (2) Alert messages:
5858 * Consume whole record content, in_msglen = 0.
5859 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005860 * (3) Change cipher spec:
5861 * Consume whole record content, in_msglen = 0.
5862 *
5863 * (4) Application data:
5864 * Don't do anything - the record layer provides
5865 * the application data as a stream transport
5866 * and consumes through mbedtls_ssl_read only.
5867 *
5868 */
5869
5870 /* Case (1): Handshake messages */
5871 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005872 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005873 /* Hard assertion to be sure that no application data
5874 * is in flight, as corrupting ssl->in_msglen during
5875 * ssl->in_offt != NULL is fatal. */
5876 if( ssl->in_offt != NULL )
5877 {
5878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5879 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5880 }
5881
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005882 /*
5883 * Get next Handshake message in the current record
5884 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005885
Hanno Becker4a810fb2017-05-24 16:27:30 +01005886 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005887 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005888 * current handshake content: If DTLS handshake
5889 * fragmentation is used, that's the fragment
5890 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005891 * size here is faulty and should be changed at
5892 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005893 * (2) While it doesn't seem to cause problems, one
5894 * has to be very careful not to assume that in_hslen
5895 * is always <= in_msglen in a sensible communication.
5896 * Again, it's wrong for DTLS handshake fragmentation.
5897 * The following check is therefore mandatory, and
5898 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005899 * Additionally, ssl->in_hslen might be arbitrarily out of
5900 * bounds after handling a DTLS message with an unexpected
5901 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005902 */
5903 if( ssl->in_hslen < ssl->in_msglen )
5904 {
5905 ssl->in_msglen -= ssl->in_hslen;
5906 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5907 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005908
Hanno Becker4a810fb2017-05-24 16:27:30 +01005909 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5910 ssl->in_msg, ssl->in_msglen );
5911 }
5912 else
5913 {
5914 ssl->in_msglen = 0;
5915 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005916
Hanno Becker4a810fb2017-05-24 16:27:30 +01005917 ssl->in_hslen = 0;
5918 }
5919 /* Case (4): Application data */
5920 else if( ssl->in_offt != NULL )
5921 {
5922 return( 0 );
5923 }
5924 /* Everything else (CCS & Alerts) */
5925 else
5926 {
5927 ssl->in_msglen = 0;
5928 }
5929
Hanno Becker1097b342018-08-15 14:09:41 +01005930 return( 0 );
5931}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005932
Hanno Beckere74d5562018-08-15 14:26:08 +01005933static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5934{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005935 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005936 return( 1 );
5937
5938 return( 0 );
5939}
5940
Hanno Becker5f066e72018-08-16 14:56:31 +01005941#if defined(MBEDTLS_SSL_PROTO_DTLS)
5942
5943static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5944{
5945 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5946 if( hs == NULL )
5947 return;
5948
Hanno Becker01315ea2018-08-21 17:22:17 +01005949 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005950 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005951 hs->buffering.total_bytes_buffered -=
5952 hs->buffering.future_record.len;
5953
5954 mbedtls_free( hs->buffering.future_record.data );
5955 hs->buffering.future_record.data = NULL;
5956 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005957}
5958
5959static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5960{
5961 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5962 unsigned char * rec;
5963 size_t rec_len;
5964 unsigned rec_epoch;
5965
5966 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5967 return( 0 );
5968
5969 if( hs == NULL )
5970 return( 0 );
5971
Hanno Becker5f066e72018-08-16 14:56:31 +01005972 rec = hs->buffering.future_record.data;
5973 rec_len = hs->buffering.future_record.len;
5974 rec_epoch = hs->buffering.future_record.epoch;
5975
5976 if( rec == NULL )
5977 return( 0 );
5978
Hanno Becker4cb782d2018-08-20 11:19:05 +01005979 /* Only consider loading future records if the
5980 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005981 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005982 return( 0 );
5983
Hanno Becker5f066e72018-08-16 14:56:31 +01005984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5985
5986 if( rec_epoch != ssl->in_epoch )
5987 {
5988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5989 goto exit;
5990 }
5991
5992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5993
5994 /* Double-check that the record is not too large */
5995 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5996 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5997 {
5998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6000 }
6001
6002 memcpy( ssl->in_hdr, rec, rec_len );
6003 ssl->in_left = rec_len;
6004 ssl->next_record_offset = 0;
6005
6006 ssl_free_buffered_record( ssl );
6007
6008exit:
6009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6010 return( 0 );
6011}
6012
Hanno Becker519f15d2019-07-11 12:43:20 +01006013static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6014 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006015{
6016 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006017
6018 /* Don't buffer future records outside handshakes. */
6019 if( hs == NULL )
6020 return( 0 );
6021
6022 /* Only buffer handshake records (we are only interested
6023 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006024 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006025 return( 0 );
6026
6027 /* Don't buffer more than one future epoch record. */
6028 if( hs->buffering.future_record.data != NULL )
6029 return( 0 );
6030
Hanno Becker01315ea2018-08-21 17:22:17 +01006031 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006032 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006033 hs->buffering.total_bytes_buffered ) )
6034 {
6035 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 +01006036 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006037 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006038 return( 0 );
6039 }
6040
Hanno Becker5f066e72018-08-16 14:56:31 +01006041 /* Buffer record */
6042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6043 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006044 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006045
6046 /* ssl_parse_record_header() only considers records
6047 * of the next epoch as candidates for buffering. */
6048 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006049 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006050
6051 hs->buffering.future_record.data =
6052 mbedtls_calloc( 1, hs->buffering.future_record.len );
6053 if( hs->buffering.future_record.data == NULL )
6054 {
6055 /* If we run out of RAM trying to buffer a
6056 * record from the next epoch, just ignore. */
6057 return( 0 );
6058 }
6059
Hanno Becker519f15d2019-07-11 12:43:20 +01006060 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006061
Hanno Becker519f15d2019-07-11 12:43:20 +01006062 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006063 return( 0 );
6064}
6065
6066#endif /* MBEDTLS_SSL_PROTO_DTLS */
6067
Hanno Beckere74d5562018-08-15 14:26:08 +01006068static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006069{
6070 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006071 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006072
Hanno Becker5f066e72018-08-16 14:56:31 +01006073#if defined(MBEDTLS_SSL_PROTO_DTLS)
6074 /* We might have buffered a future record; if so,
6075 * and if the epoch matches now, load it.
6076 * On success, this call will set ssl->in_left to
6077 * the length of the buffered record, so that
6078 * the calls to ssl_fetch_input() below will
6079 * essentially be no-ops. */
6080 ret = ssl_load_buffered_record( ssl );
6081 if( ret != 0 )
6082 return( ret );
6083#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006084
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006085 /* Ensure that we have enough space available for the default form
6086 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6087 * with no space for CIDs counted in). */
6088 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6089 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006092 return( ret );
6093 }
6094
Hanno Beckere5e7e782019-07-11 12:29:35 +01006095 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6096 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006098#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006099 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006100 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006101 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6102 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006103 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006104 if( ret != 0 )
6105 return( ret );
6106
6107 /* Fall through to handling of unexpected records */
6108 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6109 }
6110
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006111 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6112 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006113#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006114 /* Reset in pointers to default state for TLS/DTLS records,
6115 * assuming no CID and no offset between record content and
6116 * record plaintext. */
6117 ssl_update_in_pointers( ssl );
6118
Hanno Becker7ae20e02019-07-12 08:33:49 +01006119 /* Setup internal message pointers from record structure. */
6120 ssl->in_msgtype = rec.type;
6121#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6122 ssl->in_len = ssl->in_cid + rec.cid_len;
6123#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6124 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6125 ssl->in_msglen = rec.data_len;
6126
Hanno Becker2fddd372019-07-10 14:37:41 +01006127 ret = ssl_check_client_reconnect( ssl );
6128 if( ret != 0 )
6129 return( ret );
6130#endif
6131
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006132 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006133 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006134
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6136 "(header)" ) );
6137 }
6138 else
6139 {
6140 /* Skip invalid record and the rest of the datagram */
6141 ssl->next_record_offset = 0;
6142 ssl->in_left = 0;
6143
6144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6145 "(header)" ) );
6146 }
6147
6148 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006149 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006150 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006151 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006152#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006153 {
6154 return( ret );
6155 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006156 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006159 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006160 {
Hanno Beckera8814792019-07-10 15:01:45 +01006161 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006162 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006163 if( ssl->next_record_offset < ssl->in_left )
6164 {
6165 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6166 }
6167 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006168 else
6169#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006170 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006171 /*
6172 * Fetch record contents from underlying transport.
6173 */
Hanno Beckera3175662019-07-11 12:50:29 +01006174 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006175 if( ret != 0 )
6176 {
6177 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6178 return( ret );
6179 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006180
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006181 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006182 }
6183
6184 /*
6185 * Decrypt record contents.
6186 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006187
Hanno Beckerfdf66042019-07-11 13:07:45 +01006188 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006191 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006192 {
6193 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006194 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006195 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006196 /* Except when waiting for Finished as a bad mac here
6197 * probably means something went wrong in the handshake
6198 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6199 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6200 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6201 {
6202#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6203 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6204 {
6205 mbedtls_ssl_send_alert_message( ssl,
6206 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6207 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6208 }
6209#endif
6210 return( ret );
6211 }
6212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006214 if( ssl->conf->badmac_limit != 0 &&
6215 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6218 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006219 }
6220#endif
6221
Hanno Becker4a810fb2017-05-24 16:27:30 +01006222 /* As above, invalid records cause
6223 * dismissal of the whole datagram. */
6224
6225 ssl->next_record_offset = 0;
6226 ssl->in_left = 0;
6227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006229 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006230 }
6231
6232 return( ret );
6233 }
6234 else
6235#endif
6236 {
6237 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6239 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241 mbedtls_ssl_send_alert_message( ssl,
6242 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6243 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006244 }
6245#endif
6246 return( ret );
6247 }
6248 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006249
Hanno Becker44d89b22019-07-12 09:40:44 +01006250
6251 /* Reset in pointers to default state for TLS/DTLS records,
6252 * assuming no CID and no offset between record content and
6253 * record plaintext. */
6254 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006255#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6256 ssl->in_len = ssl->in_cid + rec.cid_len;
6257#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6258 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006259
Hanno Becker8685c822019-07-12 09:37:30 +01006260 /* The record content type may change during decryption,
6261 * so re-read it. */
6262 ssl->in_msgtype = rec.type;
6263 /* Also update the input buffer, because unfortunately
6264 * the server-side ssl_parse_client_hello() reparses the
6265 * record header when receiving a ClientHello initiating
6266 * a renegotiation. */
6267 ssl->in_hdr[0] = rec.type;
6268 ssl->in_msg = rec.buf + rec.data_offset;
6269 ssl->in_msglen = rec.data_len;
6270 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6271 ssl->in_len[1] = (unsigned char)( rec.data_len );
6272
Simon Butcher99000142016-10-13 17:21:01 +01006273 return( 0 );
6274}
6275
6276int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6277{
6278 int ret;
6279
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006280 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006281 * Handle particular types of records
6282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006284 {
Simon Butcher99000142016-10-13 17:21:01 +01006285 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6286 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006287 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006289 }
6290
Hanno Beckere678eaa2018-08-21 14:57:46 +01006291 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006292 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006293 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006294 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6296 ssl->in_msglen ) );
6297 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006298 }
6299
Hanno Beckere678eaa2018-08-21 14:57:46 +01006300 if( ssl->in_msg[0] != 1 )
6301 {
6302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6303 ssl->in_msg[0] ) );
6304 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6305 }
6306
6307#if defined(MBEDTLS_SSL_PROTO_DTLS)
6308 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6309 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6310 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6311 {
6312 if( ssl->handshake == NULL )
6313 {
6314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6315 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6316 }
6317
6318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6319 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6320 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006321#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006322 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006324 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006325 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006326 if( ssl->in_msglen != 2 )
6327 {
6328 /* Note: Standard allows for more than one 2 byte alert
6329 to be packed in a single message, but Mbed TLS doesn't
6330 currently support this. */
6331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6332 ssl->in_msglen ) );
6333 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6334 }
6335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006337 ssl->in_msg[0], ssl->in_msg[1] ) );
6338
6339 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006340 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006345 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 }
6348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6350 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6353 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006354 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006355
6356#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6357 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6358 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6359 {
Hanno Becker90333da2017-10-10 11:27:13 +01006360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006361 /* Will be handled when trying to parse ServerHello */
6362 return( 0 );
6363 }
6364#endif
6365
6366#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6367 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6368 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6369 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6370 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6371 {
6372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6373 /* Will be handled in mbedtls_ssl_parse_certificate() */
6374 return( 0 );
6375 }
6376#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6377
6378 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006379 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006380 }
6381
Hanno Beckerc76c6192017-06-06 10:03:17 +01006382#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006383 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006384 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006385 /* Drop unexpected ApplicationData records,
6386 * except at the beginning of renegotiations */
6387 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6388 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6389#if defined(MBEDTLS_SSL_RENEGOTIATION)
6390 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6391 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006392#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006393 )
6394 {
6395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6396 return( MBEDTLS_ERR_SSL_NON_FATAL );
6397 }
6398
6399 if( ssl->handshake != NULL &&
6400 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6401 {
6402 ssl_handshake_wrapup_free_hs_transform( ssl );
6403 }
6404 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006405#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006406
Paul Bakker5121ce52009-01-03 21:22:43 +00006407 return( 0 );
6408}
6409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006410int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006411{
6412 int ret;
6413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6415 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6416 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006417 {
6418 return( ret );
6419 }
6420
6421 return( 0 );
6422}
6423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006425 unsigned char level,
6426 unsigned char message )
6427{
6428 int ret;
6429
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006430 if( ssl == NULL || ssl->conf == NULL )
6431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006437 ssl->out_msglen = 2;
6438 ssl->out_msg[0] = level;
6439 ssl->out_msg[1] = message;
6440
Hanno Becker67bc7c32018-08-06 11:33:50 +01006441 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006444 return( ret );
6445 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006447
6448 return( 0 );
6449}
6450
Hanno Beckerb9d44792019-02-08 07:19:04 +00006451#if defined(MBEDTLS_X509_CRT_PARSE_C)
6452static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6453{
6454#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6455 if( session->peer_cert != NULL )
6456 {
6457 mbedtls_x509_crt_free( session->peer_cert );
6458 mbedtls_free( session->peer_cert );
6459 session->peer_cert = NULL;
6460 }
6461#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6462 if( session->peer_cert_digest != NULL )
6463 {
6464 /* Zeroization is not necessary. */
6465 mbedtls_free( session->peer_cert_digest );
6466 session->peer_cert_digest = NULL;
6467 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6468 session->peer_cert_digest_len = 0;
6469 }
6470#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6471}
6472#endif /* MBEDTLS_X509_CRT_PARSE_C */
6473
Paul Bakker5121ce52009-01-03 21:22:43 +00006474/*
6475 * Handshake functions
6476 */
Hanno Becker21489932019-02-05 13:20:55 +00006477#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006478/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006480{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006481 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6482 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006485
Hanno Becker7177a882019-02-05 13:36:46 +00006486 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006489 ssl->state++;
6490 return( 0 );
6491 }
6492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6494 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006495}
6496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006498{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006499 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6500 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006503
Hanno Becker7177a882019-02-05 13:36:46 +00006504 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006507 ssl->state++;
6508 return( 0 );
6509 }
6510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6512 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006513}
Gilles Peskinef9828522017-05-03 12:28:43 +02006514
Hanno Becker21489932019-02-05 13:20:55 +00006515#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006516/* Some certificate support -> implement write and parse */
6517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006521 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006523 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6524 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006527
Hanno Becker7177a882019-02-05 13:36:46 +00006528 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006531 ssl->state++;
6532 return( 0 );
6533 }
6534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006536 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006537 {
6538 if( ssl->client_auth == 0 )
6539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006541 ssl->state++;
6542 return( 0 );
6543 }
6544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006546 /*
6547 * If using SSLv3 and got no cert, send an Alert message
6548 * (otherwise an empty Certificate message will be sent).
6549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6551 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006552 {
6553 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6555 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6556 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006559 goto write_msg;
6560 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006562 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563#endif /* MBEDTLS_SSL_CLI_C */
6564#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006565 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6570 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006571 }
6572 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006573#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006576
6577 /*
6578 * 0 . 0 handshake type
6579 * 1 . 3 handshake length
6580 * 4 . 6 length of all certs
6581 * 7 . 9 length of cert. 1
6582 * 10 . n-1 peer certificate
6583 * n . n+2 length of cert. 2
6584 * n+3 . ... upper level cert, etc.
6585 */
6586 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006587 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006588
Paul Bakker29087132010-03-21 21:03:34 +00006589 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006590 {
6591 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006592 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006595 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006597 }
6598
6599 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6600 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6601 ssl->out_msg[i + 2] = (unsigned char)( n );
6602
6603 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6604 i += n; crt = crt->next;
6605 }
6606
6607 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6608 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6609 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6610
6611 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6613 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006614
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006615#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006616write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006617#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006618
6619 ssl->state++;
6620
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006621 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006622 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006624 return( ret );
6625 }
6626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006628
Paul Bakkered27a042013-04-18 22:46:23 +02006629 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006630}
6631
Hanno Becker84879e32019-01-31 07:44:03 +00006632#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006633
6634#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006635static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6636 unsigned char *crt_buf,
6637 size_t crt_buf_len )
6638{
6639 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6640
6641 if( peer_crt == NULL )
6642 return( -1 );
6643
6644 if( peer_crt->raw.len != crt_buf_len )
6645 return( -1 );
6646
Hanno Becker46f34d02019-02-08 14:00:04 +00006647 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006648}
Hanno Becker177475a2019-02-05 17:02:46 +00006649#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6650static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6651 unsigned char *crt_buf,
6652 size_t crt_buf_len )
6653{
6654 int ret;
6655 unsigned char const * const peer_cert_digest =
6656 ssl->session->peer_cert_digest;
6657 mbedtls_md_type_t const peer_cert_digest_type =
6658 ssl->session->peer_cert_digest_type;
6659 mbedtls_md_info_t const * const digest_info =
6660 mbedtls_md_info_from_type( peer_cert_digest_type );
6661 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6662 size_t digest_len;
6663
6664 if( peer_cert_digest == NULL || digest_info == NULL )
6665 return( -1 );
6666
6667 digest_len = mbedtls_md_get_size( digest_info );
6668 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6669 return( -1 );
6670
6671 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6672 if( ret != 0 )
6673 return( -1 );
6674
6675 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6676}
6677#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006678#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006679
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006680/*
6681 * Once the certificate message is read, parse it into a cert chain and
6682 * perform basic checks, but leave actual verification to the caller
6683 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006684static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6685 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006686{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006687 int ret;
6688#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6689 int crt_cnt=0;
6690#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006691 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006692 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006697 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6698 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006700 }
6701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6703 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006706 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6707 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006709 }
6710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006712
Paul Bakker5121ce52009-01-03 21:22:43 +00006713 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006715 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006716 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006717
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006718 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006725 }
6726
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006727 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6728 i += 3;
6729
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006730 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006731 while( i < ssl->in_hslen )
6732 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006733 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006734 if ( i + 3 > ssl->in_hslen ) {
6735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006736 mbedtls_ssl_send_alert_message( ssl,
6737 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6738 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006739 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6740 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006741 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6742 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006743 if( ssl->in_msg[i] != 0 )
6744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006746 mbedtls_ssl_send_alert_message( ssl,
6747 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6748 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006750 }
6751
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006752 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006753 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6754 | (unsigned int) ssl->in_msg[i + 2];
6755 i += 3;
6756
6757 if( n < 128 || i + n > ssl->in_hslen )
6758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006760 mbedtls_ssl_send_alert_message( ssl,
6761 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6762 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006764 }
6765
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006766 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006767#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6768 if( crt_cnt++ == 0 &&
6769 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6770 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006771 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006772 /* During client-side renegotiation, check that the server's
6773 * end-CRTs hasn't changed compared to the initial handshake,
6774 * mitigating the triple handshake attack. On success, reuse
6775 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006776 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6777 if( ssl_check_peer_crt_unchanged( ssl,
6778 &ssl->in_msg[i],
6779 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006780 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6782 mbedtls_ssl_send_alert_message( ssl,
6783 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6784 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6785 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006786 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006787
6788 /* Now we can safely free the original chain. */
6789 ssl_clear_peer_cert( ssl->session );
6790 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006791#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6792
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006793 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006794#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006795 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006796#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006797 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006798 * it in-place from the input buffer instead of making a copy. */
6799 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6800#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006801 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006802 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006803 case 0: /*ok*/
6804 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6805 /* Ignore certificate with an unknown algorithm: maybe a
6806 prior certificate was already trusted. */
6807 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006808
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006809 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6810 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6811 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006812
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006813 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6814 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6815 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006816
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006817 default:
6818 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6819 crt_parse_der_failed:
6820 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6821 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6822 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006823 }
6824
6825 i += n;
6826 }
6827
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006828 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006829 return( 0 );
6830}
6831
Hanno Becker4a55f632019-02-05 12:49:06 +00006832#if defined(MBEDTLS_SSL_SRV_C)
6833static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6834{
6835 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6836 return( -1 );
6837
6838#if defined(MBEDTLS_SSL_PROTO_SSL3)
6839 /*
6840 * Check if the client sent an empty certificate
6841 */
6842 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6843 {
6844 if( ssl->in_msglen == 2 &&
6845 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6846 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6847 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6848 {
6849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6850 return( 0 );
6851 }
6852
6853 return( -1 );
6854 }
6855#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6856
6857#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6858 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6859 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6860 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6861 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6862 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6863 {
6864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6865 return( 0 );
6866 }
6867
6868 return( -1 );
6869#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6870 MBEDTLS_SSL_PROTO_TLS1_2 */
6871}
6872#endif /* MBEDTLS_SSL_SRV_C */
6873
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006874/* Check if a certificate message is expected.
6875 * Return either
6876 * - SSL_CERTIFICATE_EXPECTED, or
6877 * - SSL_CERTIFICATE_SKIP
6878 * indicating whether a Certificate message is expected or not.
6879 */
6880#define SSL_CERTIFICATE_EXPECTED 0
6881#define SSL_CERTIFICATE_SKIP 1
6882static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6883 int authmode )
6884{
6885 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006886 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006887
6888 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6889 return( SSL_CERTIFICATE_SKIP );
6890
6891#if defined(MBEDTLS_SSL_SRV_C)
6892 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6893 {
6894 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6895 return( SSL_CERTIFICATE_SKIP );
6896
6897 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6898 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006899 ssl->session_negotiate->verify_result =
6900 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6901 return( SSL_CERTIFICATE_SKIP );
6902 }
6903 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006904#else
6905 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006906#endif /* MBEDTLS_SSL_SRV_C */
6907
6908 return( SSL_CERTIFICATE_EXPECTED );
6909}
6910
Hanno Becker68636192019-02-05 14:36:34 +00006911static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6912 int authmode,
6913 mbedtls_x509_crt *chain,
6914 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006915{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006916 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006917 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006918 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006919 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006920
Hanno Becker8927c832019-04-03 12:52:50 +01006921 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6922 void *p_vrfy;
6923
Hanno Becker68636192019-02-05 14:36:34 +00006924 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6925 return( 0 );
6926
Hanno Becker8927c832019-04-03 12:52:50 +01006927 if( ssl->f_vrfy != NULL )
6928 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006930 f_vrfy = ssl->f_vrfy;
6931 p_vrfy = ssl->p_vrfy;
6932 }
6933 else
6934 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006936 f_vrfy = ssl->conf->f_vrfy;
6937 p_vrfy = ssl->conf->p_vrfy;
6938 }
6939
Hanno Becker68636192019-02-05 14:36:34 +00006940 /*
6941 * Main check: verify certificate
6942 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006943#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6944 if( ssl->conf->f_ca_cb != NULL )
6945 {
6946 ((void) rs_ctx);
6947 have_ca_chain = 1;
6948
6949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006950 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006951 chain,
6952 ssl->conf->f_ca_cb,
6953 ssl->conf->p_ca_cb,
6954 ssl->conf->cert_profile,
6955 ssl->hostname,
6956 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006957 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006958 }
6959 else
6960#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6961 {
6962 mbedtls_x509_crt *ca_chain;
6963 mbedtls_x509_crl *ca_crl;
6964
6965#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6966 if( ssl->handshake->sni_ca_chain != NULL )
6967 {
6968 ca_chain = ssl->handshake->sni_ca_chain;
6969 ca_crl = ssl->handshake->sni_ca_crl;
6970 }
6971 else
6972#endif
6973 {
6974 ca_chain = ssl->conf->ca_chain;
6975 ca_crl = ssl->conf->ca_crl;
6976 }
6977
6978 if( ca_chain != NULL )
6979 have_ca_chain = 1;
6980
6981 ret = mbedtls_x509_crt_verify_restartable(
6982 chain,
6983 ca_chain, ca_crl,
6984 ssl->conf->cert_profile,
6985 ssl->hostname,
6986 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006987 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006988 }
Hanno Becker68636192019-02-05 14:36:34 +00006989
6990 if( ret != 0 )
6991 {
6992 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6993 }
6994
6995#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6996 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6997 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6998#endif
6999
7000 /*
7001 * Secondary checks: always done, but change 'ret' only if it was 0
7002 */
7003
7004#if defined(MBEDTLS_ECP_C)
7005 {
7006 const mbedtls_pk_context *pk = &chain->pk;
7007
7008 /* If certificate uses an EC key, make sure the curve is OK */
7009 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7010 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7011 {
7012 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7013
7014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7015 if( ret == 0 )
7016 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7017 }
7018 }
7019#endif /* MBEDTLS_ECP_C */
7020
7021 if( mbedtls_ssl_check_cert_usage( chain,
7022 ciphersuite_info,
7023 ! ssl->conf->endpoint,
7024 &ssl->session_negotiate->verify_result ) != 0 )
7025 {
7026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7027 if( ret == 0 )
7028 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7029 }
7030
7031 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7032 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7033 * with details encoded in the verification flags. All other kinds
7034 * of error codes, including those from the user provided f_vrfy
7035 * functions, are treated as fatal and lead to a failure of
7036 * ssl_parse_certificate even if verification was optional. */
7037 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7038 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7039 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7040 {
7041 ret = 0;
7042 }
7043
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007044 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007045 {
7046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7047 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7048 }
7049
7050 if( ret != 0 )
7051 {
7052 uint8_t alert;
7053
7054 /* The certificate may have been rejected for several reasons.
7055 Pick one and send the corresponding alert. Which alert to send
7056 may be a subject of debate in some cases. */
7057 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7058 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7059 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7060 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7061 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7062 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7063 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7064 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7065 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7066 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7067 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7068 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7069 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7070 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7071 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7072 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7073 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7074 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7075 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7076 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7077 else
7078 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7079 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7080 alert );
7081 }
7082
7083#if defined(MBEDTLS_DEBUG_C)
7084 if( ssl->session_negotiate->verify_result != 0 )
7085 {
7086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7087 ssl->session_negotiate->verify_result ) );
7088 }
7089 else
7090 {
7091 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7092 }
7093#endif /* MBEDTLS_DEBUG_C */
7094
7095 return( ret );
7096}
7097
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007098#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7099static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7100 unsigned char *start, size_t len )
7101{
7102 int ret;
7103 /* Remember digest of the peer's end-CRT. */
7104 ssl->session_negotiate->peer_cert_digest =
7105 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7106 if( ssl->session_negotiate->peer_cert_digest == NULL )
7107 {
7108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7109 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7110 mbedtls_ssl_send_alert_message( ssl,
7111 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7112 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7113
7114 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7115 }
7116
7117 ret = mbedtls_md( mbedtls_md_info_from_type(
7118 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7119 start, len,
7120 ssl->session_negotiate->peer_cert_digest );
7121
7122 ssl->session_negotiate->peer_cert_digest_type =
7123 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7124 ssl->session_negotiate->peer_cert_digest_len =
7125 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7126
7127 return( ret );
7128}
7129
7130static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7131 unsigned char *start, size_t len )
7132{
7133 unsigned char *end = start + len;
7134 int ret;
7135
7136 /* Make a copy of the peer's raw public key. */
7137 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7138 ret = mbedtls_pk_parse_subpubkey( &start, end,
7139 &ssl->handshake->peer_pubkey );
7140 if( ret != 0 )
7141 {
7142 /* We should have parsed the public key before. */
7143 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7144 }
7145
7146 return( 0 );
7147}
7148#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7149
Hanno Becker68636192019-02-05 14:36:34 +00007150int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7151{
7152 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007153 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007154#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7155 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7156 ? ssl->handshake->sni_authmode
7157 : ssl->conf->authmode;
7158#else
7159 const int authmode = ssl->conf->authmode;
7160#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007161 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007162 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007163
7164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7165
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007166 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7167 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007168 {
7169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007170 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007171 }
7172
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007173#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7174 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007175 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007176 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007177 chain = ssl->handshake->ecrs_peer_cert;
7178 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007179 goto crt_verify;
7180 }
7181#endif
7182
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007183 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007184 {
7185 /* mbedtls_ssl_read_record may have sent an alert already. We
7186 let it decide whether to alert. */
7187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007188 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007189 }
7190
Hanno Becker4a55f632019-02-05 12:49:06 +00007191#if defined(MBEDTLS_SSL_SRV_C)
7192 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7193 {
7194 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007195
7196 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007197 ret = 0;
7198 else
7199 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007200
Hanno Becker6bdfab22019-02-05 13:11:17 +00007201 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007202 }
7203#endif /* MBEDTLS_SSL_SRV_C */
7204
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007205 /* Clear existing peer CRT structure in case we tried to
7206 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007207 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007208
7209 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7210 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007211 {
7212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7213 sizeof( mbedtls_x509_crt ) ) );
7214 mbedtls_ssl_send_alert_message( ssl,
7215 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7216 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007217
Hanno Becker3dad3112019-02-05 17:19:52 +00007218 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7219 goto exit;
7220 }
7221 mbedtls_x509_crt_init( chain );
7222
7223 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007224 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007225 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007226
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007227#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7228 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007229 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007230
7231crt_verify:
7232 if( ssl->handshake->ecrs_enabled)
7233 rs_ctx = &ssl->handshake->ecrs_ctx;
7234#endif
7235
Hanno Becker68636192019-02-05 14:36:34 +00007236 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007237 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007238 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007239 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007240
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007241#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007242 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007243 unsigned char *crt_start, *pk_start;
7244 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007245
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007246 /* We parse the CRT chain without copying, so
7247 * these pointers point into the input buffer,
7248 * and are hence still valid after freeing the
7249 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007250
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007251 crt_start = chain->raw.p;
7252 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007253
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007254 pk_start = chain->pk_raw.p;
7255 pk_len = chain->pk_raw.len;
7256
7257 /* Free the CRT structures before computing
7258 * digest and copying the peer's public key. */
7259 mbedtls_x509_crt_free( chain );
7260 mbedtls_free( chain );
7261 chain = NULL;
7262
7263 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007264 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007265 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007266
7267 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7268 if( ret != 0 )
7269 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007270 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007271#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7272 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007273 ssl->session_negotiate->peer_cert = chain;
7274 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007275#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007278
Hanno Becker6bdfab22019-02-05 13:11:17 +00007279exit:
7280
Hanno Becker3dad3112019-02-05 17:19:52 +00007281 if( ret == 0 )
7282 ssl->state++;
7283
7284#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7285 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7286 {
7287 ssl->handshake->ecrs_peer_cert = chain;
7288 chain = NULL;
7289 }
7290#endif
7291
7292 if( chain != NULL )
7293 {
7294 mbedtls_x509_crt_free( chain );
7295 mbedtls_free( chain );
7296 }
7297
Paul Bakker5121ce52009-01-03 21:22:43 +00007298 return( ret );
7299}
Hanno Becker21489932019-02-05 13:20:55 +00007300#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007303{
7304 int ret;
7305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007309 ssl->out_msglen = 1;
7310 ssl->out_msg[0] = 1;
7311
Paul Bakker5121ce52009-01-03 21:22:43 +00007312 ssl->state++;
7313
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007314 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007315 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007317 return( ret );
7318 }
7319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007321
7322 return( 0 );
7323}
7324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007326{
7327 int ret;
7328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007330
Hanno Becker327c93b2018-08-15 13:56:18 +01007331 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007334 return( ret );
7335 }
7336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007340 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7341 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007343 }
7344
Hanno Beckere678eaa2018-08-21 14:57:46 +01007345 /* CCS records are only accepted if they have length 1 and content '1',
7346 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007347
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007348 /*
7349 * Switch to our negotiated transform and session parameters for inbound
7350 * data.
7351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007353 ssl->transform_in = ssl->transform_negotiate;
7354 ssl->session_in = ssl->session_negotiate;
7355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007357 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007360 ssl_dtls_replay_reset( ssl );
7361#endif
7362
7363 /* Increment epoch */
7364 if( ++ssl->in_epoch == 0 )
7365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007367 /* This is highly unlikely to happen for legitimate reasons, so
7368 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007370 }
7371 }
7372 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007374 memset( ssl->in_ctr, 0, 8 );
7375
Hanno Becker79594fd2019-05-08 09:38:41 +01007376 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7379 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007384 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7385 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007387 }
7388 }
7389#endif
7390
Paul Bakker5121ce52009-01-03 21:22:43 +00007391 ssl->state++;
7392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007394
7395 return( 0 );
7396}
7397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7399 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007400{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007401 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7404 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7405 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007406 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007407 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007408#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7410#if defined(MBEDTLS_SHA512_C)
7411 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007412 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7413 else
7414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415#if defined(MBEDTLS_SHA256_C)
7416 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007417 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007418 else
7419#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007423 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007424 }
Paul Bakker380da532012-04-18 16:10:25 +00007425}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007428{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7430 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007431 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7432 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007433#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7435#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007436#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007437 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007438 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7439#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007440 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007441#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007442#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007444#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007445 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007446 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007447#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007448 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007449#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007450#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007452}
7453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007455 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007456{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7458 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007459 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7460 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007461#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7463#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007464#if defined(MBEDTLS_USE_PSA_CRYPTO)
7465 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7466#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007467 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007468#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007469#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007471#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007472 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007473#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007474 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007475#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007476#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007478}
7479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7481 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7482static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007483 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007484{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007485 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7486 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007487}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007488#endif
Paul Bakker380da532012-04-18 16:10:25 +00007489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7491#if defined(MBEDTLS_SHA256_C)
7492static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007493 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007494{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007495#if defined(MBEDTLS_USE_PSA_CRYPTO)
7496 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7497#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007498 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007499#endif
Paul Bakker380da532012-04-18 16:10:25 +00007500}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007501#endif
Paul Bakker380da532012-04-18 16:10:25 +00007502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503#if defined(MBEDTLS_SHA512_C)
7504static void ssl_update_checksum_sha384( 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{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007507#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007508 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007509#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007510 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007511#endif
Paul Bakker380da532012-04-18 16:10:25 +00007512}
Paul Bakker769075d2012-11-24 11:26:46 +01007513#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007517static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007519{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007520 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007521 mbedtls_md5_context md5;
7522 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007523
Paul Bakker5121ce52009-01-03 21:22:43 +00007524 unsigned char padbuf[48];
7525 unsigned char md5sum[16];
7526 unsigned char sha1sum[20];
7527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007529 if( !session )
7530 session = ssl->session;
7531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007533
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007534 mbedtls_md5_init( &md5 );
7535 mbedtls_sha1_init( &sha1 );
7536
7537 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7538 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007539
7540 /*
7541 * SSLv3:
7542 * hash =
7543 * MD5( master + pad2 +
7544 * MD5( handshake + sender + master + pad1 ) )
7545 * + SHA1( master + pad2 +
7546 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007547 */
7548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007550 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7551 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007552#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007555 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7556 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007557#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007560 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007561
Paul Bakker1ef83d62012-04-11 12:09:53 +00007562 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007563
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007564 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7565 mbedtls_md5_update_ret( &md5, session->master, 48 );
7566 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7567 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007568
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007569 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7570 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7571 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7572 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007573
Paul Bakker1ef83d62012-04-11 12:09:53 +00007574 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007575
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007576 mbedtls_md5_starts_ret( &md5 );
7577 mbedtls_md5_update_ret( &md5, session->master, 48 );
7578 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7579 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7580 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007581
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007582 mbedtls_sha1_starts_ret( &sha1 );
7583 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7584 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7585 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7586 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007589
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007590 mbedtls_md5_free( &md5 );
7591 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007592
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007593 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7594 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7595 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007602static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007604{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007605 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007606 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007607 mbedtls_md5_context md5;
7608 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007609 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007612 if( !session )
7613 session = ssl->session;
7614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007616
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007617 mbedtls_md5_init( &md5 );
7618 mbedtls_sha1_init( &sha1 );
7619
7620 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7621 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007622
Paul Bakker1ef83d62012-04-11 12:09:53 +00007623 /*
7624 * TLSv1:
7625 * hash = PRF( master, finished_label,
7626 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7627 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007630 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7631 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007632#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007635 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7636 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007637#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007640 ? "client finished"
7641 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007642
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007643 mbedtls_md5_finish_ret( &md5, padbuf );
7644 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007645
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007646 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007647 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007650
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007651 mbedtls_md5_free( &md5 );
7652 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007653
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007654 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007657}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7661#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007662static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007664{
7665 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007666 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007667 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007668#if defined(MBEDTLS_USE_PSA_CRYPTO)
7669 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007670 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007671 psa_status_t status;
7672#else
7673 mbedtls_sha256_context sha256;
7674#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007677 if( !session )
7678 session = ssl->session;
7679
Andrzej Kurekeb342242019-01-29 09:14:33 -05007680 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7681 ? "client finished"
7682 : "server finished";
7683
7684#if defined(MBEDTLS_USE_PSA_CRYPTO)
7685 sha256_psa = psa_hash_operation_init();
7686
7687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7688
7689 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7690 if( status != PSA_SUCCESS )
7691 {
7692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7693 return;
7694 }
7695
7696 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7697 if( status != PSA_SUCCESS )
7698 {
7699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7700 return;
7701 }
7702 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7703#else
7704
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007705 mbedtls_sha256_init( &sha256 );
7706
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007708
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007709 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007710
7711 /*
7712 * TLSv1.2:
7713 * hash = PRF( master, finished_label,
7714 * Hash( handshake ) )[0.11]
7715 */
7716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717#if !defined(MBEDTLS_SHA256_ALT)
7718 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007719 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007720#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007721
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007722 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007723 mbedtls_sha256_free( &sha256 );
7724#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007725
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007726 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007727 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007730
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007731 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007734}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007738static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007740{
7741 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007742 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007743 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007744#if defined(MBEDTLS_USE_PSA_CRYPTO)
7745 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007746 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007747 psa_status_t status;
7748#else
7749 mbedtls_sha512_context sha512;
7750#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007753 if( !session )
7754 session = ssl->session;
7755
Andrzej Kurekeb342242019-01-29 09:14:33 -05007756 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7757 ? "client finished"
7758 : "server finished";
7759
7760#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007761 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007762
7763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7764
Andrzej Kurek972fba52019-01-30 03:29:12 -05007765 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007766 if( status != PSA_SUCCESS )
7767 {
7768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7769 return;
7770 }
7771
Andrzej Kurek972fba52019-01-30 03:29:12 -05007772 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007773 if( status != PSA_SUCCESS )
7774 {
7775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7776 return;
7777 }
7778 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7779#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007780 mbedtls_sha512_init( &sha512 );
7781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007783
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007784 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007785
7786 /*
7787 * TLSv1.2:
7788 * hash = PRF( master, finished_label,
7789 * Hash( handshake ) )[0.11]
7790 */
7791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007793 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7794 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007795#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007796
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007797 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007798 mbedtls_sha512_free( &sha512 );
7799#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007800
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007801 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007802 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007805
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007806 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007809}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810#endif /* MBEDTLS_SHA512_C */
7811#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007814{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007816
7817 /*
7818 * Free our handshake params
7819 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007820 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007822 ssl->handshake = NULL;
7823
7824 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007825 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007826 */
7827 if( ssl->transform )
7828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 mbedtls_ssl_transform_free( ssl->transform );
7830 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007831 }
7832 ssl->transform = ssl->transform_negotiate;
7833 ssl->transform_negotiate = NULL;
7834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007836}
7837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007839{
7840 int resume = ssl->handshake->resume;
7841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844#if defined(MBEDTLS_SSL_RENEGOTIATION)
7845 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007848 ssl->renego_records_seen = 0;
7849 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007850#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007851
7852 /*
7853 * Free the previous session and switch in the current one
7854 */
Paul Bakker0a597072012-09-25 21:55:46 +00007855 if( ssl->session )
7856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007858 /* RFC 7366 3.1: keep the EtM state */
7859 ssl->session_negotiate->encrypt_then_mac =
7860 ssl->session->encrypt_then_mac;
7861#endif
7862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 mbedtls_ssl_session_free( ssl->session );
7864 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007865 }
7866 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007867 ssl->session_negotiate = NULL;
7868
Paul Bakker0a597072012-09-25 21:55:46 +00007869 /*
7870 * Add cache entry
7871 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007872 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007873 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007874 resume == 0 )
7875 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007876 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007878 }
Paul Bakker0a597072012-09-25 21:55:46 +00007879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007881 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007882 ssl->handshake->flight != NULL )
7883 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007884 /* Cancel handshake timer */
7885 ssl_set_timer( ssl, 0 );
7886
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007887 /* Keep last flight around in case we need to resend it:
7888 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007890 }
7891 else
7892#endif
7893 ssl_handshake_wrapup_free_hs_transform( ssl );
7894
Paul Bakker48916f92012-09-16 19:57:18 +00007895 ssl->state++;
7896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007898}
7899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007901{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007902 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007905
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007906 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007907
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007908 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007909
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007910 /*
7911 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7912 * may define some other value. Currently (early 2016), no defined
7913 * ciphersuite does this (and this is unlikely to change as activity has
7914 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007919 ssl->verify_data_len = hash_len;
7920 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007921#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007922
Paul Bakker5121ce52009-01-03 21:22:43 +00007923 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7925 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007926
7927 /*
7928 * In case of session resuming, invert the client and server
7929 * ChangeCipherSpec messages order.
7930 */
Paul Bakker0a597072012-09-25 21:55:46 +00007931 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007934 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007936#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007938 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007940#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007941 }
7942 else
7943 ssl->state++;
7944
Paul Bakker48916f92012-09-16 19:57:18 +00007945 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007946 * Switch to our negotiated transform and session parameters for outbound
7947 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007952 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007953 {
7954 unsigned char i;
7955
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007956 /* Remember current epoch settings for resending */
7957 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007958 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007959
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007960 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007961 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007962
7963 /* Increment epoch */
7964 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007965 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007966 break;
7967
7968 /* The loop goes to its end iff the counter is wrapping */
7969 if( i == 0 )
7970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7972 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007973 }
7974 }
7975 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007977 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007978
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007979 ssl->transform_out = ssl->transform_negotiate;
7980 ssl->session_out = ssl->session_negotiate;
7981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7983 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7988 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007989 }
7990 }
7991#endif
7992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007994 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007996#endif
7997
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007998 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007999 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008001 return( ret );
8002 }
8003
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008004#if defined(MBEDTLS_SSL_PROTO_DTLS)
8005 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8006 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8007 {
8008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8009 return( ret );
8010 }
8011#endif
8012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008014
8015 return( 0 );
8016}
8017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008019#define SSL_MAX_HASH_LEN 36
8020#else
8021#define SSL_MAX_HASH_LEN 12
8022#endif
8023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008025{
Paul Bakker23986e52011-04-24 08:57:21 +00008026 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008027 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008028 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008031
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008032 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008033
Hanno Becker327c93b2018-08-15 13:56:18 +01008034 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008037 return( ret );
8038 }
8039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008040 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008043 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8044 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008046 }
8047
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008048 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008049#if defined(MBEDTLS_SSL_PROTO_SSL3)
8050 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008051 hash_len = 36;
8052 else
8053#endif
8054 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8057 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008060 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8061 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008063 }
8064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008066 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008069 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8070 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008072 }
8073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008075 ssl->verify_data_len = hash_len;
8076 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008077#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008078
Paul Bakker0a597072012-09-25 21:55:46 +00008079 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008082 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008086 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008088#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008089 }
8090 else
8091 ssl->state++;
8092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008094 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008096#endif
8097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008099
8100 return( 0 );
8101}
8102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008107#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8108 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8109 mbedtls_md5_init( &handshake->fin_md5 );
8110 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008111 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8112 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8115#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008116#if defined(MBEDTLS_USE_PSA_CRYPTO)
8117 handshake->fin_sha256_psa = psa_hash_operation_init();
8118 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8119#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008121 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008122#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008124#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008125#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008126 handshake->fin_sha384_psa = psa_hash_operation_init();
8127 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008128#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008130 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008131#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008132#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008134
8135 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008136
8137#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8138 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8139 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8140#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142#if defined(MBEDTLS_DHM_C)
8143 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145#if defined(MBEDTLS_ECDH_C)
8146 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008147#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008148#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008149 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008150#if defined(MBEDTLS_SSL_CLI_C)
8151 handshake->ecjpake_cache = NULL;
8152 handshake->ecjpake_cache_len = 0;
8153#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008154#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008155
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008156#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008157 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008158#endif
8159
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008160#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8161 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8162#endif
Hanno Becker75173122019-02-06 16:18:31 +00008163
8164#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8165 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8166 mbedtls_pk_init( &handshake->peer_pubkey );
8167#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008168}
8169
Hanno Beckera18d1322018-01-03 14:27:32 +00008170void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8175 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008176
Hanno Beckerd56ed242018-01-03 15:32:51 +00008177#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178 mbedtls_md_init( &transform->md_ctx_enc );
8179 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008180#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008181}
8182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008184{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008186}
8187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008189{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008190 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008191 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008193 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008195 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008196 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008197
8198 /*
8199 * Either the pointers are now NULL or cleared properly and can be freed.
8200 * Now allocate missing structures.
8201 */
8202 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008203 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008204 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008205 }
Paul Bakker48916f92012-09-16 19:57:18 +00008206
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008207 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008208 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008209 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008210 }
Paul Bakker48916f92012-09-16 19:57:18 +00008211
Paul Bakker82788fb2014-10-20 13:59:19 +02008212 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008213 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008214 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008215 }
Paul Bakker48916f92012-09-16 19:57:18 +00008216
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008217 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008218 if( ssl->handshake == NULL ||
8219 ssl->transform_negotiate == NULL ||
8220 ssl->session_negotiate == NULL )
8221 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008224 mbedtls_free( ssl->handshake );
8225 mbedtls_free( ssl->transform_negotiate );
8226 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008227
8228 ssl->handshake = NULL;
8229 ssl->transform_negotiate = NULL;
8230 ssl->session_negotiate = NULL;
8231
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008232 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008233 }
8234
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008235 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008237 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008238 ssl_handshake_params_init( ssl->handshake );
8239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008240#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008241 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8242 {
8243 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008244
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008245 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8246 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8247 else
8248 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008249
8250 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008251 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008252#endif
8253
Paul Bakker48916f92012-09-16 19:57:18 +00008254 return( 0 );
8255}
8256
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008257#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008258/* Dummy cookie callbacks for defaults */
8259static int ssl_cookie_write_dummy( void *ctx,
8260 unsigned char **p, unsigned char *end,
8261 const unsigned char *cli_id, size_t cli_id_len )
8262{
8263 ((void) ctx);
8264 ((void) p);
8265 ((void) end);
8266 ((void) cli_id);
8267 ((void) cli_id_len);
8268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008270}
8271
8272static int ssl_cookie_check_dummy( void *ctx,
8273 const unsigned char *cookie, size_t cookie_len,
8274 const unsigned char *cli_id, size_t cli_id_len )
8275{
8276 ((void) ctx);
8277 ((void) cookie);
8278 ((void) cookie_len);
8279 ((void) cli_id);
8280 ((void) cli_id_len);
8281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008283}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008284#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008285
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008286/* Once ssl->out_hdr as the address of the beginning of the
8287 * next outgoing record is set, deduce the other pointers.
8288 *
8289 * Note: For TLS, we save the implicit record sequence number
8290 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8291 * and the caller has to make sure there's space for this.
8292 */
8293
8294static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8295 mbedtls_ssl_transform *transform )
8296{
8297#if defined(MBEDTLS_SSL_PROTO_DTLS)
8298 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8299 {
8300 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008301#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008302 ssl->out_cid = ssl->out_ctr + 8;
8303 ssl->out_len = ssl->out_cid;
8304 if( transform != NULL )
8305 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008306#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008307 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008308#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008309 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008310 }
8311 else
8312#endif
8313 {
8314 ssl->out_ctr = ssl->out_hdr - 8;
8315 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008316#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008317 ssl->out_cid = ssl->out_len;
8318#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008319 ssl->out_iv = ssl->out_hdr + 5;
8320 }
8321
8322 /* Adjust out_msg to make space for explicit IV, if used. */
8323 if( transform != NULL &&
8324 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8325 {
8326 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8327 }
8328 else
8329 ssl->out_msg = ssl->out_iv;
8330}
8331
8332/* Once ssl->in_hdr as the address of the beginning of the
8333 * next incoming record is set, deduce the other pointers.
8334 *
8335 * Note: For TLS, we save the implicit record sequence number
8336 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8337 * and the caller has to make sure there's space for this.
8338 */
8339
Hanno Becker79594fd2019-05-08 09:38:41 +01008340static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008341{
Hanno Becker79594fd2019-05-08 09:38:41 +01008342 /* This function sets the pointers to match the case
8343 * of unprotected TLS/DTLS records, with both ssl->in_iv
8344 * and ssl->in_msg pointing to the beginning of the record
8345 * content.
8346 *
8347 * When decrypting a protected record, ssl->in_msg
8348 * will be shifted to point to the beginning of the
8349 * record plaintext.
8350 */
8351
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008352#if defined(MBEDTLS_SSL_PROTO_DTLS)
8353 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8354 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008355 /* This sets the header pointers to match records
8356 * without CID. When we receive a record containing
8357 * a CID, the fields are shifted accordingly in
8358 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008359 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008360#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008361 ssl->in_cid = ssl->in_ctr + 8;
8362 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008363#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008364 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008365#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008366 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008367 }
8368 else
8369#endif
8370 {
8371 ssl->in_ctr = ssl->in_hdr - 8;
8372 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008373#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008374 ssl->in_cid = ssl->in_len;
8375#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008376 ssl->in_iv = ssl->in_hdr + 5;
8377 }
8378
Hanno Becker79594fd2019-05-08 09:38:41 +01008379 /* This will be adjusted at record decryption time. */
8380 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008381}
8382
Paul Bakker5121ce52009-01-03 21:22:43 +00008383/*
8384 * Initialize an SSL context
8385 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008386void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8387{
8388 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8389}
8390
8391/*
8392 * Setup an SSL context
8393 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008394
8395static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8396{
8397 /* Set the incoming and outgoing record pointers. */
8398#if defined(MBEDTLS_SSL_PROTO_DTLS)
8399 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8400 {
8401 ssl->out_hdr = ssl->out_buf;
8402 ssl->in_hdr = ssl->in_buf;
8403 }
8404 else
8405#endif /* MBEDTLS_SSL_PROTO_DTLS */
8406 {
8407 ssl->out_hdr = ssl->out_buf + 8;
8408 ssl->in_hdr = ssl->in_buf + 8;
8409 }
8410
8411 /* Derive other internal pointers. */
8412 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008413 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008414}
8415
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008416int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008417 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008418{
Paul Bakker48916f92012-09-16 19:57:18 +00008419 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008420
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008421 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008422
8423 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008424 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008425 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008426
8427 /* Set to NULL in case of an error condition */
8428 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008429
Angus Grattond8213d02016-05-25 20:56:48 +10008430 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8431 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008432 {
Angus Grattond8213d02016-05-25 20:56:48 +10008433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008434 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008435 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008436 }
8437
8438 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8439 if( ssl->out_buf == NULL )
8440 {
8441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008442 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008443 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008444 }
8445
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008446 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008447
Paul Bakker48916f92012-09-16 19:57:18 +00008448 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008449 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008450
8451 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008452
8453error:
8454 mbedtls_free( ssl->in_buf );
8455 mbedtls_free( ssl->out_buf );
8456
8457 ssl->conf = NULL;
8458
8459 ssl->in_buf = NULL;
8460 ssl->out_buf = NULL;
8461
8462 ssl->in_hdr = NULL;
8463 ssl->in_ctr = NULL;
8464 ssl->in_len = NULL;
8465 ssl->in_iv = NULL;
8466 ssl->in_msg = NULL;
8467
8468 ssl->out_hdr = NULL;
8469 ssl->out_ctr = NULL;
8470 ssl->out_len = NULL;
8471 ssl->out_iv = NULL;
8472 ssl->out_msg = NULL;
8473
k-stachowiak9f7798e2018-07-31 16:52:32 +02008474 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008475}
8476
8477/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008478 * Reset an initialized and used SSL context for re-use while retaining
8479 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008480 *
8481 * If partial is non-zero, keep data in the input buffer and client ID.
8482 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008483 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008484static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008485{
Paul Bakker48916f92012-09-16 19:57:18 +00008486 int ret;
8487
Hanno Becker7e772132018-08-10 12:38:21 +01008488#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8489 !defined(MBEDTLS_SSL_SRV_C)
8490 ((void) partial);
8491#endif
8492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008493 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008494
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008495 /* Cancel any possibly running timer */
8496 ssl_set_timer( ssl, 0 );
8497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498#if defined(MBEDTLS_SSL_RENEGOTIATION)
8499 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008500 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008501
8502 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008503 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8504 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008505#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008507
Paul Bakker7eb013f2011-10-06 12:37:39 +00008508 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008509 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008510
8511 ssl->in_msgtype = 0;
8512 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008514 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008515 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008516#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008517#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008518 ssl_dtls_replay_reset( ssl );
8519#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008520
8521 ssl->in_hslen = 0;
8522 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008523
8524 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008525
8526 ssl->out_msgtype = 0;
8527 ssl->out_msglen = 0;
8528 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8530 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008531 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008532#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008533
Hanno Becker19859472018-08-06 09:40:20 +01008534 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8535
Paul Bakker48916f92012-09-16 19:57:18 +00008536 ssl->transform_in = NULL;
8537 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008538
Hanno Becker78640902018-08-13 16:35:15 +01008539 ssl->session_in = NULL;
8540 ssl->session_out = NULL;
8541
Angus Grattond8213d02016-05-25 20:56:48 +10008542 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008543
8544#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008545 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008546#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8547 {
8548 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008549 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008550 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008552#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8553 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8556 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8559 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008560 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008561 }
8562#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008563
Paul Bakker48916f92012-09-16 19:57:18 +00008564 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008566 mbedtls_ssl_transform_free( ssl->transform );
8567 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008568 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008569 }
Paul Bakker48916f92012-09-16 19:57:18 +00008570
Paul Bakkerc0463502013-02-14 11:19:38 +01008571 if( ssl->session )
8572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008573 mbedtls_ssl_session_free( ssl->session );
8574 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008575 ssl->session = NULL;
8576 }
8577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008579 ssl->alpn_chosen = NULL;
8580#endif
8581
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008582#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008583#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008584 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008585#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008586 {
8587 mbedtls_free( ssl->cli_id );
8588 ssl->cli_id = NULL;
8589 ssl->cli_id_len = 0;
8590 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008591#endif
8592
Paul Bakker48916f92012-09-16 19:57:18 +00008593 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8594 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008595
8596 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008597}
8598
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008599/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008600 * Reset an initialized and used SSL context for re-use while retaining
8601 * all application-set variables, function pointers and data.
8602 */
8603int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8604{
8605 return( ssl_session_reset_int( ssl, 0 ) );
8606}
8607
8608/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008609 * SSL set accessors
8610 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008611void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008612{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008613 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008614}
8615
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008616void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008617{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008618 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008619}
8620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008621#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008622void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008623{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008624 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008625}
8626#endif
8627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008629void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008630{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008631 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008632}
8633#endif
8634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008636
Hanno Becker1841b0a2018-08-24 11:13:57 +01008637void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8638 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008639{
8640 ssl->disable_datagram_packing = !allow_packing;
8641}
8642
8643void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8644 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008645{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008646 conf->hs_timeout_min = min;
8647 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008648}
8649#endif
8650
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008651void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008652{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008653 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008654}
8655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008657void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008658 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008659 void *p_vrfy )
8660{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008661 conf->f_vrfy = f_vrfy;
8662 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008663}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008664#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008665
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008666void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008667 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008668 void *p_rng )
8669{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008670 conf->f_rng = f_rng;
8671 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008672}
8673
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008674void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008675 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008676 void *p_dbg )
8677{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008678 conf->f_dbg = f_dbg;
8679 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008680}
8681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008683 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008684 mbedtls_ssl_send_t *f_send,
8685 mbedtls_ssl_recv_t *f_recv,
8686 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008687{
8688 ssl->p_bio = p_bio;
8689 ssl->f_send = f_send;
8690 ssl->f_recv = f_recv;
8691 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008692}
8693
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008694#if defined(MBEDTLS_SSL_PROTO_DTLS)
8695void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8696{
8697 ssl->mtu = mtu;
8698}
8699#endif
8700
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008701void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008702{
8703 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008704}
8705
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008706void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8707 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008708 mbedtls_ssl_set_timer_t *f_set_timer,
8709 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008710{
8711 ssl->p_timer = p_timer;
8712 ssl->f_set_timer = f_set_timer;
8713 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008714
8715 /* Make sure we start with no timer running */
8716 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008717}
8718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008719#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008720void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008721 void *p_cache,
8722 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8723 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008724{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008725 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008726 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008727 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008728}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731#if defined(MBEDTLS_SSL_CLI_C)
8732int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008733{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008734 int ret;
8735
8736 if( ssl == NULL ||
8737 session == NULL ||
8738 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008739 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008742 }
8743
Hanno Becker52055ae2019-02-06 14:30:46 +00008744 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8745 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008746 return( ret );
8747
Paul Bakker0a597072012-09-25 21:55:46 +00008748 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008749
8750 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008751}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008753
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008754void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008755 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008756{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008757 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8758 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8759 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8760 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008761}
8762
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008763void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008764 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008765 int major, int minor )
8766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008767 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008768 return;
8769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008771 return;
8772
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008773 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008774}
8775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008777void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008778 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008779{
8780 conf->cert_profile = profile;
8781}
8782
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008783/* Append a new keycert entry to a (possibly empty) list */
8784static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8785 mbedtls_x509_crt *cert,
8786 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008787{
niisato8ee24222018-06-25 19:05:48 +09008788 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008789
niisato8ee24222018-06-25 19:05:48 +09008790 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8791 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008792 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008793
niisato8ee24222018-06-25 19:05:48 +09008794 new_cert->cert = cert;
8795 new_cert->key = key;
8796 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008797
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008798 /* Update head is the list was null, else add to the end */
8799 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008800 {
niisato8ee24222018-06-25 19:05:48 +09008801 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008802 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008803 else
8804 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008805 mbedtls_ssl_key_cert *cur = *head;
8806 while( cur->next != NULL )
8807 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008808 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008809 }
8810
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008811 return( 0 );
8812}
8813
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008814int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008815 mbedtls_x509_crt *own_cert,
8816 mbedtls_pk_context *pk_key )
8817{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008818 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008819}
8820
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008821void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008822 mbedtls_x509_crt *ca_chain,
8823 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008824{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008825 conf->ca_chain = ca_chain;
8826 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008827
8828#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8829 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8830 * cannot be used together. */
8831 conf->f_ca_cb = NULL;
8832 conf->p_ca_cb = NULL;
8833#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008834}
Hanno Becker5adaad92019-03-27 16:54:37 +00008835
8836#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8837void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008838 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008839 void *p_ca_cb )
8840{
8841 conf->f_ca_cb = f_ca_cb;
8842 conf->p_ca_cb = p_ca_cb;
8843
8844 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8845 * cannot be used together. */
8846 conf->ca_chain = NULL;
8847 conf->ca_crl = NULL;
8848}
8849#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008851
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008852#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8853int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8854 mbedtls_x509_crt *own_cert,
8855 mbedtls_pk_context *pk_key )
8856{
8857 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8858 own_cert, pk_key ) );
8859}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008860
8861void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8862 mbedtls_x509_crt *ca_chain,
8863 mbedtls_x509_crl *ca_crl )
8864{
8865 ssl->handshake->sni_ca_chain = ca_chain;
8866 ssl->handshake->sni_ca_crl = ca_crl;
8867}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008868
8869void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8870 int authmode )
8871{
8872 ssl->handshake->sni_authmode = authmode;
8873}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008874#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8875
Hanno Becker8927c832019-04-03 12:52:50 +01008876#if defined(MBEDTLS_X509_CRT_PARSE_C)
8877void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8878 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8879 void *p_vrfy )
8880{
8881 ssl->f_vrfy = f_vrfy;
8882 ssl->p_vrfy = p_vrfy;
8883}
8884#endif
8885
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008886#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008887/*
8888 * Set EC J-PAKE password for current handshake
8889 */
8890int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8891 const unsigned char *pw,
8892 size_t pw_len )
8893{
8894 mbedtls_ecjpake_role role;
8895
Janos Follath8eb64132016-06-03 15:40:57 +01008896 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008897 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8898
8899 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8900 role = MBEDTLS_ECJPAKE_SERVER;
8901 else
8902 role = MBEDTLS_ECJPAKE_CLIENT;
8903
8904 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8905 role,
8906 MBEDTLS_MD_SHA256,
8907 MBEDTLS_ECP_DP_SECP256R1,
8908 pw, pw_len ) );
8909}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008910#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008913
8914static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8915{
8916 /* Remove reference to existing PSK, if any. */
8917#if defined(MBEDTLS_USE_PSA_CRYPTO)
8918 if( conf->psk_opaque != 0 )
8919 {
8920 /* The maintenance of the PSK key slot is the
8921 * user's responsibility. */
8922 conf->psk_opaque = 0;
8923 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008924 /* This and the following branch should never
8925 * be taken simultaenously as we maintain the
8926 * invariant that raw and opaque PSKs are never
8927 * configured simultaneously. As a safeguard,
8928 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008929#endif /* MBEDTLS_USE_PSA_CRYPTO */
8930 if( conf->psk != NULL )
8931 {
8932 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8933
8934 mbedtls_free( conf->psk );
8935 conf->psk = NULL;
8936 conf->psk_len = 0;
8937 }
8938
8939 /* Remove reference to PSK identity, if any. */
8940 if( conf->psk_identity != NULL )
8941 {
8942 mbedtls_free( conf->psk_identity );
8943 conf->psk_identity = NULL;
8944 conf->psk_identity_len = 0;
8945 }
8946}
8947
Hanno Becker7390c712018-11-15 13:33:04 +00008948/* This function assumes that PSK identity in the SSL config is unset.
8949 * It checks that the provided identity is well-formed and attempts
8950 * to make a copy of it in the SSL config.
8951 * On failure, the PSK identity in the config remains unset. */
8952static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8953 unsigned char const *psk_identity,
8954 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008955{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008956 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008957 if( psk_identity == NULL ||
8958 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008959 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008960 {
8961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8962 }
8963
Hanno Becker7390c712018-11-15 13:33:04 +00008964 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8965 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008966 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008967
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008968 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008969 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008970
8971 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008972}
8973
Hanno Becker7390c712018-11-15 13:33:04 +00008974int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8975 const unsigned char *psk, size_t psk_len,
8976 const unsigned char *psk_identity, size_t psk_identity_len )
8977{
8978 int ret;
8979 /* Remove opaque/raw PSK + PSK Identity */
8980 ssl_conf_remove_psk( conf );
8981
8982 /* Check and set raw PSK */
8983 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8985 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8986 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8987 conf->psk_len = psk_len;
8988 memcpy( conf->psk, psk, conf->psk_len );
8989
8990 /* Check and set PSK Identity */
8991 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8992 if( ret != 0 )
8993 ssl_conf_remove_psk( conf );
8994
8995 return( ret );
8996}
8997
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008998static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8999{
9000#if defined(MBEDTLS_USE_PSA_CRYPTO)
9001 if( ssl->handshake->psk_opaque != 0 )
9002 {
9003 ssl->handshake->psk_opaque = 0;
9004 }
9005 else
9006#endif /* MBEDTLS_USE_PSA_CRYPTO */
9007 if( ssl->handshake->psk != NULL )
9008 {
9009 mbedtls_platform_zeroize( ssl->handshake->psk,
9010 ssl->handshake->psk_len );
9011 mbedtls_free( ssl->handshake->psk );
9012 ssl->handshake->psk_len = 0;
9013 }
9014}
9015
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009016int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9017 const unsigned char *psk, size_t psk_len )
9018{
9019 if( psk == NULL || ssl->handshake == NULL )
9020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9021
9022 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9024
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009025 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009026
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009027 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009028 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009029
9030 ssl->handshake->psk_len = psk_len;
9031 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9032
9033 return( 0 );
9034}
9035
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009036#if defined(MBEDTLS_USE_PSA_CRYPTO)
9037int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009038 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009039 const unsigned char *psk_identity,
9040 size_t psk_identity_len )
9041{
Hanno Becker7390c712018-11-15 13:33:04 +00009042 int ret;
9043 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009044 ssl_conf_remove_psk( conf );
9045
Hanno Becker7390c712018-11-15 13:33:04 +00009046 /* Check and set opaque PSK */
9047 if( psk_slot == 0 )
9048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009049 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009050
9051 /* Check and set PSK Identity */
9052 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9053 psk_identity_len );
9054 if( ret != 0 )
9055 ssl_conf_remove_psk( conf );
9056
9057 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009058}
9059
9060int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009061 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009062{
9063 if( psk_slot == 0 || ssl->handshake == NULL )
9064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9065
9066 ssl_remove_psk( ssl );
9067 ssl->handshake->psk_opaque = psk_slot;
9068 return( 0 );
9069}
9070#endif /* MBEDTLS_USE_PSA_CRYPTO */
9071
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009072void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009073 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009074 size_t),
9075 void *p_psk )
9076{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009077 conf->f_psk = f_psk;
9078 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009079}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009081
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009082#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009083
9084#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009085int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009086{
9087 int ret;
9088
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009089 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9090 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9091 {
9092 mbedtls_mpi_free( &conf->dhm_P );
9093 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009094 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009096
9097 return( 0 );
9098}
Hanno Becker470a8c42017-10-04 15:28:46 +01009099#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009100
Hanno Beckera90658f2017-10-04 15:29:08 +01009101int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9102 const unsigned char *dhm_P, size_t P_len,
9103 const unsigned char *dhm_G, size_t G_len )
9104{
9105 int ret;
9106
9107 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9108 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9109 {
9110 mbedtls_mpi_free( &conf->dhm_P );
9111 mbedtls_mpi_free( &conf->dhm_G );
9112 return( ret );
9113 }
9114
9115 return( 0 );
9116}
Paul Bakker5121ce52009-01-03 21:22:43 +00009117
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009118int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009119{
9120 int ret;
9121
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009122 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9123 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9124 {
9125 mbedtls_mpi_free( &conf->dhm_P );
9126 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009127 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009128 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009129
9130 return( 0 );
9131}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009132#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009133
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009134#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9135/*
9136 * Set the minimum length for Diffie-Hellman parameters
9137 */
9138void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9139 unsigned int bitlen )
9140{
9141 conf->dhm_min_bitlen = bitlen;
9142}
9143#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9144
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009145#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009146/*
9147 * Set allowed/preferred hashes for handshake signatures
9148 */
9149void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9150 const int *hashes )
9151{
9152 conf->sig_hashes = hashes;
9153}
Hanno Becker947194e2017-04-07 13:25:49 +01009154#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009155
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009156#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009157/*
9158 * Set the allowed elliptic curves
9159 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009160void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009161 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009162{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009163 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009164}
Hanno Becker947194e2017-04-07 13:25:49 +01009165#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009166
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009167#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009168int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009169{
Hanno Becker947194e2017-04-07 13:25:49 +01009170 /* Initialize to suppress unnecessary compiler warning */
9171 size_t hostname_len = 0;
9172
9173 /* Check if new hostname is valid before
9174 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009175 if( hostname != NULL )
9176 {
9177 hostname_len = strlen( hostname );
9178
9179 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9181 }
9182
9183 /* Now it's clear that we will overwrite the old hostname,
9184 * so we can free it safely */
9185
9186 if( ssl->hostname != NULL )
9187 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009188 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009189 mbedtls_free( ssl->hostname );
9190 }
9191
9192 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009193
Paul Bakker5121ce52009-01-03 21:22:43 +00009194 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009195 {
9196 ssl->hostname = NULL;
9197 }
9198 else
9199 {
9200 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009201 if( ssl->hostname == NULL )
9202 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009203
Hanno Becker947194e2017-04-07 13:25:49 +01009204 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009205
Hanno Becker947194e2017-04-07 13:25:49 +01009206 ssl->hostname[hostname_len] = '\0';
9207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009208
9209 return( 0 );
9210}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009211#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009212
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009213#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009214void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009216 const unsigned char *, size_t),
9217 void *p_sni )
9218{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009219 conf->f_sni = f_sni;
9220 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009222#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009224#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009225int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009226{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009227 size_t cur_len, tot_len;
9228 const char **p;
9229
9230 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009231 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9232 * MUST NOT be truncated."
9233 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009234 */
9235 tot_len = 0;
9236 for( p = protos; *p != NULL; p++ )
9237 {
9238 cur_len = strlen( *p );
9239 tot_len += cur_len;
9240
9241 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009243 }
9244
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009245 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009246
9247 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009248}
9249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009250const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009251{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009252 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009254#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009255
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009256void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009257{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009258 conf->max_major_ver = major;
9259 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009260}
9261
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009262void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009263{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009264 conf->min_major_ver = major;
9265 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009266}
9267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009269void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009270{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009271 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009272}
9273#endif
9274
Janos Follath088ce432017-04-10 12:42:31 +01009275#if defined(MBEDTLS_SSL_SRV_C)
9276void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9277 char cert_req_ca_list )
9278{
9279 conf->cert_req_ca_list = cert_req_ca_list;
9280}
9281#endif
9282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009283#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009284void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009285{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009286 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009287}
9288#endif
9289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009291void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009292{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009293 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009294}
9295#endif
9296
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009297#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009298void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009300 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009301}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009302#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009305int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009306{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009308 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009311 }
9312
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009313 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009314
9315 return( 0 );
9316}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009320void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009321{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009322 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009323}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009327void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009328{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009329 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009330}
9331#endif
9332
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009333void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009334{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009335 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009336}
9337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009339void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009340{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009341 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009342}
9343
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009344void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009345{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009346 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009347}
9348
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009349void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009350 const unsigned char period[8] )
9351{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009352 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009354#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009357#if defined(MBEDTLS_SSL_CLI_C)
9358void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009359{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009360 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009361}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009362#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009363
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009364#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009365void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9366 mbedtls_ssl_ticket_write_t *f_ticket_write,
9367 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9368 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009369{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009370 conf->f_ticket_write = f_ticket_write;
9371 conf->f_ticket_parse = f_ticket_parse;
9372 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009373}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009374#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009376
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009377#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9378void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9379 mbedtls_ssl_export_keys_t *f_export_keys,
9380 void *p_export_keys )
9381{
9382 conf->f_export_keys = f_export_keys;
9383 conf->p_export_keys = p_export_keys;
9384}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009385
9386void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9387 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9388 void *p_export_keys )
9389{
9390 conf->f_export_keys_ext = f_export_keys_ext;
9391 conf->p_export_keys = p_export_keys;
9392}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009393#endif
9394
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009395#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009396void mbedtls_ssl_conf_async_private_cb(
9397 mbedtls_ssl_config *conf,
9398 mbedtls_ssl_async_sign_t *f_async_sign,
9399 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9400 mbedtls_ssl_async_resume_t *f_async_resume,
9401 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009402 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009403{
9404 conf->f_async_sign_start = f_async_sign;
9405 conf->f_async_decrypt_start = f_async_decrypt;
9406 conf->f_async_resume = f_async_resume;
9407 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009408 conf->p_async_config_data = async_config_data;
9409}
9410
Gilles Peskine8f97af72018-04-26 11:46:10 +02009411void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9412{
9413 return( conf->p_async_config_data );
9414}
9415
Gilles Peskine1febfef2018-04-30 11:54:39 +02009416void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009417{
9418 if( ssl->handshake == NULL )
9419 return( NULL );
9420 else
9421 return( ssl->handshake->user_async_ctx );
9422}
9423
Gilles Peskine1febfef2018-04-30 11:54:39 +02009424void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009425 void *ctx )
9426{
9427 if( ssl->handshake != NULL )
9428 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009429}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009430#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009431
Paul Bakker5121ce52009-01-03 21:22:43 +00009432/*
9433 * SSL get accessors
9434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009436{
9437 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9438}
9439
Hanno Becker8b170a02017-10-10 11:51:19 +01009440int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9441{
9442 /*
9443 * Case A: We're currently holding back
9444 * a message for further processing.
9445 */
9446
9447 if( ssl->keep_current_message == 1 )
9448 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009450 return( 1 );
9451 }
9452
9453 /*
9454 * Case B: Further records are pending in the current datagram.
9455 */
9456
9457#if defined(MBEDTLS_SSL_PROTO_DTLS)
9458 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9459 ssl->in_left > ssl->next_record_offset )
9460 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009461 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009462 return( 1 );
9463 }
9464#endif /* MBEDTLS_SSL_PROTO_DTLS */
9465
9466 /*
9467 * Case C: A handshake message is being processed.
9468 */
9469
Hanno Becker8b170a02017-10-10 11:51:19 +01009470 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9471 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009473 return( 1 );
9474 }
9475
9476 /*
9477 * Case D: An application data message is being processed
9478 */
9479 if( ssl->in_offt != NULL )
9480 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009482 return( 1 );
9483 }
9484
9485 /*
9486 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009487 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009488 * we implement support for multiple alerts in single records.
9489 */
9490
9491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9492 return( 0 );
9493}
9494
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009495uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009496{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009497 if( ssl->session != NULL )
9498 return( ssl->session->verify_result );
9499
9500 if( ssl->session_negotiate != NULL )
9501 return( ssl->session_negotiate->verify_result );
9502
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009503 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009504}
9505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009507{
Paul Bakker926c8e42013-03-06 10:23:34 +01009508 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009509 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009512}
9513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009515{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009517 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009518 {
9519 switch( ssl->minor_ver )
9520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009522 return( "DTLSv1.0" );
9523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009525 return( "DTLSv1.2" );
9526
9527 default:
9528 return( "unknown (DTLS)" );
9529 }
9530 }
9531#endif
9532
Paul Bakker43ca69c2011-01-15 17:35:19 +00009533 switch( ssl->minor_ver )
9534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009536 return( "SSLv3.0" );
9537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009539 return( "TLSv1.0" );
9540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009542 return( "TLSv1.1" );
9543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009545 return( "TLSv1.2" );
9546
Paul Bakker43ca69c2011-01-15 17:35:19 +00009547 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009548 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009549 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009550}
9551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009553{
Hanno Becker3136ede2018-08-17 15:28:19 +01009554 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009556 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009557
Hanno Becker5903de42019-05-03 14:46:38 +01009558 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9559
Hanno Becker78640902018-08-13 16:35:15 +01009560 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009561 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563#if defined(MBEDTLS_ZLIB_SUPPORT)
9564 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9565 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009566#endif
9567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570 case MBEDTLS_MODE_GCM:
9571 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009572 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009574 transform_expansion = transform->minlen;
9575 break;
9576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009578
9579 block_size = mbedtls_cipher_get_block_size(
9580 &transform->cipher_ctx_enc );
9581
Hanno Becker3136ede2018-08-17 15:28:19 +01009582 /* Expansion due to the addition of the MAC. */
9583 transform_expansion += transform->maclen;
9584
9585 /* Expansion due to the addition of CBC padding;
9586 * Theoretically up to 256 bytes, but we never use
9587 * more than the block size of the underlying cipher. */
9588 transform_expansion += block_size;
9589
9590 /* For TLS 1.1 or higher, an explicit IV is added
9591 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009592#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9593 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009594 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009595#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009596
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009597 break;
9598
9599 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009602 }
9603
Hanno Beckera0e20d02019-05-15 14:03:01 +01009604#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009605 if( transform->out_cid_len != 0 )
9606 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009607#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009608
Hanno Becker5903de42019-05-03 14:46:38 +01009609 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009610}
9611
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009612#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9613size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9614{
9615 size_t max_len;
9616
9617 /*
9618 * Assume mfl_code is correct since it was checked when set
9619 */
Angus Grattond8213d02016-05-25 20:56:48 +10009620 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009621
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009622 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009623 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009624 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009625 {
Angus Grattond8213d02016-05-25 20:56:48 +10009626 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009627 }
9628
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009629 /* During a handshake, use the value being negotiated */
9630 if( ssl->session_negotiate != NULL &&
9631 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9632 {
9633 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9634 }
9635
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009636 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009637}
9638#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9639
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009640#if defined(MBEDTLS_SSL_PROTO_DTLS)
9641static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9642{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009643 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9644 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9645 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9646 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9647 return ( 0 );
9648
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009649 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9650 return( ssl->mtu );
9651
9652 if( ssl->mtu == 0 )
9653 return( ssl->handshake->mtu );
9654
9655 return( ssl->mtu < ssl->handshake->mtu ?
9656 ssl->mtu : ssl->handshake->mtu );
9657}
9658#endif /* MBEDTLS_SSL_PROTO_DTLS */
9659
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009660int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9661{
9662 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9663
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009664#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9665 !defined(MBEDTLS_SSL_PROTO_DTLS)
9666 (void) ssl;
9667#endif
9668
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009669#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9670 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9671
9672 if( max_len > mfl )
9673 max_len = mfl;
9674#endif
9675
9676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009677 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009678 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009679 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009680 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9681 const size_t overhead = (size_t) ret;
9682
9683 if( ret < 0 )
9684 return( ret );
9685
9686 if( mtu <= overhead )
9687 {
9688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9689 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9690 }
9691
9692 if( max_len > mtu - overhead )
9693 max_len = mtu - overhead;
9694 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009695#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009696
Hanno Becker0defedb2018-08-10 12:35:02 +01009697#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9698 !defined(MBEDTLS_SSL_PROTO_DTLS)
9699 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009700#endif
9701
9702 return( (int) max_len );
9703}
9704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009705#if defined(MBEDTLS_X509_CRT_PARSE_C)
9706const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009707{
9708 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009709 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009710
Hanno Beckere6824572019-02-07 13:18:46 +00009711#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009712 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009713#else
9714 return( NULL );
9715#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009716}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009720int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9721 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009722{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009723 if( ssl == NULL ||
9724 dst == NULL ||
9725 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009726 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009729 }
9730
Hanno Becker52055ae2019-02-06 14:30:46 +00009731 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009733#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009734
Paul Bakker5121ce52009-01-03 21:22:43 +00009735/*
Paul Bakker1961b702013-01-25 14:49:24 +01009736 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009739{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009741
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009742 if( ssl == NULL || ssl->conf == NULL )
9743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009746 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009750 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009752#endif
9753
Paul Bakker1961b702013-01-25 14:49:24 +01009754 return( ret );
9755}
9756
9757/*
9758 * Perform the SSL handshake
9759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009761{
9762 int ret = 0;
9763
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009764 if( ssl == NULL || ssl->conf == NULL )
9765 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009772
9773 if( ret != 0 )
9774 break;
9775 }
9776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009778
9779 return( ret );
9780}
9781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009782#if defined(MBEDTLS_SSL_RENEGOTIATION)
9783#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009784/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009785 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009788{
9789 int ret;
9790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009792
9793 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9795 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009796
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009797 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009798 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009800 return( ret );
9801 }
9802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009804
9805 return( 0 );
9806}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009808
9809/*
9810 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811 * - any side: calling mbedtls_ssl_renegotiate(),
9812 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9813 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009814 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009815 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009819{
9820 int ret;
9821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009823
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009824 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9825 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009826
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009827 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9828 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009830 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009832 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009833 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009834 ssl->handshake->out_msg_seq = 1;
9835 else
9836 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009837 }
9838#endif
9839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9841 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009846 return( ret );
9847 }
9848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009850
9851 return( 0 );
9852}
9853
9854/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009855 * Renegotiate current connection on client,
9856 * or request renegotiation on server
9857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009859{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009861
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009862 if( ssl == NULL || ssl->conf == NULL )
9863 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009866 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009867 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9870 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009873
9874 /* Did we already try/start sending HelloRequest? */
9875 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009876 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009877
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009878 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009879 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009882#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009883 /*
9884 * On client, either start the renegotiation process or,
9885 * if already in progress, continue the handshake
9886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009889 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9890 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009891
9892 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009895 return( ret );
9896 }
9897 }
9898 else
9899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009903 return( ret );
9904 }
9905 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009907
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009908 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009909}
9910
9911/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009912 * Check record counters and renegotiate if they're above the limit.
9913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009914static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009915{
Andres AG2196c7f2016-12-15 17:01:16 +00009916 size_t ep_len = ssl_ep_len( ssl );
9917 int in_ctr_cmp;
9918 int out_ctr_cmp;
9919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9921 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009922 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009923 {
9924 return( 0 );
9925 }
9926
Andres AG2196c7f2016-12-15 17:01:16 +00009927 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9928 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009929 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009930 ssl->conf->renego_period + ep_len, 8 - ep_len );
9931
9932 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009933 {
9934 return( 0 );
9935 }
9936
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009938 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009939}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009941
9942/*
9943 * Receive application data decrypted from the SSL layer
9944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009946{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009947 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009948 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009949
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009950 if( ssl == NULL || ssl->conf == NULL )
9951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009956 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009959 return( ret );
9960
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009961 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009962 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009963 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009964 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009965 return( ret );
9966 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009967 }
9968#endif
9969
Hanno Becker4a810fb2017-05-24 16:27:30 +01009970 /*
9971 * Check if renegotiation is necessary and/or handshake is
9972 * in process. If yes, perform/continue, and fall through
9973 * if an unexpected packet is received while the client
9974 * is waiting for the ServerHello.
9975 *
9976 * (There is no equivalent to the last condition on
9977 * the server-side as it is not treated as within
9978 * a handshake while waiting for the ClientHello
9979 * after a renegotiation request.)
9980 */
9981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009983 ret = ssl_check_ctr_renegotiate( ssl );
9984 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9985 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009987 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009988 return( ret );
9989 }
9990#endif
9991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009992 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009995 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9996 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009999 return( ret );
10000 }
10001 }
10002
Hanno Beckere41158b2017-10-23 13:30:32 +010010003 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010004 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010005 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010006 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010007 if( ssl->f_get_timer != NULL &&
10008 ssl->f_get_timer( ssl->p_timer ) == -1 )
10009 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010010 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010011 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010012
Hanno Becker327c93b2018-08-15 13:56:18 +010010013 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010014 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010015 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10016 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010017
Hanno Becker4a810fb2017-05-24 16:27:30 +010010018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10019 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010020 }
10021
10022 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010024 {
10025 /*
10026 * OpenSSL sends empty messages to randomize the IV
10027 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010028 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010030 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010031 return( 0 );
10032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010034 return( ret );
10035 }
10036 }
10037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010041
Hanno Becker4a810fb2017-05-24 16:27:30 +010010042 /*
10043 * - For client-side, expect SERVER_HELLO_REQUEST.
10044 * - For server-side, expect CLIENT_HELLO.
10045 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10046 */
10047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010049 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010051 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010054
10055 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010057 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010058 {
10059 continue;
10060 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010063 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010064#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010065
Hanno Becker4a810fb2017-05-24 16:27:30 +010010066#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010067 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010068 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010071
10072 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010073#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010074 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010075 {
10076 continue;
10077 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010078#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010079 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010080 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010081#endif /* MBEDTLS_SSL_SRV_C */
10082
Hanno Becker21df7f92017-10-17 11:03:26 +010010083#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010084 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010085 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10086 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10087 ssl->conf->allow_legacy_renegotiation ==
10088 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10089 {
10090 /*
10091 * Accept renegotiation request
10092 */
Paul Bakker48916f92012-09-16 19:57:18 +000010093
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010094 /* DTLS clients need to know renego is server-initiated */
10095#if defined(MBEDTLS_SSL_PROTO_DTLS)
10096 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10097 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10098 {
10099 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10100 }
10101#endif
10102 ret = ssl_start_renegotiation( ssl );
10103 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10104 ret != 0 )
10105 {
10106 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10107 return( ret );
10108 }
10109 }
10110 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010111#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010112 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010113 /*
10114 * Refuse renegotiation
10115 */
10116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010117 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010119#if defined(MBEDTLS_SSL_PROTO_SSL3)
10120 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010121 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010122 /* SSLv3 does not have a "no_renegotiation" warning, so
10123 we send a fatal alert and abort the connection. */
10124 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10125 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10126 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010127 }
10128 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10130#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10131 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10132 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010134 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10135 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10136 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010137 {
10138 return( ret );
10139 }
Paul Bakker48916f92012-09-16 19:57:18 +000010140 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010141 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10143 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10146 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010147 }
Paul Bakker48916f92012-09-16 19:57:18 +000010148 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010149
Hanno Becker90333da2017-10-10 11:27:13 +010010150 /* At this point, we don't know whether the renegotiation has been
10151 * completed or not. The cases to consider are the following:
10152 * 1) The renegotiation is complete. In this case, no new record
10153 * has been read yet.
10154 * 2) The renegotiation is incomplete because the client received
10155 * an application data record while awaiting the ServerHello.
10156 * 3) The renegotiation is incomplete because the client received
10157 * a non-handshake, non-application data message while awaiting
10158 * the ServerHello.
10159 * In each of these case, looping will be the proper action:
10160 * - For 1), the next iteration will read a new record and check
10161 * if it's application data.
10162 * - For 2), the loop condition isn't satisfied as application data
10163 * is present, hence continue is the same as break
10164 * - For 3), the loop condition is satisfied and read_record
10165 * will re-deliver the message that was held back by the client
10166 * when expecting the ServerHello.
10167 */
10168 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010169 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010170#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010172 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010173 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010174 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010175 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010178 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010179 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010180 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010181 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010182 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010183#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010185 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10186 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010189 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010190 }
10191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10195 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010196 }
10197
10198 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010199
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010200 /* We're going to return something now, cancel timer,
10201 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010203 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010204
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010205#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010206 /* If we requested renego but received AppData, resend HelloRequest.
10207 * Do it now, after setting in_offt, to avoid taking this branch
10208 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010210 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010212 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010213 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010215 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010216 return( ret );
10217 }
10218 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010220#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010221 }
10222
10223 n = ( len < ssl->in_msglen )
10224 ? len : ssl->in_msglen;
10225
10226 memcpy( buf, ssl->in_offt, n );
10227 ssl->in_msglen -= n;
10228
10229 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010230 {
10231 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010232 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010233 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010234 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010235 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010236 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010237 /* more data available */
10238 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010239 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010242
Paul Bakker23986e52011-04-24 08:57:21 +000010243 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010244}
10245
10246/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010247 * Send application data to be encrypted by the SSL layer, taking care of max
10248 * fragment length and buffer size.
10249 *
10250 * According to RFC 5246 Section 6.2.1:
10251 *
10252 * Zero-length fragments of Application data MAY be sent as they are
10253 * potentially useful as a traffic analysis countermeasure.
10254 *
10255 * Therefore, it is possible that the input message length is 0 and the
10256 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010257 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010258static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010259 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010260{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010261 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10262 const size_t max_len = (size_t) ret;
10263
10264 if( ret < 0 )
10265 {
10266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10267 return( ret );
10268 }
10269
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010270 if( len > max_len )
10271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010272#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010273 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010276 "maximum fragment length: %d > %d",
10277 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010278 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010279 }
10280 else
10281#endif
10282 len = max_len;
10283 }
Paul Bakker887bd502011-06-08 13:10:54 +000010284
Paul Bakker5121ce52009-01-03 21:22:43 +000010285 if( ssl->out_left != 0 )
10286 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010287 /*
10288 * The user has previously tried to send the data and
10289 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10290 * written. In this case, we expect the high-level write function
10291 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010296 return( ret );
10297 }
10298 }
Paul Bakker887bd502011-06-08 13:10:54 +000010299 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010300 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010301 /*
10302 * The user is trying to send a message the first time, so we need to
10303 * copy the data into the internal buffers and setup the data structure
10304 * to keep track of partial writes
10305 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010306 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010307 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010308 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010309
Hanno Becker67bc7c32018-08-06 11:33:50 +010010310 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010313 return( ret );
10314 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010315 }
10316
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010317 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010318}
10319
10320/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010321 * Write application data, doing 1/n-1 splitting if necessary.
10322 *
10323 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010324 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010325 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010327#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010328static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010329 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010330{
10331 int ret;
10332
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010333 if( ssl->conf->cbc_record_splitting ==
10334 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010335 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010336 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10337 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10338 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010339 {
10340 return( ssl_write_real( ssl, buf, len ) );
10341 }
10342
10343 if( ssl->split_done == 0 )
10344 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010345 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010346 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010347 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010348 }
10349
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010350 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10351 return( ret );
10352 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010353
10354 return( ret + 1 );
10355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010357
10358/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010359 * Write application data (public-facing wrapper)
10360 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010361int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010362{
10363 int ret;
10364
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010366
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010367 if( ssl == NULL || ssl->conf == NULL )
10368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10369
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010370#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010371 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10372 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010373 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010374 return( ret );
10375 }
10376#endif
10377
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010378 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010379 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010380 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010381 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010383 return( ret );
10384 }
10385 }
10386
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010387#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010388 ret = ssl_write_split( ssl, buf, len );
10389#else
10390 ret = ssl_write_real( ssl, buf, len );
10391#endif
10392
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010394
10395 return( ret );
10396}
10397
10398/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010399 * Notify the peer that the connection is being closed
10400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010401int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010402{
10403 int ret;
10404
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010405 if( ssl == NULL || ssl->conf == NULL )
10406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010409
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010410 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010413 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010415 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10416 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10417 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010420 return( ret );
10421 }
10422 }
10423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010425
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010426 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010427}
10428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010429void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010430{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010431 if( transform == NULL )
10432 return;
10433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010435 deflateEnd( &transform->ctx_deflate );
10436 inflateEnd( &transform->ctx_inflate );
10437#endif
10438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10440 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010441
Hanno Beckerd56ed242018-01-03 15:32:51 +000010442#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010443 mbedtls_md_free( &transform->md_ctx_enc );
10444 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010445#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010446
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010447 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010448}
10449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450#if defined(MBEDTLS_X509_CRT_PARSE_C)
10451static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010452{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010453 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010454
10455 while( cur != NULL )
10456 {
10457 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010458 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010459 cur = next;
10460 }
10461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010462#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010463
Hanno Becker0271f962018-08-16 13:23:47 +010010464#if defined(MBEDTLS_SSL_PROTO_DTLS)
10465
10466static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10467{
10468 unsigned offset;
10469 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10470
10471 if( hs == NULL )
10472 return;
10473
Hanno Becker283f5ef2018-08-24 09:34:47 +010010474 ssl_free_buffered_record( ssl );
10475
Hanno Becker0271f962018-08-16 13:23:47 +010010476 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010477 ssl_buffering_free_slot( ssl, offset );
10478}
10479
10480static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10481 uint8_t slot )
10482{
10483 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10484 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010485
10486 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10487 return;
10488
Hanno Beckere605b192018-08-21 15:59:07 +010010489 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010490 {
Hanno Beckere605b192018-08-21 15:59:07 +010010491 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010492 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010493 mbedtls_free( hs_buf->data );
10494 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010495 }
10496}
10497
10498#endif /* MBEDTLS_SSL_PROTO_DTLS */
10499
Gilles Peskine9b562d52018-04-25 20:32:43 +020010500void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010501{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010502 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10503
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010504 if( handshake == NULL )
10505 return;
10506
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010507#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10508 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10509 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010510 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010511 handshake->async_in_progress = 0;
10512 }
10513#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10514
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010515#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10516 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10517 mbedtls_md5_free( &handshake->fin_md5 );
10518 mbedtls_sha1_free( &handshake->fin_sha1 );
10519#endif
10520#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10521#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010522#if defined(MBEDTLS_USE_PSA_CRYPTO)
10523 psa_hash_abort( &handshake->fin_sha256_psa );
10524#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010525 mbedtls_sha256_free( &handshake->fin_sha256 );
10526#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010527#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010528#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010529#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010530 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010531#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010532 mbedtls_sha512_free( &handshake->fin_sha512 );
10533#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010534#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010535#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010537#if defined(MBEDTLS_DHM_C)
10538 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010540#if defined(MBEDTLS_ECDH_C)
10541 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010542#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010543#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010544 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010545#if defined(MBEDTLS_SSL_CLI_C)
10546 mbedtls_free( handshake->ecjpake_cache );
10547 handshake->ecjpake_cache = NULL;
10548 handshake->ecjpake_cache_len = 0;
10549#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010550#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010551
Janos Follath4ae5c292016-02-10 11:27:43 +000010552#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10553 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010554 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010555 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010556#endif
10557
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010558#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10559 if( handshake->psk != NULL )
10560 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010561 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010562 mbedtls_free( handshake->psk );
10563 }
10564#endif
10565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010566#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10567 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010568 /*
10569 * Free only the linked list wrapper, not the keys themselves
10570 * since the belong to the SNI callback
10571 */
10572 if( handshake->sni_key_cert != NULL )
10573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010574 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010575
10576 while( cur != NULL )
10577 {
10578 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010579 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010580 cur = next;
10581 }
10582 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010583#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010584
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010585#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010586 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010587 if( handshake->ecrs_peer_cert != NULL )
10588 {
10589 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10590 mbedtls_free( handshake->ecrs_peer_cert );
10591 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010592#endif
10593
Hanno Becker75173122019-02-06 16:18:31 +000010594#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10595 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10596 mbedtls_pk_free( &handshake->peer_pubkey );
10597#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599#if defined(MBEDTLS_SSL_PROTO_DTLS)
10600 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010601 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010602 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010603#endif
10604
Hanno Becker4a63ed42019-01-08 11:39:35 +000010605#if defined(MBEDTLS_ECDH_C) && \
10606 defined(MBEDTLS_USE_PSA_CRYPTO)
10607 psa_destroy_key( handshake->ecdh_psa_privkey );
10608#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10609
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010610 mbedtls_platform_zeroize( handshake,
10611 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010612}
10613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010614void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010615{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010616 if( session == NULL )
10617 return;
10618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010619#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010620 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010621#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010622
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010623#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010625#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010626
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010627 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010628}
10629
Paul Bakker5121ce52009-01-03 21:22:43 +000010630/*
10631 * Free an SSL context
10632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010633void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010634{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010635 if( ssl == NULL )
10636 return;
10637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010639
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010640 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010641 {
Angus Grattond8213d02016-05-25 20:56:48 +100010642 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010643 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010644 }
10645
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010646 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010647 {
Angus Grattond8213d02016-05-25 20:56:48 +100010648 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010650 }
10651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010653 if( ssl->compress_buf != NULL )
10654 {
Angus Grattond8213d02016-05-25 20:56:48 +100010655 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010657 }
10658#endif
10659
Paul Bakker48916f92012-09-16 19:57:18 +000010660 if( ssl->transform )
10661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010662 mbedtls_ssl_transform_free( ssl->transform );
10663 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010664 }
10665
10666 if( ssl->handshake )
10667 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010668 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010669 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10670 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010672 mbedtls_free( ssl->handshake );
10673 mbedtls_free( ssl->transform_negotiate );
10674 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010675 }
10676
Paul Bakkerc0463502013-02-14 11:19:38 +010010677 if( ssl->session )
10678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679 mbedtls_ssl_session_free( ssl->session );
10680 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010681 }
10682
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010683#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010684 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010685 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010686 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010688 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010689#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010691#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10692 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10695 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010696 }
10697#endif
10698
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010699#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010700 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010701#endif
10702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010704
Paul Bakker86f04f42013-02-14 11:20:09 +010010705 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010706 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010707}
10708
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010709/*
10710 * Initialze mbedtls_ssl_config
10711 */
10712void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10713{
10714 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10715}
10716
Simon Butcherc97b6972015-12-27 23:48:17 +000010717#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010718static int ssl_preset_default_hashes[] = {
10719#if defined(MBEDTLS_SHA512_C)
10720 MBEDTLS_MD_SHA512,
10721 MBEDTLS_MD_SHA384,
10722#endif
10723#if defined(MBEDTLS_SHA256_C)
10724 MBEDTLS_MD_SHA256,
10725 MBEDTLS_MD_SHA224,
10726#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010727#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010728 MBEDTLS_MD_SHA1,
10729#endif
10730 MBEDTLS_MD_NONE
10731};
Simon Butcherc97b6972015-12-27 23:48:17 +000010732#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010733
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010734static int ssl_preset_suiteb_ciphersuites[] = {
10735 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10736 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10737 0
10738};
10739
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010740#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010741static int ssl_preset_suiteb_hashes[] = {
10742 MBEDTLS_MD_SHA256,
10743 MBEDTLS_MD_SHA384,
10744 MBEDTLS_MD_NONE
10745};
10746#endif
10747
10748#if defined(MBEDTLS_ECP_C)
10749static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010750#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010751 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010752#endif
10753#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010754 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010755#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010756 MBEDTLS_ECP_DP_NONE
10757};
10758#endif
10759
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010760/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010761 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010762 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010763int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010764 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010765{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010766#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010767 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010768#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010769
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010770 /* Use the functions here so that they are covered in tests,
10771 * but otherwise access member directly for efficiency */
10772 mbedtls_ssl_conf_endpoint( conf, endpoint );
10773 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010774
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010775 /*
10776 * Things that are common to all presets
10777 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010778#if defined(MBEDTLS_SSL_CLI_C)
10779 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10780 {
10781 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10782#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10783 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10784#endif
10785 }
10786#endif
10787
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010788#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010789 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010790#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010791
10792#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10793 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10794#endif
10795
10796#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10797 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10798#endif
10799
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010800#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10801 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10802#endif
10803
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010804#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010805 conf->f_cookie_write = ssl_cookie_write_dummy;
10806 conf->f_cookie_check = ssl_cookie_check_dummy;
10807#endif
10808
10809#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10810 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10811#endif
10812
Janos Follath088ce432017-04-10 12:42:31 +010010813#if defined(MBEDTLS_SSL_SRV_C)
10814 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10815#endif
10816
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010817#if defined(MBEDTLS_SSL_PROTO_DTLS)
10818 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10819 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10820#endif
10821
10822#if defined(MBEDTLS_SSL_RENEGOTIATION)
10823 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010824 memset( conf->renego_period, 0x00, 2 );
10825 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010826#endif
10827
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010828#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10829 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10830 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010831 const unsigned char dhm_p[] =
10832 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10833 const unsigned char dhm_g[] =
10834 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10835
Hanno Beckera90658f2017-10-04 15:29:08 +010010836 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10837 dhm_p, sizeof( dhm_p ),
10838 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010839 {
10840 return( ret );
10841 }
10842 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010843#endif
10844
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010845 /*
10846 * Preset-specific defaults
10847 */
10848 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010849 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010850 /*
10851 * NSA Suite B
10852 */
10853 case MBEDTLS_SSL_PRESET_SUITEB:
10854 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10855 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10856 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10857 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10858
10859 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10860 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10861 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10862 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10863 ssl_preset_suiteb_ciphersuites;
10864
10865#if defined(MBEDTLS_X509_CRT_PARSE_C)
10866 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010867#endif
10868
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010869#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010870 conf->sig_hashes = ssl_preset_suiteb_hashes;
10871#endif
10872
10873#if defined(MBEDTLS_ECP_C)
10874 conf->curve_list = ssl_preset_suiteb_curves;
10875#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010876 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010877
10878 /*
10879 * Default
10880 */
10881 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010882 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10883 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10884 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10885 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10886 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10887 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10888 MBEDTLS_SSL_MIN_MINOR_VERSION :
10889 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010890 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10891 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10892
10893#if defined(MBEDTLS_SSL_PROTO_DTLS)
10894 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10895 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10896#endif
10897
10898 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10899 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10900 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10901 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10902 mbedtls_ssl_list_ciphersuites();
10903
10904#if defined(MBEDTLS_X509_CRT_PARSE_C)
10905 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10906#endif
10907
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010908#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010909 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010910#endif
10911
10912#if defined(MBEDTLS_ECP_C)
10913 conf->curve_list = mbedtls_ecp_grp_id_list();
10914#endif
10915
10916#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10917 conf->dhm_min_bitlen = 1024;
10918#endif
10919 }
10920
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010921 return( 0 );
10922}
10923
10924/*
10925 * Free mbedtls_ssl_config
10926 */
10927void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10928{
10929#if defined(MBEDTLS_DHM_C)
10930 mbedtls_mpi_free( &conf->dhm_P );
10931 mbedtls_mpi_free( &conf->dhm_G );
10932#endif
10933
10934#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10935 if( conf->psk != NULL )
10936 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010937 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010938 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010939 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010940 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010941 }
10942
10943 if( conf->psk_identity != NULL )
10944 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010945 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010946 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010947 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010948 conf->psk_identity_len = 0;
10949 }
10950#endif
10951
10952#if defined(MBEDTLS_X509_CRT_PARSE_C)
10953 ssl_key_cert_free( conf->key_cert );
10954#endif
10955
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010956 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010957}
10958
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010959#if defined(MBEDTLS_PK_C) && \
10960 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010961/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010962 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010964unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010965{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010966#if defined(MBEDTLS_RSA_C)
10967 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10968 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010969#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010970#if defined(MBEDTLS_ECDSA_C)
10971 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10972 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010973#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010974 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010975}
10976
Hanno Becker7e5437a2017-04-28 17:15:26 +010010977unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10978{
10979 switch( type ) {
10980 case MBEDTLS_PK_RSA:
10981 return( MBEDTLS_SSL_SIG_RSA );
10982 case MBEDTLS_PK_ECDSA:
10983 case MBEDTLS_PK_ECKEY:
10984 return( MBEDTLS_SSL_SIG_ECDSA );
10985 default:
10986 return( MBEDTLS_SSL_SIG_ANON );
10987 }
10988}
10989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010990mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010991{
10992 switch( sig )
10993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010994#if defined(MBEDTLS_RSA_C)
10995 case MBEDTLS_SSL_SIG_RSA:
10996 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010997#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010998#if defined(MBEDTLS_ECDSA_C)
10999 case MBEDTLS_SSL_SIG_ECDSA:
11000 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011001#endif
11002 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011003 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011004 }
11005}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011006#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011007
Hanno Becker7e5437a2017-04-28 17:15:26 +010011008#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11009 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11010
11011/* Find an entry in a signature-hash set matching a given hash algorithm. */
11012mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11013 mbedtls_pk_type_t sig_alg )
11014{
11015 switch( sig_alg )
11016 {
11017 case MBEDTLS_PK_RSA:
11018 return( set->rsa );
11019 case MBEDTLS_PK_ECDSA:
11020 return( set->ecdsa );
11021 default:
11022 return( MBEDTLS_MD_NONE );
11023 }
11024}
11025
11026/* Add a signature-hash-pair to a signature-hash set */
11027void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11028 mbedtls_pk_type_t sig_alg,
11029 mbedtls_md_type_t md_alg )
11030{
11031 switch( sig_alg )
11032 {
11033 case MBEDTLS_PK_RSA:
11034 if( set->rsa == MBEDTLS_MD_NONE )
11035 set->rsa = md_alg;
11036 break;
11037
11038 case MBEDTLS_PK_ECDSA:
11039 if( set->ecdsa == MBEDTLS_MD_NONE )
11040 set->ecdsa = md_alg;
11041 break;
11042
11043 default:
11044 break;
11045 }
11046}
11047
11048/* Allow exactly one hash algorithm for each signature. */
11049void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11050 mbedtls_md_type_t md_alg )
11051{
11052 set->rsa = md_alg;
11053 set->ecdsa = md_alg;
11054}
11055
11056#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11057 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11058
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011059/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011060 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011062mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011063{
11064 switch( hash )
11065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011066#if defined(MBEDTLS_MD5_C)
11067 case MBEDTLS_SSL_HASH_MD5:
11068 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011069#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011070#if defined(MBEDTLS_SHA1_C)
11071 case MBEDTLS_SSL_HASH_SHA1:
11072 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011073#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011074#if defined(MBEDTLS_SHA256_C)
11075 case MBEDTLS_SSL_HASH_SHA224:
11076 return( MBEDTLS_MD_SHA224 );
11077 case MBEDTLS_SSL_HASH_SHA256:
11078 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011080#if defined(MBEDTLS_SHA512_C)
11081 case MBEDTLS_SSL_HASH_SHA384:
11082 return( MBEDTLS_MD_SHA384 );
11083 case MBEDTLS_SSL_HASH_SHA512:
11084 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011085#endif
11086 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011087 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011088 }
11089}
11090
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011091/*
11092 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11093 */
11094unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11095{
11096 switch( md )
11097 {
11098#if defined(MBEDTLS_MD5_C)
11099 case MBEDTLS_MD_MD5:
11100 return( MBEDTLS_SSL_HASH_MD5 );
11101#endif
11102#if defined(MBEDTLS_SHA1_C)
11103 case MBEDTLS_MD_SHA1:
11104 return( MBEDTLS_SSL_HASH_SHA1 );
11105#endif
11106#if defined(MBEDTLS_SHA256_C)
11107 case MBEDTLS_MD_SHA224:
11108 return( MBEDTLS_SSL_HASH_SHA224 );
11109 case MBEDTLS_MD_SHA256:
11110 return( MBEDTLS_SSL_HASH_SHA256 );
11111#endif
11112#if defined(MBEDTLS_SHA512_C)
11113 case MBEDTLS_MD_SHA384:
11114 return( MBEDTLS_SSL_HASH_SHA384 );
11115 case MBEDTLS_MD_SHA512:
11116 return( MBEDTLS_SSL_HASH_SHA512 );
11117#endif
11118 default:
11119 return( MBEDTLS_SSL_HASH_NONE );
11120 }
11121}
11122
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011123#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011124/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011125 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011126 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011127 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011128int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011130 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011131
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011132 if( ssl->conf->curve_list == NULL )
11133 return( -1 );
11134
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011135 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011136 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011137 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011138
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011139 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011140}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011141#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011142
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011143#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011144/*
11145 * Check if a hash proposed by the peer is in our list.
11146 * Return 0 if we're willing to use it, -1 otherwise.
11147 */
11148int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11149 mbedtls_md_type_t md )
11150{
11151 const int *cur;
11152
11153 if( ssl->conf->sig_hashes == NULL )
11154 return( -1 );
11155
11156 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11157 if( *cur == (int) md )
11158 return( 0 );
11159
11160 return( -1 );
11161}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011162#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011164#if defined(MBEDTLS_X509_CRT_PARSE_C)
11165int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11166 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011167 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011168 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011169{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011170 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011171#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011172 int usage = 0;
11173#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011174#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011175 const char *ext_oid;
11176 size_t ext_len;
11177#endif
11178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011179#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11180 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011181 ((void) cert);
11182 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011183 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011184#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011186#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11187 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011188 {
11189 /* Server part of the key exchange */
11190 switch( ciphersuite->key_exchange )
11191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011192 case MBEDTLS_KEY_EXCHANGE_RSA:
11193 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011194 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011195 break;
11196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011197 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11198 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11199 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11200 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011201 break;
11202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011203 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11204 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011205 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011206 break;
11207
11208 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011209 case MBEDTLS_KEY_EXCHANGE_NONE:
11210 case MBEDTLS_KEY_EXCHANGE_PSK:
11211 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11212 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011213 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011214 usage = 0;
11215 }
11216 }
11217 else
11218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011219 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11220 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011221 }
11222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011223 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011224 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011225 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011226 ret = -1;
11227 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011228#else
11229 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011230#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011232#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11233 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011235 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11236 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011237 }
11238 else
11239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011240 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11241 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011242 }
11243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011244 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011245 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011246 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011247 ret = -1;
11248 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011249#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011250
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011251 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011253#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011254
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011255/*
11256 * Convert version numbers to/from wire format
11257 * and, for DTLS, to/from TLS equivalent.
11258 *
11259 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011260 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011261 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11262 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011264void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011265 unsigned char ver[2] )
11266{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011267#if defined(MBEDTLS_SSL_PROTO_DTLS)
11268 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011270 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011271 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11272
11273 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11274 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11275 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011276 else
11277#else
11278 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011279#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011280 {
11281 ver[0] = (unsigned char) major;
11282 ver[1] = (unsigned char) minor;
11283 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011284}
11285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011286void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011287 const unsigned char ver[2] )
11288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011289#if defined(MBEDTLS_SSL_PROTO_DTLS)
11290 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011291 {
11292 *major = 255 - ver[0] + 2;
11293 *minor = 255 - ver[1] + 1;
11294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011295 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011296 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11297 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011298 else
11299#else
11300 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011301#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011302 {
11303 *major = ver[0];
11304 *minor = ver[1];
11305 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011306}
11307
Simon Butcher99000142016-10-13 17:21:01 +010011308int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11309{
11310#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11311 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11312 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11313
11314 switch( md )
11315 {
11316#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11317#if defined(MBEDTLS_MD5_C)
11318 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011319 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011320#endif
11321#if defined(MBEDTLS_SHA1_C)
11322 case MBEDTLS_SSL_HASH_SHA1:
11323 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11324 break;
11325#endif
11326#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11327#if defined(MBEDTLS_SHA512_C)
11328 case MBEDTLS_SSL_HASH_SHA384:
11329 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11330 break;
11331#endif
11332#if defined(MBEDTLS_SHA256_C)
11333 case MBEDTLS_SSL_HASH_SHA256:
11334 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11335 break;
11336#endif
11337 default:
11338 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11339 }
11340
11341 return 0;
11342#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11343 (void) ssl;
11344 (void) md;
11345
11346 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11347#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11348}
11349
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011350#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11351 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11352int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11353 unsigned char *output,
11354 unsigned char *data, size_t data_len )
11355{
11356 int ret = 0;
11357 mbedtls_md5_context mbedtls_md5;
11358 mbedtls_sha1_context mbedtls_sha1;
11359
11360 mbedtls_md5_init( &mbedtls_md5 );
11361 mbedtls_sha1_init( &mbedtls_sha1 );
11362
11363 /*
11364 * digitally-signed struct {
11365 * opaque md5_hash[16];
11366 * opaque sha_hash[20];
11367 * };
11368 *
11369 * md5_hash
11370 * MD5(ClientHello.random + ServerHello.random
11371 * + ServerParams);
11372 * sha_hash
11373 * SHA(ClientHello.random + ServerHello.random
11374 * + ServerParams);
11375 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011376 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011377 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011379 goto exit;
11380 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011381 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011382 ssl->handshake->randbytes, 64 ) ) != 0 )
11383 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011385 goto exit;
11386 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011387 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011388 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011390 goto exit;
11391 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011392 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011393 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011394 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011395 goto exit;
11396 }
11397
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011398 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011399 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011401 goto exit;
11402 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011403 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011404 ssl->handshake->randbytes, 64 ) ) != 0 )
11405 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011407 goto exit;
11408 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011409 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011410 data_len ) ) != 0 )
11411 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011413 goto exit;
11414 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011415 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011416 output + 16 ) ) != 0 )
11417 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011419 goto exit;
11420 }
11421
11422exit:
11423 mbedtls_md5_free( &mbedtls_md5 );
11424 mbedtls_sha1_free( &mbedtls_sha1 );
11425
11426 if( ret != 0 )
11427 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11428 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11429
11430 return( ret );
11431
11432}
11433#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11434 MBEDTLS_SSL_PROTO_TLS1_1 */
11435
11436#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11437 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011438
11439#if defined(MBEDTLS_USE_PSA_CRYPTO)
11440int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11441 unsigned char *hash, size_t *hashlen,
11442 unsigned char *data, size_t data_len,
11443 mbedtls_md_type_t md_alg )
11444{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011445 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011446 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011447 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11448
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011450
11451 if( ( status = psa_hash_setup( &hash_operation,
11452 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011453 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011454 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011455 goto exit;
11456 }
11457
Andrzej Kurek814feff2019-01-14 04:35:19 -050011458 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11459 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011460 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011461 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011462 goto exit;
11463 }
11464
Andrzej Kurek814feff2019-01-14 04:35:19 -050011465 if( ( status = psa_hash_update( &hash_operation,
11466 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011467 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011468 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011469 goto exit;
11470 }
11471
Andrzej Kurek814feff2019-01-14 04:35:19 -050011472 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11473 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011474 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011475 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011476 goto exit;
11477 }
11478
11479exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011480 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011481 {
11482 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11483 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011484 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011485 {
11486 case PSA_ERROR_NOT_SUPPORTED:
11487 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011488 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011489 case PSA_ERROR_BUFFER_TOO_SMALL:
11490 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11491 case PSA_ERROR_INSUFFICIENT_MEMORY:
11492 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11493 default:
11494 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11495 }
11496 }
11497 return( 0 );
11498}
11499
11500#else
11501
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011502int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011503 unsigned char *hash, size_t *hashlen,
11504 unsigned char *data, size_t data_len,
11505 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011506{
11507 int ret = 0;
11508 mbedtls_md_context_t ctx;
11509 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011510 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011511
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011513
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011514 mbedtls_md_init( &ctx );
11515
11516 /*
11517 * digitally-signed struct {
11518 * opaque client_random[32];
11519 * opaque server_random[32];
11520 * ServerDHParams params;
11521 * };
11522 */
11523 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11524 {
11525 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11526 goto exit;
11527 }
11528 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11529 {
11530 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11531 goto exit;
11532 }
11533 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11534 {
11535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11536 goto exit;
11537 }
11538 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11539 {
11540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11541 goto exit;
11542 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011543 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011544 {
11545 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11546 goto exit;
11547 }
11548
11549exit:
11550 mbedtls_md_free( &ctx );
11551
11552 if( ret != 0 )
11553 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11554 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11555
11556 return( ret );
11557}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011558#endif /* MBEDTLS_USE_PSA_CRYPTO */
11559
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011560#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11561 MBEDTLS_SSL_PROTO_TLS1_2 */
11562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011563#endif /* MBEDTLS_SSL_TLS_C */