blob: 0db1e9d464822e6f8c8c3cf88a786889d591b057 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Hanno Becker2a43f6f2018-08-10 11:12:52 +010062static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010063static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010064
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010070 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010071#else
72 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010073#endif
74 return( 0 );
75}
76
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077/*
78 * Start a timer.
79 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020083 if( ssl->f_set_timer == NULL )
84 return;
85
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
87 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088}
89
90/*
91 * Return -1 is timer is expired, 0 if it isn't.
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020094{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020095 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020096 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020097
98 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020099 {
100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200102 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103
104 return( 0 );
105}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200106
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100107static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
108 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100109static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100110
Hanno Beckercfe45792019-07-03 16:13:00 +0100111#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +0100112static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
113 unsigned char *buf,
114 size_t len,
115 mbedtls_record *rec );
116
Hanno Beckercfe45792019-07-03 16:13:00 +0100117int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
118 unsigned char *buf,
119 size_t buflen )
120{
Hanno Becker54229812019-07-12 14:40:00 +0100121 int ret = 0;
122 mbedtls_record rec;
123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
124 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
125
126 /* We don't support record checking in TLS because
127 * (a) there doesn't seem to be a usecase for it, and
128 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
129 * and we'd need to backup the transform here.
130 */
131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
132 {
133 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
134 goto exit;
135 }
136#if defined(MBEDTLS_SSL_PROTO_DTLS)
137 else
138 {
139 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
140 if( ret != 0 )
141 {
142 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
143 goto exit;
144 }
145
146 if( ssl->transform_in != NULL )
147 {
148 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
149 if( ret != 0 )
150 {
151 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
152 goto exit;
153 }
154 }
155 }
156#endif /* MBEDTLS_SSL_PROTO_DTLS */
157
158exit:
159 /* On success, we have decrypted the buffer in-place, so make
160 * sure we don't leak any plaintext data. */
161 mbedtls_platform_zeroize( buf, buflen );
162
163 /* For the purpose of this API, treat messages with unexpected CID
164 * as well as such from future epochs as unexpected. */
165 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
166 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
167 {
168 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
169 }
170
171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
172 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100173}
174#endif /* MBEDTLS_SSL_RECORD_CHECKING */
175
Hanno Becker67bc7c32018-08-06 11:33:50 +0100176#define SSL_DONT_FORCE_FLUSH 0
177#define SSL_FORCE_FLUSH 1
178
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200179#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100180
Hanno Beckera0e20d02019-05-15 14:03:01 +0100181#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100182/* Top-level Connection ID API */
183
Hanno Becker8367ccc2019-05-14 11:30:10 +0100184int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
185 size_t len,
186 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100187{
188 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
190
Hanno Becker611ac772019-05-14 11:45:26 +0100191 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
192 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
193 {
194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
195 }
196
197 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100198 conf->cid_len = len;
199 return( 0 );
200}
201
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100202int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
203 int enable,
204 unsigned char const *own_cid,
205 size_t own_cid_len )
206{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100207 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
209
Hanno Beckerca092242019-04-25 16:01:49 +0100210 ssl->negotiate_cid = enable;
211 if( enable == MBEDTLS_SSL_CID_DISABLED )
212 {
213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
214 return( 0 );
215 }
216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100217 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100218
Hanno Beckerad4a1372019-05-03 13:06:44 +0100219 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100220 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
222 (unsigned) own_cid_len,
223 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
225 }
226
227 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100228 /* Truncation is not an issue here because
229 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
230 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100231
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100232 return( 0 );
233}
234
235int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
236 int *enabled,
237 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
238 size_t *peer_cid_len )
239{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100240 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100241
Hanno Becker76a79ab2019-05-03 14:38:32 +0100242 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
243 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
244 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100246 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100247
Hanno Beckerc5f24222019-05-03 12:54:52 +0100248 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
249 * were used, but client and server requested the empty CID.
250 * This is indistinguishable from not using the CID extension
251 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100252 if( ssl->transform_in->in_cid_len == 0 &&
253 ssl->transform_in->out_cid_len == 0 )
254 {
255 return( 0 );
256 }
257
Hanno Becker615ef172019-05-22 16:50:35 +0100258 if( peer_cid_len != NULL )
259 {
260 *peer_cid_len = ssl->transform_in->out_cid_len;
261 if( peer_cid != NULL )
262 {
263 memcpy( peer_cid, ssl->transform_in->out_cid,
264 ssl->transform_in->out_cid_len );
265 }
266 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100267
268 *enabled = MBEDTLS_SSL_CID_ENABLED;
269
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100270 return( 0 );
271}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100272#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100273
Hanno Beckerd5847772018-08-28 10:09:23 +0100274/* Forward declarations for functions related to message buffering. */
275static void ssl_buffering_free( mbedtls_ssl_context *ssl );
276static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
277 uint8_t slot );
278static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
279static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
280static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
281static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100282static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
283 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100284static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100285
Hanno Beckera67dee22018-08-22 10:05:20 +0100286static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100287static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100288{
Hanno Becker11682cc2018-08-22 14:41:02 +0100289 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100290
291 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100292 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100293
294 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
295}
296
Hanno Becker67bc7c32018-08-06 11:33:50 +0100297static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
298{
Hanno Becker11682cc2018-08-22 14:41:02 +0100299 size_t const bytes_written = ssl->out_left;
300 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100301
302 /* Double-check that the write-index hasn't gone
303 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100304 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100305 {
306 /* Should never happen... */
307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
308 }
309
310 return( (int) ( mtu - bytes_written ) );
311}
312
313static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
314{
315 int ret;
316 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400317 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100318
319#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
320 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
321
322 if( max_len > mfl )
323 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100324
325 /* By the standard (RFC 6066 Sect. 4), the MFL extension
326 * only limits the maximum record payload size, so in theory
327 * we would be allowed to pack multiple records of payload size
328 * MFL into a single datagram. However, this would mean that there's
329 * no way to explicitly communicate MTU restrictions to the peer.
330 *
331 * The following reduction of max_len makes sure that we never
332 * write datagrams larger than MFL + Record Expansion Overhead.
333 */
334 if( max_len <= ssl->out_left )
335 return( 0 );
336
337 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100338#endif
339
340 ret = ssl_get_remaining_space_in_datagram( ssl );
341 if( ret < 0 )
342 return( ret );
343 remaining = (size_t) ret;
344
345 ret = mbedtls_ssl_get_record_expansion( ssl );
346 if( ret < 0 )
347 return( ret );
348 expansion = (size_t) ret;
349
350 if( remaining <= expansion )
351 return( 0 );
352
353 remaining -= expansion;
354 if( remaining >= max_len )
355 remaining = max_len;
356
357 return( (int) remaining );
358}
359
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200360/*
361 * Double the retransmit timeout value, within the allowed range,
362 * returning -1 if the maximum value has already been reached.
363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200365{
366 uint32_t new_timeout;
367
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200368 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200369 return( -1 );
370
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200371 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
372 * in the following way: after the initial transmission and a first
373 * retransmission, back off to a temporary estimated MTU of 508 bytes.
374 * This value is guaranteed to be deliverable (if not guaranteed to be
375 * delivered) of any compliant IPv4 (and IPv6) network, and should work
376 * on most non-IP stacks too. */
377 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400378 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200379 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
381 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200382
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200383 new_timeout = 2 * ssl->handshake->retransmit_timeout;
384
385 /* Avoid arithmetic overflow and range overflow */
386 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200387 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200388 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200389 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200390 }
391
392 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200394 ssl->handshake->retransmit_timeout ) );
395
396 return( 0 );
397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200400{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200401 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200403 ssl->handshake->retransmit_timeout ) );
404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200408/*
409 * Convert max_fragment_length codes to length.
410 * RFC 6066 says:
411 * enum{
412 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
413 * } MaxFragmentLength;
414 * and we add 0 -> extension unused
415 */
Angus Grattond8213d02016-05-25 20:56:48 +1000416static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200417{
Angus Grattond8213d02016-05-25 20:56:48 +1000418 switch( mfl )
419 {
420 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
421 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
422 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
423 return 512;
424 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
425 return 1024;
426 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
427 return 2048;
428 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
429 return 4096;
430 default:
431 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
432 }
433}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200435
Hanno Becker52055ae2019-02-06 14:30:46 +0000436int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
437 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200438{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ssl_session_free( dst );
440 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000443
444#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200445 if( src->peer_cert != NULL )
446 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200447 int ret;
448
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200449 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200450 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200451 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200456 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200459 dst->peer_cert = NULL;
460 return( ret );
461 }
462 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000463#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000464 if( src->peer_cert_digest != NULL )
465 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000466 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000467 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000468 if( dst->peer_cert_digest == NULL )
469 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
470
471 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
472 src->peer_cert_digest_len );
473 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000474 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000475 }
476#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200479
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200480#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200481 if( src->ticket != NULL )
482 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200483 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200484 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200485 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200486
487 memcpy( dst->ticket, src->ticket, src->ticket_len );
488 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200489#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200490
491 return( 0 );
492}
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
495int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200496 const unsigned char *key_enc, const unsigned char *key_dec,
497 size_t keylen,
498 const unsigned char *iv_enc, const unsigned char *iv_dec,
499 size_t ivlen,
500 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200501 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
503int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
504int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
505int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
506int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
507#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000508
Paul Bakker5121ce52009-01-03 21:22:43 +0000509/*
510 * Key material generation
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200513static int ssl3_prf( const unsigned char *secret, size_t slen,
514 const char *label,
515 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000516 unsigned char *dstbuf, size_t dlen )
517{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100518 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000519 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200520 mbedtls_md5_context md5;
521 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000522 unsigned char padding[16];
523 unsigned char sha1sum[20];
524 ((void)label);
525
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200526 mbedtls_md5_init( &md5 );
527 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200528
Paul Bakker5f70b252012-09-13 14:23:06 +0000529 /*
530 * SSLv3:
531 * block =
532 * MD5( secret + SHA1( 'A' + secret + random ) ) +
533 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
534 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
535 * ...
536 */
537 for( i = 0; i < dlen / 16; i++ )
538 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200539 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000540
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100541 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100542 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100543 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100544 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100545 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100546 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100547 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100548 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100549 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100550 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000551
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100552 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100553 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100554 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100555 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100556 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100557 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100558 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100559 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000560 }
561
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100562exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200563 mbedtls_md5_free( &md5 );
564 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000565
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500566 mbedtls_platform_zeroize( padding, sizeof( padding ) );
567 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000568
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100569 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200574static int tls1_prf( const unsigned char *secret, size_t slen,
575 const char *label,
576 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000577 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578{
Paul Bakker23986e52011-04-24 08:57:21 +0000579 size_t nb, hs;
580 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200581 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300582 unsigned char *tmp;
583 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 const mbedtls_md_info_t *md_info;
586 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100587 int ret;
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Ron Eldor3b350852019-05-07 18:31:49 +0300591 tmp_len = 20 + strlen( label ) + rlen;
592 tmp = mbedtls_calloc( 1, tmp_len );
593 if( tmp == NULL )
594 {
595 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
596 goto exit;
597 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 hs = ( slen + 1 ) / 2;
600 S1 = secret;
601 S2 = secret + slen - hs;
602
603 nb = strlen( label );
604 memcpy( tmp + 20, label, nb );
605 memcpy( tmp + 20 + nb, random, rlen );
606 nb += rlen;
607
608 /*
609 * First compute P_md5(secret,label+random)[0..dlen]
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300612 {
613 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
614 goto exit;
615 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300618 {
619 goto exit;
620 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
623 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
624 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626 for( i = 0; i < dlen; i += 16 )
627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_md_hmac_reset ( &md_ctx );
629 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
630 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
634 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
637
638 for( j = 0; j < k; j++ )
639 dstbuf[i + j] = h_i[j];
640 }
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100643
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 /*
645 * XOR out with P_sha1(secret,label+random)[0..dlen]
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300648 {
649 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
650 goto exit;
651 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300654 {
655 goto exit;
656 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
659 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
660 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
662 for( i = 0; i < dlen; i += 20 )
663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_md_hmac_reset ( &md_ctx );
665 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
666 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_hmac_reset ( &md_ctx );
669 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
670 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
673
674 for( j = 0; j < k; j++ )
675 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
676 }
677
Ron Eldor3b350852019-05-07 18:31:49 +0300678exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100680
Ron Eldor3b350852019-05-07 18:31:49 +0300681 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500682 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Ron Eldor3b350852019-05-07 18:31:49 +0300684 mbedtls_free( tmp );
685 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500690#if defined(MBEDTLS_USE_PSA_CRYPTO)
691static int tls_prf_generic( mbedtls_md_type_t md_type,
692 const unsigned char *secret, size_t slen,
693 const char *label,
694 const unsigned char *random, size_t rlen,
695 unsigned char *dstbuf, size_t dlen )
696{
697 psa_status_t status;
698 psa_algorithm_t alg;
Janos Follath53b8ec22019-08-08 10:28:27 +0100699 psa_key_attributes_t key_attributes;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500700 psa_key_handle_t master_slot;
Janos Follathda6ac012019-08-16 13:47:29 +0100701 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100702 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500703
Andrzej Kurekc929a822019-01-14 03:51:11 -0500704 if( md_type == MBEDTLS_MD_SHA384 )
705 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
706 else
707 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
708
Janos Follath53b8ec22019-08-08 10:28:27 +0100709 key_attributes = psa_key_attributes_init();
710 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
711 psa_set_key_algorithm( &key_attributes, alg );
712 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500713
Janos Follath53b8ec22019-08-08 10:28:27 +0100714 status = psa_import_key( &key_attributes, secret, slen, &master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500715 if( status != PSA_SUCCESS )
716 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
717
Janos Follathda6ac012019-08-16 13:47:29 +0100718 status = psa_key_derivation( &derivation,
Andrzej Kurekc929a822019-01-14 03:51:11 -0500719 master_slot, alg,
720 random, rlen,
721 (unsigned char const *) label,
722 (size_t) strlen( label ),
723 dlen );
724 if( status != PSA_SUCCESS )
725 {
Janos Follathda6ac012019-08-16 13:47:29 +0100726 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500727 psa_destroy_key( master_slot );
728 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
729 }
730
Janos Follathda6ac012019-08-16 13:47:29 +0100731 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500732 if( status != PSA_SUCCESS )
733 {
Janos Follathda6ac012019-08-16 13:47:29 +0100734 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500735 psa_destroy_key( master_slot );
736 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
737 }
738
Janos Follathda6ac012019-08-16 13:47:29 +0100739 status = psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500740 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500741 {
742 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500743 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500744 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500745
746 status = psa_destroy_key( master_slot );
747 if( status != PSA_SUCCESS )
748 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
749
Andrzej Kurek33171262019-01-15 03:25:18 -0500750 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500751}
752
753#else /* MBEDTLS_USE_PSA_CRYPTO */
754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100756 const unsigned char *secret, size_t slen,
757 const char *label,
758 const unsigned char *random, size_t rlen,
759 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000760{
761 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100762 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300763 unsigned char *tmp;
764 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
Ron Eldor3b350852019-05-07 18:31:49 +0300777 tmp_len = md_len + strlen( label ) + rlen;
778 tmp = mbedtls_calloc( 1, tmp_len );
779 if( tmp == NULL )
780 {
781 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
782 goto exit;
783 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000784
785 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100786 memcpy( tmp + md_len, label, nb );
787 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000788 nb += rlen;
789
790 /*
791 * Compute P_<hash>(secret, label + random)[0..dlen]
792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300794 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
797 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
798 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100799
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100800 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_md_hmac_reset ( &md_ctx );
803 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
804 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_hmac_reset ( &md_ctx );
807 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
808 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000809
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100810 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000811
812 for( j = 0; j < k; j++ )
813 dstbuf[i + j] = h_i[j];
814 }
815
Ron Eldor3b350852019-05-07 18:31:49 +0300816exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100818
Ron Eldor3b350852019-05-07 18:31:49 +0300819 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500820 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000821
Ron Eldor3b350852019-05-07 18:31:49 +0300822 mbedtls_free( tmp );
823
824 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000825}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500826#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100828static int tls_prf_sha256( const unsigned char *secret, size_t slen,
829 const char *label,
830 const unsigned char *random, size_t rlen,
831 unsigned char *dstbuf, size_t dlen )
832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100834 label, random, rlen, dstbuf, dlen ) );
835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200839static int tls_prf_sha384( const unsigned char *secret, size_t slen,
840 const char *label,
841 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000842 unsigned char *dstbuf, size_t dlen )
843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100845 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_SHA512_C */
848#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
853 defined(MBEDTLS_SSL_PROTO_TLS1_1)
854static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#endif
Paul Bakker380da532012-04-18 16:10:25 +0000856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_SSL_PROTO_SSL3)
858static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
859static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200860#endif
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
863static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
864static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200865#endif
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
868#if defined(MBEDTLS_SHA256_C)
869static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
870static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
871static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_SHA512_C)
875static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
876static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
877static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000880
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100881#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100882 defined(MBEDTLS_USE_PSA_CRYPTO)
883static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
884{
885 if( ssl->conf->f_psk != NULL )
886 {
887 /* If we've used a callback to select the PSK,
888 * the static configuration is irrelevant. */
889 if( ssl->handshake->psk_opaque != 0 )
890 return( 1 );
891
892 return( 0 );
893 }
894
895 if( ssl->conf->psk_opaque != 0 )
896 return( 1 );
897
898 return( 0 );
899}
900#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100901 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100902
Ron Eldorcf280092019-05-14 20:19:13 +0300903#if defined(MBEDTLS_SSL_EXPORT_KEYS)
904static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
905{
906#if defined(MBEDTLS_SSL_PROTO_SSL3)
907 if( tls_prf == ssl3_prf )
908 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300909 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300910 }
911 else
912#endif
913#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
914 if( tls_prf == tls1_prf )
915 {
916 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
917 }
918 else
919#endif
920#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
921#if defined(MBEDTLS_SHA512_C)
922 if( tls_prf == tls_prf_sha384 )
923 {
924 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
925 }
926 else
927#endif
928#if defined(MBEDTLS_SHA256_C)
929 if( tls_prf == tls_prf_sha256 )
930 {
931 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
932 }
933 else
934#endif
935#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
936 return( MBEDTLS_SSL_TLS_PRF_NONE );
937}
938#endif /* MBEDTLS_SSL_EXPORT_KEYS */
939
Ron Eldor51d3ab52019-05-12 14:54:30 +0300940int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
941 const unsigned char *secret, size_t slen,
942 const char *label,
943 const unsigned char *random, size_t rlen,
944 unsigned char *dstbuf, size_t dlen )
945{
946 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
947
948 switch( prf )
949 {
950#if defined(MBEDTLS_SSL_PROTO_SSL3)
951 case MBEDTLS_SSL_TLS_PRF_SSL3:
952 tls_prf = ssl3_prf;
953 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300954#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300955#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
956 case MBEDTLS_SSL_TLS_PRF_TLS1:
957 tls_prf = tls1_prf;
958 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300959#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
960
961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300962#if defined(MBEDTLS_SHA512_C)
963 case MBEDTLS_SSL_TLS_PRF_SHA384:
964 tls_prf = tls_prf_sha384;
965 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300966#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300967#if defined(MBEDTLS_SHA256_C)
968 case MBEDTLS_SSL_TLS_PRF_SHA256:
969 tls_prf = tls_prf_sha256;
970 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300971#endif /* MBEDTLS_SHA256_C */
972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300973 default:
974 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
975 }
976
977 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
978}
979
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200980/*
981 * This function will ultimetaly only be responsible for populating a
982 * transform structure from data passed as explicit parameters.
983 *
984 * For now however it's doing rather more in a rather less explicit way.
985 */
986static int ssl_populate_transform( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000987{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200988 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000989#if defined(MBEDTLS_USE_PSA_CRYPTO)
990 int psa_fallthrough;
991#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 unsigned char keyblk[256];
994 unsigned char *key1;
995 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100996 unsigned char *mac_enc;
997 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000998 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200999 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001000 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001001 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 const mbedtls_cipher_info_t *cipher_info;
1003 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001004
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001005 /* cf. RFC 5246, Section 8.1:
1006 * "The master secret is always exactly 48 bytes in length." */
1007 size_t const master_secret_len = 48;
1008
Hanno Becker35b23c72018-10-23 12:10:41 +01001009#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1010 unsigned char session_hash[48];
1011#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_ssl_session *session = ssl->session_negotiate;
1014 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
1015 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Jaeden Amero2de07f12019-06-05 13:32:08 +01001019#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1020 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001021 transform->encrypt_then_mac = session->encrypt_then_mac;
1022#endif
1023 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001024
1025 ciphersuite_info = handshake->ciphersuite_info;
1026 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001027 if( cipher_info == NULL )
1028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001030 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001032 }
1033
Hanno Beckere694c3e2017-12-27 21:34:08 +00001034 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001035 if( md_info == NULL )
1036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001038 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001040 }
1041
Hanno Beckera0e20d02019-05-15 14:03:01 +01001042#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001043 /* Copy own and peer's CID if the use of the CID
1044 * extension has been negotiated. */
1045 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1046 {
1047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001048
Hanno Becker05154c32019-05-03 15:23:51 +01001049 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001050 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001051 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001052 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001053
1054 transform->out_cid_len = ssl->handshake->peer_cid_len;
1055 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1056 ssl->handshake->peer_cid_len );
1057 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1058 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001059 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001060#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001061
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +00001063 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +00001064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#if defined(MBEDTLS_SSL_PROTO_SSL3)
1066 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001067 {
Paul Bakker48916f92012-09-16 19:57:18 +00001068 handshake->tls_prf = ssl3_prf;
1069 handshake->calc_verify = ssl_calc_verify_ssl;
1070 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001071 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001072 else
1073#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1075 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001076 {
Paul Bakker48916f92012-09-16 19:57:18 +00001077 handshake->tls_prf = tls1_prf;
1078 handshake->calc_verify = ssl_calc_verify_tls;
1079 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001080 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001081 else
1082#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1084#if defined(MBEDTLS_SHA512_C)
1085 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001086 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001087 {
Paul Bakker48916f92012-09-16 19:57:18 +00001088 handshake->tls_prf = tls_prf_sha384;
1089 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1090 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001091 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001092 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001093#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094#if defined(MBEDTLS_SHA256_C)
1095 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001096 {
Paul Bakker48916f92012-09-16 19:57:18 +00001097 handshake->tls_prf = tls_prf_sha256;
1098 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1099 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001100 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001101 else
1102#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1106 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001107 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001108
1109 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 * SSLv3:
1111 * master =
1112 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1113 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1114 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001115 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001116 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 * master = PRF( premaster, "master secret", randbytes )[0..47]
1118 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001119 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1122 }
1123 else
1124 {
1125 /* The label for the KDF used for key expansion.
1126 * This is either "master secret" or "extended master secret"
1127 * depending on whether the Extended Master Secret extension
1128 * is used. */
1129 char const *lbl = "master secret";
1130
1131 /* The salt for the KDF used for key expansion.
1132 * - If the Extended Master Secret extension is not used,
1133 * this is ClientHello.Random + ServerHello.Random
1134 * (see Sect. 8.1 in RFC 5246).
1135 * - If the Extended Master Secret extension is used,
1136 * this is the transcript of the handshake so far.
1137 * (see Sect. 4 in RFC 7627). */
1138 unsigned char const *salt = handshake->randbytes;
1139 size_t salt_len = 64;
1140
1141#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001145
Hanno Becker35b23c72018-10-23 12:10:41 +01001146 lbl = "extended master secret";
1147 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001148 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1150 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001153 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1154 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001155 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001156 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001157 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001158#endif /* MBEDTLS_SHA512_C */
1159 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001160 }
1161 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001163 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001164
Hanno Becker35b23c72018-10-23 12:10:41 +01001165 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001166 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001167#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1168
Hanno Becker7d0a5692018-10-23 15:26:22 +01001169#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1170 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1171 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1172 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1173 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001174 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001175 /* Perform PSK-to-MS expansion in a single step. */
1176 psa_status_t status;
1177 psa_algorithm_t alg;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001178 psa_key_handle_t psk;
Janos Follathda6ac012019-08-16 13:47:29 +01001179 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +01001180 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001181
1182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1183
1184 psk = ssl->conf->psk_opaque;
1185 if( ssl->handshake->psk_opaque != 0 )
1186 psk = ssl->handshake->psk_opaque;
1187
Hanno Becker22bf1452019-04-05 11:21:08 +01001188 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001189 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1190 else
1191 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1192
Janos Follathda6ac012019-08-16 13:47:29 +01001193 status = psa_key_derivation( &derivation, psk, alg,
Hanno Becker7d0a5692018-10-23 15:26:22 +01001194 salt, salt_len,
1195 (unsigned char const *) lbl,
1196 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001197 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001198 if( status != PSA_SUCCESS )
1199 {
Janos Follathda6ac012019-08-16 13:47:29 +01001200 psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001201 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1202 }
1203
Janos Follathda6ac012019-08-16 13:47:29 +01001204 status = psa_key_derivation_output_bytes( &derivation,
Janos Follath6de99db2019-07-30 12:55:06 +01001205 session->master,
1206 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001207 if( status != PSA_SUCCESS )
1208 {
Janos Follathda6ac012019-08-16 13:47:29 +01001209 psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001210 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1211 }
1212
Janos Follathda6ac012019-08-16 13:47:29 +01001213 status = psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001214 if( status != PSA_SUCCESS )
1215 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001216 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001217 else
1218#endif
1219 {
1220 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1221 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001222 session->master,
1223 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001224 if( ret != 0 )
1225 {
1226 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1227 return( ret );
1228 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001229
Hanno Becker7d0a5692018-10-23 15:26:22 +01001230 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1231 handshake->premaster,
1232 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001233
Hanno Becker7d0a5692018-10-23 15:26:22 +01001234 mbedtls_platform_zeroize( handshake->premaster,
1235 sizeof(handshake->premaster) );
1236 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
1239 /*
1240 * Swap the client and server random values.
1241 */
Paul Bakker48916f92012-09-16 19:57:18 +00001242 memcpy( tmp, handshake->randbytes, 64 );
1243 memcpy( handshake->randbytes, tmp + 32, 32 );
1244 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001245 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
1247 /*
1248 * SSLv3:
1249 * key block =
1250 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1251 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1252 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1253 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1254 * ...
1255 *
1256 * TLSv1:
1257 * key block = PRF( master, "key expansion", randbytes )
1258 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001259 ret = handshake->tls_prf( session->master, 48, "key expansion",
1260 handshake->randbytes, 64, keyblk, 256 );
1261 if( ret != 0 )
1262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001264 return( ret );
1265 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1268 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1269 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1270 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1271 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Paul Bakker5121ce52009-01-03 21:22:43 +00001273 /*
1274 * Determine the appropriate key, IV and MAC length.
1275 */
Paul Bakker68884e32013-01-07 18:20:04 +01001276
Hanno Becker88aaf652017-12-27 08:17:40 +00001277 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001278
Hanno Becker8031d062018-01-03 15:32:31 +00001279#if defined(MBEDTLS_GCM_C) || \
1280 defined(MBEDTLS_CCM_C) || \
1281 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001283 cipher_info->mode == MBEDTLS_MODE_CCM ||
1284 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001286 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001287
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001288 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001289 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001290 transform->taglen =
1291 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001292
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001293 /* All modes haves 96-bit IVs;
1294 * GCM and CCM has 4 implicit and 8 explicit bytes
1295 * ChachaPoly has all 12 bytes implicit
1296 */
Paul Bakker68884e32013-01-07 18:20:04 +01001297 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001298 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1299 transform->fixed_ivlen = 12;
1300 else
1301 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001302
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001303 /* Minimum length of encrypted record */
1304 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001305 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001306 }
1307 else
Hanno Becker8031d062018-01-03 15:32:31 +00001308#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1309#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1310 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1311 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001312 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001313 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1315 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001318 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001321 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001322 mac_key_len = mbedtls_md_get_size( md_info );
1323 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001326 /*
1327 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1328 * (rfc 6066 page 13 or rfc 2104 section 4),
1329 * so we only need to adjust the length here.
1330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001334
1335#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1336 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001337 * HMAC implementation which also truncates the key
1338 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001339 mac_key_len = transform->maclen;
1340#endif
1341 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001343
1344 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001345 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001347 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001349 transform->minlen = transform->maclen;
1350 else
Paul Bakker68884e32013-01-07 18:20:04 +01001351 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001352 /*
1353 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001354 * 1. if EtM is in use: one block plus MAC
1355 * otherwise: * first multiple of blocklen greater than maclen
1356 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1359 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001360 {
1361 transform->minlen = transform->maclen
1362 + cipher_info->block_size;
1363 }
1364 else
1365#endif
1366 {
1367 transform->minlen = transform->maclen
1368 + cipher_info->block_size
1369 - transform->maclen % cipher_info->block_size;
1370 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1373 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1374 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001375 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001376 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001377#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1379 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1380 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001381 {
1382 transform->minlen += transform->ivlen;
1383 }
1384 else
1385#endif
1386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001388 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1389 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001390 }
Paul Bakker68884e32013-01-07 18:20:04 +01001391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001392 }
Hanno Becker8031d062018-01-03 15:32:31 +00001393 else
1394#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1395 {
1396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1397 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Hanno Becker88aaf652017-12-27 08:17:40 +00001400 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1401 (unsigned) keylen,
1402 (unsigned) transform->minlen,
1403 (unsigned) transform->ivlen,
1404 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405
1406 /*
1407 * Finally setup the cipher contexts, IVs and MAC secrets.
1408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001410 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001412 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001413 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001414
Paul Bakker68884e32013-01-07 18:20:04 +01001415 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001416 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001418 /*
1419 * This is not used in TLS v1.1.
1420 */
Paul Bakker48916f92012-09-16 19:57:18 +00001421 iv_copy_len = ( transform->fixed_ivlen ) ?
1422 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001423 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1424 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001425 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 }
1427 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_SSL_CLI_C */
1429#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001430 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001432 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001433 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
Hanno Becker81c7b182017-11-09 18:39:33 +00001435 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001436 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001438 /*
1439 * This is not used in TLS v1.1.
1440 */
Paul Bakker48916f92012-09-16 19:57:18 +00001441 iv_copy_len = ( transform->fixed_ivlen ) ?
1442 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001443 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1444 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001445 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001447 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001451 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1452 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001453 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
Hanno Beckerd56ed242018-01-03 15:32:51 +00001455#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_SSL_PROTO_SSL3)
1457 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001458 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001459 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001462 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1463 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001464 }
1465
Hanno Becker81c7b182017-11-09 18:39:33 +00001466 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1467 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001468 }
1469 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1471#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1472 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1473 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001474 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001475 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1476 For AEAD-based ciphersuites, there is nothing to do here. */
1477 if( mac_key_len != 0 )
1478 {
1479 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1480 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1481 }
Paul Bakker68884e32013-01-07 18:20:04 +01001482 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001483 else
1484#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001487 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1488 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001489 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001490#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1493 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001494 {
1495 int ret = 0;
1496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001498
Hanno Becker88aaf652017-12-27 08:17:40 +00001499 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001500 transform->iv_enc, transform->iv_dec,
1501 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001502 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001503 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001506 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1507 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001508 }
1509 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001510#else
1511 ((void) mac_dec);
1512 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001514
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001515#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1516 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001517 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001518 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1519 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001520 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001521 iv_copy_len );
1522 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001523
1524 if( ssl->conf->f_export_keys_ext != NULL )
1525 {
1526 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1527 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001528 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001529 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001530 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001531 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001532 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001533 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001534#endif
1535
Hanno Beckerf704bef2018-11-16 15:21:18 +00001536#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001537
1538 /* Only use PSA-based ciphers for TLS-1.2.
1539 * That's relevant at least for TLS-1.0, where
1540 * we assume that mbedtls_cipher_crypt() updates
1541 * the structure field for the IV, which the PSA-based
1542 * implementation currently doesn't. */
1543#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1544 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001545 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001546 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001547 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001548 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1549 {
1550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001551 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001552 }
1553
1554 if( ret == 0 )
1555 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001557 psa_fallthrough = 0;
1558 }
1559 else
1560 {
1561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1562 psa_fallthrough = 1;
1563 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001564 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001565 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001566 psa_fallthrough = 1;
1567#else
1568 psa_fallthrough = 1;
1569#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001570
Hanno Beckercb1cc802018-11-17 22:27:38 +00001571 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001572#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001573 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001577 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001578 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001579
Hanno Beckerf704bef2018-11-16 15:21:18 +00001580#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001581 /* Only use PSA-based ciphers for TLS-1.2.
1582 * That's relevant at least for TLS-1.0, where
1583 * we assume that mbedtls_cipher_crypt() updates
1584 * the structure field for the IV, which the PSA-based
1585 * implementation currently doesn't. */
1586#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1587 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001588 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001589 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001590 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001591 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1592 {
1593 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001594 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001595 }
1596
1597 if( ret == 0 )
1598 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001600 psa_fallthrough = 0;
1601 }
1602 else
1603 {
1604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1605 psa_fallthrough = 1;
1606 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001607 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001608 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001609 psa_fallthrough = 1;
1610#else
1611 psa_fallthrough = 1;
1612#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001613
Hanno Beckercb1cc802018-11-17 22:27:38 +00001614 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001615#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001616 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001617 cipher_info ) ) != 0 )
1618 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001620 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001621 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001624 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001628 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001629 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001632 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001636 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001637 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_CIPHER_MODE_CBC)
1640 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1643 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001646 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001647 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1650 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001653 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001654 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001660 // Initialize compression
1661 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001663 {
Paul Bakker16770332013-10-11 09:59:44 +02001664 if( ssl->compress_buf == NULL )
1665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001667 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001668 if( ssl->compress_buf == NULL )
1669 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001671 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001672 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1673 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001674 }
1675 }
1676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001678
Paul Bakker48916f92012-09-16 19:57:18 +00001679 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1680 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001681
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001682 if( deflateInit( &transform->ctx_deflate,
1683 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001684 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001687 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1688 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001689 }
1690 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001694end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001695 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1696 mbedtls_platform_zeroize( handshake->randbytes,
1697 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001698 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001699}
1700
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001701int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1702{
1703 return( ssl_populate_transform( ssl ) );
1704}
1705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_SSL_PROTO_SSL3)
1707void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001708{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001709 mbedtls_md5_context md5;
1710 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001711 unsigned char pad_1[48];
1712 unsigned char pad_2[48];
1713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001716 mbedtls_md5_init( &md5 );
1717 mbedtls_sha1_init( &sha1 );
1718
1719 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1720 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001721
Paul Bakker380da532012-04-18 16:10:25 +00001722 memset( pad_1, 0x36, 48 );
1723 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001724
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001725 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1726 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1727 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001728
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001729 mbedtls_md5_starts_ret( &md5 );
1730 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1731 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1732 mbedtls_md5_update_ret( &md5, hash, 16 );
1733 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001735 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1736 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1737 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001738
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001739 mbedtls_sha1_starts_ret( &sha1 );
1740 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1741 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1742 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1743 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001747
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001748 mbedtls_md5_free( &md5 );
1749 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001750
Paul Bakker380da532012-04-18 16:10:25 +00001751 return;
1752}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1756void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001757{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001758 mbedtls_md5_context md5;
1759 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001762
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001763 mbedtls_md5_init( &md5 );
1764 mbedtls_sha1_init( &sha1 );
1765
1766 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1767 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001768
Andrzej Kurekeb342242019-01-29 09:14:33 -05001769 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001770 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001774
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001775 mbedtls_md5_free( &md5 );
1776 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001777
Paul Bakker380da532012-04-18 16:10:25 +00001778 return;
1779}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1783#if defined(MBEDTLS_SHA256_C)
1784void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001785{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001786#if defined(MBEDTLS_USE_PSA_CRYPTO)
1787 size_t hash_size;
1788 psa_status_t status;
1789 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1790
1791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1792 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1793 if( status != PSA_SUCCESS )
1794 {
1795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1796 return;
1797 }
1798
1799 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1800 if( status != PSA_SUCCESS )
1801 {
1802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1803 return;
1804 }
1805 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1807#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001808 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001809
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001810 mbedtls_sha256_init( &sha256 );
1811
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001813
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001814 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001815 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001819
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001820 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001821#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001822 return;
1823}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#if defined(MBEDTLS_SHA512_C)
1827void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001828{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001829#if defined(MBEDTLS_USE_PSA_CRYPTO)
1830 size_t hash_size;
1831 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001832 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001833
1834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001835 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001836 if( status != PSA_SUCCESS )
1837 {
1838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1839 return;
1840 }
1841
Andrzej Kurek972fba52019-01-30 03:29:12 -05001842 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001843 if( status != PSA_SUCCESS )
1844 {
1845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1846 return;
1847 }
1848 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1850#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001851 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001852
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001853 mbedtls_sha512_init( &sha512 );
1854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001856
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001857 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001858 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001863 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001864#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 return;
1866}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867#endif /* MBEDTLS_SHA512_C */
1868#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1871int 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 +02001872{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001873 unsigned char *p = ssl->handshake->premaster;
1874 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001875 const unsigned char *psk = ssl->conf->psk;
1876 size_t psk_len = ssl->conf->psk_len;
1877
1878 /* If the psk callback was called, use its result */
1879 if( ssl->handshake->psk != NULL )
1880 {
1881 psk = ssl->handshake->psk;
1882 psk_len = ssl->handshake->psk_len;
1883 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001884
1885 /*
1886 * PMS = struct {
1887 * opaque other_secret<0..2^16-1>;
1888 * opaque psk<0..2^16-1>;
1889 * };
1890 * with "other_secret" depending on the particular key exchange
1891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1893 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001894 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001895 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001897
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001898 *(p++) = (unsigned char)( psk_len >> 8 );
1899 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001900
1901 if( end < p || (size_t)( end - p ) < psk_len )
1902 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1903
1904 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001905 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001906 }
1907 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1909#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1910 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001911 {
1912 /*
1913 * other_secret already set by the ClientKeyExchange message,
1914 * and is 48 bytes long
1915 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001916 if( end - p < 2 )
1917 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1918
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001919 *p++ = 0;
1920 *p++ = 48;
1921 p += 48;
1922 }
1923 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1925#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1926 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001927 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001928 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001929 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001930
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001931 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001933 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001934 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001937 return( ret );
1938 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001939 *(p++) = (unsigned char)( len >> 8 );
1940 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001941 p += len;
1942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001944 }
1945 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1947#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1948 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001949 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001950 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001951 size_t zlen;
1952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001954 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001955 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001958 return( ret );
1959 }
1960
1961 *(p++) = (unsigned char)( zlen >> 8 );
1962 *(p++) = (unsigned char)( zlen );
1963 p += zlen;
1964
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001965 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1966 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001967 }
1968 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001973 }
1974
1975 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001976 if( end - p < 2 )
1977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001978
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001979 *(p++) = (unsigned char)( psk_len >> 8 );
1980 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001981
1982 if( end < p || (size_t)( end - p ) < psk_len )
1983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1984
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001985 memcpy( p, psk, psk_len );
1986 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001987
1988 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1989
1990 return( 0 );
1991}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001995/*
1996 * SSLv3.0 MAC functions
1997 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001998#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001999static void ssl_mac( mbedtls_md_context_t *md_ctx,
2000 const unsigned char *secret,
2001 const unsigned char *buf, size_t len,
2002 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002003 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002004{
2005 unsigned char header[11];
2006 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002007 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2009 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002010
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002011 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002013 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002014 else
Paul Bakker68884e32013-01-07 18:20:04 +01002015 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002016
2017 memcpy( header, ctr, 8 );
2018 header[ 8] = (unsigned char) type;
2019 header[ 9] = (unsigned char)( len >> 8 );
2020 header[10] = (unsigned char)( len );
2021
Paul Bakker68884e32013-01-07 18:20:04 +01002022 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 mbedtls_md_starts( md_ctx );
2024 mbedtls_md_update( md_ctx, secret, md_size );
2025 mbedtls_md_update( md_ctx, padding, padlen );
2026 mbedtls_md_update( md_ctx, header, 11 );
2027 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002028 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002029
Paul Bakker68884e32013-01-07 18:20:04 +01002030 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 mbedtls_md_starts( md_ctx );
2032 mbedtls_md_update( md_ctx, secret, md_size );
2033 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002034 mbedtls_md_update( md_ctx, out, md_size );
2035 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002036}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002038
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002039/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002040 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002041#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002042 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2043 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2044 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2045/* This function makes sure every byte in the memory region is accessed
2046 * (in ascending addresses order) */
2047static void ssl_read_memory( unsigned char *p, size_t len )
2048{
2049 unsigned char acc = 0;
2050 volatile unsigned char force;
2051
2052 for( ; len != 0; p++, len-- )
2053 acc ^= *p;
2054
2055 force = acc;
2056 (void) force;
2057}
2058#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2059
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002060/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002062 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002063
Hanno Beckera0e20d02019-05-15 14:03:01 +01002064#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002065/* This functions transforms a DTLS plaintext fragment and a record content
2066 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002067 *
2068 * struct {
2069 * opaque content[DTLSPlaintext.length];
2070 * ContentType real_type;
2071 * uint8 zeros[length_of_padding];
2072 * } DTLSInnerPlaintext;
2073 *
2074 * Input:
2075 * - `content`: The beginning of the buffer holding the
2076 * plaintext to be wrapped.
2077 * - `*content_size`: The length of the plaintext in Bytes.
2078 * - `max_len`: The number of Bytes available starting from
2079 * `content`. This must be `>= *content_size`.
2080 * - `rec_type`: The desired record content type.
2081 *
2082 * Output:
2083 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2084 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2085 *
2086 * Returns:
2087 * - `0` on success.
2088 * - A negative error code if `max_len` didn't offer enough space
2089 * for the expansion.
2090 */
2091static int ssl_cid_build_inner_plaintext( unsigned char *content,
2092 size_t *content_size,
2093 size_t remaining,
2094 uint8_t rec_type )
2095{
2096 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002097 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2098 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2099 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002100
2101 /* Write real content type */
2102 if( remaining == 0 )
2103 return( -1 );
2104 content[ len ] = rec_type;
2105 len++;
2106 remaining--;
2107
2108 if( remaining < pad )
2109 return( -1 );
2110 memset( content + len, 0, pad );
2111 len += pad;
2112 remaining -= pad;
2113
2114 *content_size = len;
2115 return( 0 );
2116}
2117
Hanno Becker07dc97d2019-05-20 15:08:01 +01002118/* This function parses a DTLSInnerPlaintext structure.
2119 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002120static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2121 size_t *content_size,
2122 uint8_t *rec_type )
2123{
2124 size_t remaining = *content_size;
2125
2126 /* Determine length of padding by skipping zeroes from the back. */
2127 do
2128 {
2129 if( remaining == 0 )
2130 return( -1 );
2131 remaining--;
2132 } while( content[ remaining ] == 0 );
2133
2134 *content_size = remaining;
2135 *rec_type = content[ remaining ];
2136
2137 return( 0 );
2138}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002139#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002140
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002141/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002142 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002143static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002144 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002145 mbedtls_record *rec )
2146{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002147 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002148 *
2149 * additional_data = seq_num + TLSCompressed.type +
2150 * TLSCompressed.version + TLSCompressed.length;
2151 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002152 * For the CID extension, this is extended as follows
2153 * (quoting draft-ietf-tls-dtls-connection-id-05,
2154 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002155 *
2156 * additional_data = seq_num + DTLSPlaintext.type +
2157 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002158 * cid +
2159 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002160 * length_of_DTLSInnerPlaintext;
2161 */
2162
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002163 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2164 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002165 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002166
Hanno Beckera0e20d02019-05-15 14:03:01 +01002167#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002168 if( rec->cid_len != 0 )
2169 {
2170 memcpy( add_data + 11, rec->cid, rec->cid_len );
2171 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2172 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2173 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2174 *add_data_len = 13 + 1 + rec->cid_len;
2175 }
2176 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002177#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002178 {
2179 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2180 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2181 *add_data_len = 13;
2182 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002183}
2184
Hanno Beckera18d1322018-01-03 14:27:32 +00002185int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2186 mbedtls_ssl_transform *transform,
2187 mbedtls_record *rec,
2188 int (*f_rng)(void *, unsigned char *, size_t),
2189 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002190{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002192 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002193 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002194 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002195 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002196 size_t post_avail;
2197
2198 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002199#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002200 ((void) ssl);
2201#endif
2202
2203 /* The PRNG is used for dynamic IV generation that's used
2204 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2205#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2206 ( defined(MBEDTLS_AES_C) || \
2207 defined(MBEDTLS_ARIA_C) || \
2208 defined(MBEDTLS_CAMELLIA_C) ) && \
2209 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2210 ((void) f_rng);
2211 ((void) p_rng);
2212#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002216 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002217 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2219 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2220 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002221 if( rec == NULL
2222 || rec->buf == NULL
2223 || rec->buf_len < rec->data_offset
2224 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002225#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002226 || rec->cid_len != 0
2227#endif
2228 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002229 {
2230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002232 }
2233
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002234 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002235 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002237 data, rec->data_len );
2238
2239 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2240
2241 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2242 {
2243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2244 (unsigned) rec->data_len,
2245 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2247 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002248
Hanno Beckera0e20d02019-05-15 14:03:01 +01002249#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002250 /*
2251 * Add CID information
2252 */
2253 rec->cid_len = transform->out_cid_len;
2254 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2255 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002256
2257 if( rec->cid_len != 0 )
2258 {
2259 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002260 * Wrap plaintext into DTLSInnerPlaintext structure.
2261 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002262 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002263 * Note that this changes `rec->data_len`, and hence
2264 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002265 */
2266 if( ssl_cid_build_inner_plaintext( data,
2267 &rec->data_len,
2268 post_avail,
2269 rec->type ) != 0 )
2270 {
2271 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2272 }
2273
2274 rec->type = MBEDTLS_SSL_MSG_CID;
2275 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002276#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002277
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002278 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2279
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002281 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 */
Hanno Becker52344c22018-01-03 15:24:20 +00002283#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 if( mode == MBEDTLS_MODE_STREAM ||
2285 ( mode == MBEDTLS_MODE_CBC
2286#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002287 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002288#endif
2289 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002291 if( post_avail < transform->maclen )
2292 {
2293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2294 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2295 }
2296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002298 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002299 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002300 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002301 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2302 data, rec->data_len, rec->ctr, rec->type, mac );
2303 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002304 }
2305 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002306#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2308 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002309 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002310 {
Hanno Becker992b6872017-11-09 18:57:39 +00002311 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2312
Hanno Beckercab87e62019-04-29 13:52:53 +01002313 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002314
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002315 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002316 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002317 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2318 data, rec->data_len );
2319 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2320 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2321
2322 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002323 }
2324 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002325#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002329 }
2330
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002331 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2332 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002333
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002334 rec->data_len += transform->maclen;
2335 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002336 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002337 }
Hanno Becker52344c22018-01-03 15:24:20 +00002338#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002340 /*
2341 * Encrypt
2342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2344 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002346 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002347 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002349 "including %d bytes of padding",
2350 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002351
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002352 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2353 transform->iv_enc, transform->ivlen,
2354 data, rec->data_len,
2355 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002358 return( ret );
2359 }
2360
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002361 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2364 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002365 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 }
Paul Bakker68884e32013-01-07 18:20:04 +01002367 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002369
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002370#if defined(MBEDTLS_GCM_C) || \
2371 defined(MBEDTLS_CCM_C) || \
2372 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002374 mode == MBEDTLS_MODE_CCM ||
2375 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002376 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002377 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002378 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002379 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002380
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002381 /* Check that there's space for both the authentication tag
2382 * and the explicit IV before and after the record content. */
2383 if( post_avail < transform->taglen ||
2384 rec->data_offset < explicit_iv_len )
2385 {
2386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2387 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2388 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002389
Paul Bakker68884e32013-01-07 18:20:04 +01002390 /*
2391 * Generate IV
2392 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002393 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2394 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002395 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002396 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002397 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2398 explicit_iv_len );
2399 /* Prefix record content with explicit IV. */
2400 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002401 }
2402 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2403 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002404 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002405 unsigned char i;
2406
2407 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2408
2409 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002410 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002411 }
2412 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002413 {
2414 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002417 }
2418
Hanno Beckercab87e62019-04-29 13:52:53 +01002419 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002420
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002421 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2422 iv, transform->ivlen );
2423 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002424 data - explicit_iv_len, explicit_iv_len );
2425 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002426 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002428 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002429 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002430
Paul Bakker68884e32013-01-07 18:20:04 +01002431 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002432 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002433 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002434
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002435 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002436 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002437 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002438 data, rec->data_len, /* source */
2439 data, &rec->data_len, /* destination */
2440 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002443 return( ret );
2444 }
2445
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002446 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2447 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002448
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002449 rec->data_len += transform->taglen + explicit_iv_len;
2450 rec->data_offset -= explicit_iv_len;
2451 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002452 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002453 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2456#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002457 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002460 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002461 size_t padlen, i;
2462 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002463
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002464 /* Currently we're always using minimal padding
2465 * (up to 255 bytes would be allowed). */
2466 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2467 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 padlen = 0;
2469
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002470 /* Check there's enough space in the buffer for the padding. */
2471 if( post_avail < padlen + 1 )
2472 {
2473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2474 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2475 }
2476
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002478 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002480 rec->data_len += padlen + 1;
2481 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002484 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002485 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2486 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002487 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002488 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002489 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002490 if( f_rng == NULL )
2491 {
2492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2493 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2494 }
2495
2496 if( rec->data_offset < transform->ivlen )
2497 {
2498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2499 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2500 }
2501
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002502 /*
2503 * Generate IV
2504 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002505 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002506 if( ret != 0 )
2507 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002508
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002509 memcpy( data - transform->ivlen, transform->iv_enc,
2510 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002511
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002512 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002516 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002517 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002518 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002520 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2521 transform->iv_enc,
2522 transform->ivlen,
2523 data, rec->data_len,
2524 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002527 return( ret );
2528 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002529
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002530 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002534 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002537 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002538 {
2539 /*
2540 * Save IV in SSL3 and TLS1
2541 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002542 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2543 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002545 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002546#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002547 {
2548 data -= transform->ivlen;
2549 rec->data_offset -= transform->ivlen;
2550 rec->data_len += transform->ivlen;
2551 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002554 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002555 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002556 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2557
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002558 /*
2559 * MAC(MAC_write_key, seq_num +
2560 * TLSCipherText.type +
2561 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002562 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002563 * IV + // except for TLS 1.0
2564 * ENC(content + padding + padding_length));
2565 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002566
2567 if( post_avail < transform->maclen)
2568 {
2569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2570 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2571 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002572
Hanno Beckercab87e62019-04-29 13:52:53 +01002573 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002576 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002577 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002578
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002579 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002580 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002581 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2582 data, rec->data_len );
2583 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2584 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002585
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002586 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002587
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002588 rec->data_len += transform->maclen;
2589 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002590 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002591 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002594 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002596 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2599 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002602 /* Make extra sure authentication was performed, exactly once */
2603 if( auth_done != 1 )
2604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2606 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002607 }
2608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
2611 return( 0 );
2612}
2613
Hanno Becker605949f2019-07-12 08:23:59 +01002614int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002615 mbedtls_ssl_transform *transform,
2616 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002617{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002618 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002620 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002621#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002622 size_t padlen = 0, correct = 1;
2623#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002624 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002625 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002626 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002627
Hanno Beckera18d1322018-01-03 14:27:32 +00002628#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002629 ((void) ssl);
2630#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002633 if( rec == NULL ||
2634 rec->buf == NULL ||
2635 rec->buf_len < rec->data_offset ||
2636 rec->buf_len - rec->data_offset < rec->data_len )
2637 {
2638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002640 }
2641
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002642 data = rec->buf + rec->data_offset;
2643 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
Hanno Beckera0e20d02019-05-15 14:03:01 +01002645#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002646 /*
2647 * Match record's CID with incoming CID.
2648 */
Hanno Becker938489a2019-05-08 13:02:22 +01002649 if( rec->cid_len != transform->in_cid_len ||
2650 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2651 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002652 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002653 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002654#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2657 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002658 {
2659 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002660 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2661 transform->iv_dec,
2662 transform->ivlen,
2663 data, rec->data_len,
2664 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002667 return( ret );
2668 }
2669
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002670 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2673 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002674 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 }
Paul Bakker68884e32013-01-07 18:20:04 +01002676 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002678#if defined(MBEDTLS_GCM_C) || \
2679 defined(MBEDTLS_CCM_C) || \
2680 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002682 mode == MBEDTLS_MODE_CCM ||
2683 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002684 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002685 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002686 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002687
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002688 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002689 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002690 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002691
2692 /* Check that there's enough space for the explicit IV
2693 * (at the beginning of the record) and the MAC (at the
2694 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002695 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002698 "+ taglen (%d)", rec->data_len,
2699 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002701 }
Paul Bakker68884e32013-01-07 18:20:04 +01002702
Hanno Beckerd96a6522019-07-10 13:55:25 +01002703#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002704 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2705 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002706 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002707
Hanno Beckerd96a6522019-07-10 13:55:25 +01002708 /* Fixed */
2709 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2710 /* Explicit */
2711 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002712 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002713 else
2714#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2715#if defined(MBEDTLS_CHACHAPOLY_C)
2716 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002717 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002718 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002719 unsigned char i;
2720
2721 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2722
2723 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002724 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002725 }
2726 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002727#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002728 {
2729 /* Reminder if we ever add an AEAD mode with a different size */
2730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2731 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2732 }
2733
Hanno Beckerd96a6522019-07-10 13:55:25 +01002734 /* Group changes to data, data_len, and add_data, because
2735 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002736 data += explicit_iv_len;
2737 rec->data_offset += explicit_iv_len;
2738 rec->data_len -= explicit_iv_len + transform->taglen;
2739
Hanno Beckercab87e62019-04-29 13:52:53 +01002740 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002741 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002742 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002743
Hanno Beckerd96a6522019-07-10 13:55:25 +01002744 /* Because of the check above, we know that there are
2745 * explicit_iv_len Bytes preceeding data, and taglen
2746 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002747 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002748 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002749
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002750 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002751 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002752 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002753
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002754 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002755 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002756 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002757 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2758 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002759 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002760 data, rec->data_len,
2761 data, &olen,
2762 data + rec->data_len,
2763 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2768 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002769
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002770 return( ret );
2771 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002772 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002773
Hanno Beckerd96a6522019-07-10 13:55:25 +01002774 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002775 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002779 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002780 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2783#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002784 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002787 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002788
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 /*
Paul Bakker45829992013-01-03 14:52:21 +01002790 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002793 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2794 {
2795 /* The ciphertext is prefixed with the CBC IV. */
2796 minlen += transform->ivlen;
2797 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002798#endif
Paul Bakker45829992013-01-03 14:52:21 +01002799
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002800 /* Size considerations:
2801 *
2802 * - The CBC cipher text must not be empty and hence
2803 * at least of size transform->ivlen.
2804 *
2805 * Together with the potential IV-prefix, this explains
2806 * the first of the two checks below.
2807 *
2808 * - The record must contain a MAC, either in plain or
2809 * encrypted, depending on whether Encrypt-then-MAC
2810 * is used or not.
2811 * - If it is, the message contains the IV-prefix,
2812 * the CBC ciphertext, and the MAC.
2813 * - If it is not, the padded plaintext, and hence
2814 * the CBC ciphertext, has at least length maclen + 1
2815 * because there is at least the padding length byte.
2816 *
2817 * As the CBC ciphertext is not empty, both cases give the
2818 * lower bound minlen + maclen + 1 on the record size, which
2819 * we test for in the second check below.
2820 */
2821 if( rec->data_len < minlen + transform->ivlen ||
2822 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002825 "+ 1 ) ( + expl IV )", rec->data_len,
2826 transform->ivlen,
2827 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002829 }
2830
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002831 /*
2832 * Authenticate before decrypt if enabled
2833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002835 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002836 {
Hanno Becker992b6872017-11-09 18:57:39 +00002837 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002840
Hanno Beckerd96a6522019-07-10 13:55:25 +01002841 /* Update data_len in tandem with add_data.
2842 *
2843 * The subtraction is safe because of the previous check
2844 * data_len >= minlen + maclen + 1.
2845 *
2846 * Afterwards, we know that data + data_len is followed by at
2847 * least maclen Bytes, which justifies the call to
2848 * mbedtls_ssl_safer_memcmp() below.
2849 *
2850 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002851 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002852 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002853
Hanno Beckerd96a6522019-07-10 13:55:25 +01002854 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002855 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2856 add_data_len );
2857 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2858 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002859 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2860 data, rec->data_len );
2861 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2862 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002863
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002864 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2865 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002866 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002867 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002868
Hanno Beckerd96a6522019-07-10 13:55:25 +01002869 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002870 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2871 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002875 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002876 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002877 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002879
2880 /*
2881 * Check length sanity
2882 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002883
2884 /* We know from above that data_len > minlen >= 0,
2885 * so the following check in particular implies that
2886 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002887 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002892 }
2893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002895 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002896 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002897 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002898 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002899 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002900 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002901 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002902
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002903 data += transform->ivlen;
2904 rec->data_offset += transform->ivlen;
2905 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002906 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002908
Hanno Beckerd96a6522019-07-10 13:55:25 +01002909 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2910
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002911 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2912 transform->iv_dec, transform->ivlen,
2913 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002916 return( ret );
2917 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002918
Hanno Beckerd96a6522019-07-10 13:55:25 +01002919 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002920 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2923 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002924 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002927 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002928 {
2929 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002930 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
2931 * records is equivalent to CBC decryption of the concatenation
2932 * of the records; in other words, IVs are maintained across
2933 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02002934 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002935 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2936 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002937 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002938#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002940 /* Safe since data_len >= minlen + maclen + 1, so after having
2941 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01002942 * data_len > 0 (because of data_len % ivlen == 0, it's actually
2943 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002944 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002945
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002946 if( auth_done == 1 )
2947 {
2948 correct *= ( rec->data_len >= padlen + 1 );
2949 padlen *= ( rec->data_len >= padlen + 1 );
2950 }
2951 else
Paul Bakker45829992013-01-03 14:52:21 +01002952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002954 if( rec->data_len < transform->maclen + padlen + 1 )
2955 {
2956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2957 rec->data_len,
2958 transform->maclen,
2959 padlen + 1 ) );
2960 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002961#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002962
2963 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2964 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002965 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002967 padlen++;
2968
2969 /* Regardless of the validity of the padding,
2970 * we have data_len >= padlen here. */
2971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002973 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002974 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002975 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#if defined(MBEDTLS_SSL_DEBUG_ALL)
2978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002979 "should be no more than %d",
2980 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002981#endif
Paul Bakker45829992013-01-03 14:52:21 +01002982 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002983 }
2984 }
2985 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2987#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2988 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002989 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002990 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002991 /* The padding check involves a series of up to 256
2992 * consecutive memory reads at the end of the record
2993 * plaintext buffer. In order to hide the length and
2994 * validity of the padding, always perform exactly
2995 * `min(256,plaintext_len)` reads (but take into account
2996 * only the last `padlen` bytes for the padding check). */
2997 size_t pad_count = 0;
2998 size_t real_count = 0;
2999 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003000
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003001 /* Index of first padding byte; it has been ensured above
3002 * that the subtraction is safe. */
3003 size_t const padding_idx = rec->data_len - padlen;
3004 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3005 size_t const start_idx = rec->data_len - num_checks;
3006 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003007
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003008 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003009 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003010 real_count |= ( idx >= padding_idx );
3011 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003012 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003013 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003016 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003018#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003019 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003020 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003021 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3023 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3026 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003027 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003028
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003029 /* If the padding was found to be invalid, padlen == 0
3030 * and the subtraction is safe. If the padding was found valid,
3031 * padlen hasn't been changed and the previous assertion
3032 * data_len >= padlen still holds. */
3033 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003034 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003037 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3040 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003041 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003042
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003043#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003045 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
3048 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003049 * Authenticate if not done yet.
3050 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 */
Hanno Becker52344c22018-01-03 15:24:20 +00003052#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003053 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003054 {
Hanno Becker992b6872017-11-09 18:57:39 +00003055 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003056
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003057 /* If the initial value of padlen was such that
3058 * data_len < maclen + padlen + 1, then padlen
3059 * got reset to 1, and the initial check
3060 * data_len >= minlen + maclen + 1
3061 * guarantees that at this point we still
3062 * have at least data_len >= maclen.
3063 *
3064 * If the initial value of padlen was such that
3065 * data_len >= maclen + padlen + 1, then we have
3066 * subtracted either padlen + 1 (if the padding was correct)
3067 * or 0 (if the padding was incorrect) since then,
3068 * hence data_len >= maclen in any case.
3069 */
3070 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003071 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003074 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003075 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003076 ssl_mac( &transform->md_ctx_dec,
3077 transform->mac_dec,
3078 data, rec->data_len,
3079 rec->ctr, rec->type,
3080 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003081 }
3082 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3084#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3085 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003086 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003087 {
3088 /*
3089 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003090 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003091 *
3092 * Known timing attacks:
3093 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3094 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003095 * To compensate for different timings for the MAC calculation
3096 * depending on how much padding was removed (which is determined
3097 * by padlen), process extra_run more blocks through the hash
3098 * function.
3099 *
3100 * The formula in the paper is
3101 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3102 * where L1 is the size of the header plus the decrypted message
3103 * plus CBC padding and L2 is the size of the header plus the
3104 * decrypted message. This is for an underlying hash function
3105 * with 64-byte blocks.
3106 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3107 * correctly. We round down instead of up, so -56 is the correct
3108 * value for our calculations instead of -55.
3109 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003110 * Repeat the formula rather than defining a block_size variable.
3111 * This avoids requiring division by a variable at runtime
3112 * (which would be marginally less efficient and would require
3113 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003114 */
3115 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003116 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003117
3118 /*
3119 * The next two sizes are the minimum and maximum values of
3120 * in_msglen over all padlen values.
3121 *
3122 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003123 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003124 *
3125 * Note that max_len + maclen is never more than the buffer
3126 * length, as we previously did in_msglen -= maclen too.
3127 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003128 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003129 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3130
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003131 memset( tmp, 0, sizeof( tmp ) );
3132
3133 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003134 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003135#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3136 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003137 case MBEDTLS_MD_MD5:
3138 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003139 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003140 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003141 extra_run =
3142 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3143 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003144 break;
3145#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003146#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003147 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003148 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003149 extra_run =
3150 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3151 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003152 break;
3153#endif
3154 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3157 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003158
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003159 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003160
Hanno Beckercab87e62019-04-29 13:52:53 +01003161 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3162 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003163 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3164 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003165 /* Make sure we access everything even when padlen > 0. This
3166 * makes the synchronisation requirements for just-in-time
3167 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003168 ssl_read_memory( data + rec->data_len, padlen );
3169 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003170
3171 /* Call mbedtls_md_process at least once due to cache attacks
3172 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003173 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003174 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003175
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003176 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003177
3178 /* Make sure we access all the memory that could contain the MAC,
3179 * before we check it in the next code block. This makes the
3180 * synchronisation requirements for just-in-time Prime+Probe
3181 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003182 ssl_read_memory( data + min_len,
3183 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003184 }
3185 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3187 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3190 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003191 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003192
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003193#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003194 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3195 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003196#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003197
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003198 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3199 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201#if defined(MBEDTLS_SSL_DEBUG_ALL)
3202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003203#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003204 correct = 0;
3205 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003206 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003207 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003208
3209 /*
3210 * Finally check the correct flag
3211 */
3212 if( correct == 0 )
3213 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003214#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003215
3216 /* Make extra sure authentication was performed, exactly once */
3217 if( auth_done != 1 )
3218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003221 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
Hanno Beckera0e20d02019-05-15 14:03:01 +01003223#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003224 if( rec->cid_len != 0 )
3225 {
3226 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3227 &rec->type );
3228 if( ret != 0 )
3229 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3230 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003231#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003234
3235 return( 0 );
3236}
3237
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003238#undef MAC_NONE
3239#undef MAC_PLAINTEXT
3240#undef MAC_CIPHERTEXT
3241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003243/*
3244 * Compression/decompression functions
3245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003247{
3248 int ret;
3249 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003250 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003251 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003252 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003255
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003256 if( len_pre == 0 )
3257 return( 0 );
3258
Paul Bakker2770fbd2012-07-03 13:30:23 +00003259 memcpy( msg_pre, ssl->out_msg, len_pre );
3260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003262 ssl->out_msglen ) );
3263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003265 ssl->out_msg, ssl->out_msglen );
3266
Paul Bakker48916f92012-09-16 19:57:18 +00003267 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3268 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3269 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003270 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003271
Paul Bakker48916f92012-09-16 19:57:18 +00003272 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003273 if( ret != Z_OK )
3274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3276 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003277 }
3278
Angus Grattond8213d02016-05-25 20:56:48 +10003279 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003280 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003283 ssl->out_msglen ) );
3284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003286 ssl->out_msg, ssl->out_msglen );
3287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003289
3290 return( 0 );
3291}
3292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003294{
3295 int ret;
3296 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003297 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003298 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003299 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003302
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003303 if( len_pre == 0 )
3304 return( 0 );
3305
Paul Bakker2770fbd2012-07-03 13:30:23 +00003306 memcpy( msg_pre, ssl->in_msg, len_pre );
3307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003309 ssl->in_msglen ) );
3310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003312 ssl->in_msg, ssl->in_msglen );
3313
Paul Bakker48916f92012-09-16 19:57:18 +00003314 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3315 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3316 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003317 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003318 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003319
Paul Bakker48916f92012-09-16 19:57:18 +00003320 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003321 if( ret != Z_OK )
3322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3324 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003325 }
3326
Angus Grattond8213d02016-05-25 20:56:48 +10003327 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003328 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003331 ssl->in_msglen ) );
3332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003334 ssl->in_msg, ssl->in_msglen );
3335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003337
3338 return( 0 );
3339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3343static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345#if defined(MBEDTLS_SSL_PROTO_DTLS)
3346static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003347{
3348 /* If renegotiation is not enforced, retransmit until we would reach max
3349 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003350 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003351 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003352 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003353 unsigned char doublings = 1;
3354
3355 while( ratio != 0 )
3356 {
3357 ++doublings;
3358 ratio >>= 1;
3359 }
3360
3361 if( ++ssl->renego_records_seen > doublings )
3362 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003364 return( 0 );
3365 }
3366 }
3367
3368 return( ssl_write_hello_request( ssl ) );
3369}
3370#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003372
Paul Bakker5121ce52009-01-03 21:22:43 +00003373/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003374 * Fill the input message buffer by appending data to it.
3375 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003376 *
3377 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3378 * available (from this read and/or a previous one). Otherwise, an error code
3379 * is returned (possibly EOF or WANT_READ).
3380 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003381 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3382 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3383 * since we always read a whole datagram at once.
3384 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003385 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003386 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003389{
Paul Bakker23986e52011-04-24 08:57:21 +00003390 int ret;
3391 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003394
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003395 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003398 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003400 }
3401
Angus Grattond8213d02016-05-25 20:56:48 +10003402 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3405 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003406 }
3407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003409 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003410 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003411 uint32_t timeout;
3412
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003413 /* Just to be sure */
3414 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3415 {
3416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3417 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3419 }
3420
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003421 /*
3422 * The point is, we need to always read a full datagram at once, so we
3423 * sometimes read more then requested, and handle the additional data.
3424 * It could be the rest of the current record (while fetching the
3425 * header) and/or some other records in the same datagram.
3426 */
3427
3428 /*
3429 * Move to the next record in the already read datagram if applicable
3430 */
3431 if( ssl->next_record_offset != 0 )
3432 {
3433 if( ssl->in_left < ssl->next_record_offset )
3434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3436 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003437 }
3438
3439 ssl->in_left -= ssl->next_record_offset;
3440
3441 if( ssl->in_left != 0 )
3442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003444 ssl->next_record_offset ) );
3445 memmove( ssl->in_hdr,
3446 ssl->in_hdr + ssl->next_record_offset,
3447 ssl->in_left );
3448 }
3449
3450 ssl->next_record_offset = 0;
3451 }
3452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003455
3456 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003457 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003458 */
3459 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003462 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003463 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003464
3465 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003466 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003467 * are not at the beginning of a new record, the caller did something
3468 * wrong.
3469 */
3470 if( ssl->in_left != 0 )
3471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003474 }
3475
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003476 /*
3477 * Don't even try to read if time's out already.
3478 * This avoids by-passing the timer when repeatedly receiving messages
3479 * that will end up being dropped.
3480 */
3481 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003482 {
3483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003484 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003485 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003486 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003487 {
Angus Grattond8213d02016-05-25 20:56:48 +10003488 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003491 timeout = ssl->handshake->retransmit_timeout;
3492 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003493 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003496
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003497 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003498 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3499 timeout );
3500 else
3501 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003504
3505 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003507 }
3508
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003509 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003512 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003515 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003516 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003519 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003520 }
3521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003525 return( ret );
3526 }
3527
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003528 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003531 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003533 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003534 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003537 return( ret );
3538 }
3539
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003540 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003541 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003543 }
3544
Paul Bakker5121ce52009-01-03 21:22:43 +00003545 if( ret < 0 )
3546 return( ret );
3547
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003548 ssl->in_left = ret;
3549 }
3550 else
3551#endif
3552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003554 ssl->in_left, nb_want ) );
3555
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003556 while( ssl->in_left < nb_want )
3557 {
3558 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003559
3560 if( ssl_check_timer( ssl ) != 0 )
3561 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3562 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003563 {
3564 if( ssl->f_recv_timeout != NULL )
3565 {
3566 ret = ssl->f_recv_timeout( ssl->p_bio,
3567 ssl->in_hdr + ssl->in_left, len,
3568 ssl->conf->read_timeout );
3569 }
3570 else
3571 {
3572 ret = ssl->f_recv( ssl->p_bio,
3573 ssl->in_hdr + ssl->in_left, len );
3574 }
3575 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003578 ssl->in_left, nb_want ) );
3579 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003580
3581 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003583
3584 if( ret < 0 )
3585 return( ret );
3586
mohammad160352aecb92018-03-28 23:41:40 -07003587 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003588 {
Darryl Green11999bb2018-03-13 15:22:58 +00003589 MBEDTLS_SSL_DEBUG_MSG( 1,
3590 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003591 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003592 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3593 }
3594
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003595 ssl->in_left += ret;
3596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 }
3598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003600
3601 return( 0 );
3602}
3603
3604/*
3605 * Flush any data not yet written
3606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003608{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003609 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003610 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003613
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003614 if( ssl->f_send == NULL )
3615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003617 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003619 }
3620
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003621 /* Avoid incrementing counter if data is flushed */
3622 if( ssl->out_left == 0 )
3623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003625 return( 0 );
3626 }
3627
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 while( ssl->out_left > 0 )
3629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003631 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003632
Hanno Becker2b1e3542018-08-06 11:19:13 +01003633 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003634 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003637
3638 if( ret <= 0 )
3639 return( ret );
3640
mohammad160352aecb92018-03-28 23:41:40 -07003641 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003642 {
Darryl Green11999bb2018-03-13 15:22:58 +00003643 MBEDTLS_SSL_DEBUG_MSG( 1,
3644 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003645 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003646 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3647 }
3648
Paul Bakker5121ce52009-01-03 21:22:43 +00003649 ssl->out_left -= ret;
3650 }
3651
Hanno Becker2b1e3542018-08-06 11:19:13 +01003652#if defined(MBEDTLS_SSL_PROTO_DTLS)
3653 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003654 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003655 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003656 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003657 else
3658#endif
3659 {
3660 ssl->out_hdr = ssl->out_buf + 8;
3661 }
3662 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003665
3666 return( 0 );
3667}
3668
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003669/*
3670 * Functions to handle the DTLS retransmission state machine
3671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003673/*
3674 * Append current handshake message to current outgoing flight
3675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003677{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3680 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3681 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003682
3683 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003684 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003685 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003688 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003689 }
3690
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003691 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003692 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003695 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003696 }
3697
3698 /* Copy current handshake message with headers */
3699 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3700 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003701 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003702 msg->next = NULL;
3703
3704 /* Append to the current flight */
3705 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003706 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003707 else
3708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003710 while( cur->next != NULL )
3711 cur = cur->next;
3712 cur->next = msg;
3713 }
3714
Hanno Becker3b235902018-08-06 09:54:53 +01003715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003716 return( 0 );
3717}
3718
3719/*
3720 * Free the current flight of handshake messages
3721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003723{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 mbedtls_ssl_flight_item *cur = flight;
3725 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003726
3727 while( cur != NULL )
3728 {
3729 next = cur->next;
3730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731 mbedtls_free( cur->p );
3732 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003733
3734 cur = next;
3735 }
3736}
3737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3739static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003740#endif
3741
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003742/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003743 * Swap transform_out and out_ctr with the alternative ones
3744 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003746{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003748 unsigned char tmp_out_ctr[8];
3749
3750 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003753 return;
3754 }
3755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003757
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003758 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003759 tmp_transform = ssl->transform_out;
3760 ssl->transform_out = ssl->handshake->alt_transform_out;
3761 ssl->handshake->alt_transform_out = tmp_transform;
3762
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003763 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003764 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3765 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003766 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003767
3768 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003769 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3772 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3777 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003778 }
3779 }
3780#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003781}
3782
3783/*
3784 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003785 */
3786int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3787{
3788 int ret = 0;
3789
3790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3791
3792 ret = mbedtls_ssl_flight_transmit( ssl );
3793
3794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3795
3796 return( ret );
3797}
3798
3799/*
3800 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003801 *
3802 * Need to remember the current message in case flush_output returns
3803 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003804 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003805 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003806int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003807{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003808 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003812 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003814
3815 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003816 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003817 ssl_swap_epochs( ssl );
3818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003820 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003821
3822 while( ssl->handshake->cur_msg != NULL )
3823 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003824 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003825 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003826
Hanno Beckere1dcb032018-08-17 16:47:58 +01003827 int const is_finished =
3828 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3829 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3830
Hanno Becker04da1892018-08-14 13:22:10 +01003831 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3832 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3833
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003834 /* Swap epochs before sending Finished: we can't do it after
3835 * sending ChangeCipherSpec, in case write returns WANT_READ.
3836 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003837 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003838 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003840 ssl_swap_epochs( ssl );
3841 }
3842
Hanno Becker67bc7c32018-08-06 11:33:50 +01003843 ret = ssl_get_remaining_payload_in_datagram( ssl );
3844 if( ret < 0 )
3845 return( ret );
3846 max_frag_len = (size_t) ret;
3847
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003848 /* CCS is copied as is, while HS messages may need fragmentation */
3849 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3850 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003851 if( max_frag_len == 0 )
3852 {
3853 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3854 return( ret );
3855
3856 continue;
3857 }
3858
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003859 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003860 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003861 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003862
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003863 /* Update position inside current message */
3864 ssl->handshake->cur_msg_p += cur->len;
3865 }
3866 else
3867 {
3868 const unsigned char * const p = ssl->handshake->cur_msg_p;
3869 const size_t hs_len = cur->len - 12;
3870 const size_t frag_off = p - ( cur->p + 12 );
3871 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003872 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003873
Hanno Beckere1dcb032018-08-17 16:47:58 +01003874 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003875 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003876 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003877 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003878
Hanno Becker67bc7c32018-08-06 11:33:50 +01003879 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3880 return( ret );
3881
3882 continue;
3883 }
3884 max_hs_frag_len = max_frag_len - 12;
3885
3886 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3887 max_hs_frag_len : rem_len;
3888
3889 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003890 {
3891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003892 (unsigned) cur_hs_frag_len,
3893 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003894 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003895
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003896 /* Messages are stored with handshake headers as if not fragmented,
3897 * copy beginning of headers then fill fragmentation fields.
3898 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3899 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003900
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003901 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3902 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3903 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3904
Hanno Becker67bc7c32018-08-06 11:33:50 +01003905 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3906 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3907 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003908
3909 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3910
Hanno Becker3f7b9732018-08-28 09:53:25 +01003911 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003912 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3913 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003914 ssl->out_msgtype = cur->type;
3915
3916 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003917 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003918 }
3919
3920 /* If done with the current message move to the next one if any */
3921 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3922 {
3923 if( cur->next != NULL )
3924 {
3925 ssl->handshake->cur_msg = cur->next;
3926 ssl->handshake->cur_msg_p = cur->next->p + 12;
3927 }
3928 else
3929 {
3930 ssl->handshake->cur_msg = NULL;
3931 ssl->handshake->cur_msg_p = NULL;
3932 }
3933 }
3934
3935 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003936 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003939 return( ret );
3940 }
3941 }
3942
Hanno Becker67bc7c32018-08-06 11:33:50 +01003943 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3944 return( ret );
3945
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003946 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3948 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003949 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003952 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3953 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003954
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003956
3957 return( 0 );
3958}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003959
3960/*
3961 * To be called when the last message of an incoming flight is received.
3962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003964{
3965 /* We won't need to resend that one any more */
3966 ssl_flight_free( ssl->handshake->flight );
3967 ssl->handshake->flight = NULL;
3968 ssl->handshake->cur_msg = NULL;
3969
3970 /* The next incoming flight will start with this msg_seq */
3971 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3972
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003973 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003974 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003975
Hanno Becker0271f962018-08-16 13:23:47 +01003976 /* Clear future message buffering structure. */
3977 ssl_buffering_free( ssl );
3978
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003979 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003980 ssl_set_timer( ssl, 0 );
3981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3983 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003986 }
3987 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003989}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003990
3991/*
3992 * To be called when the last message of an outgoing flight is send.
3993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003995{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003996 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003997 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4000 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004003 }
4004 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004006}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004008
Paul Bakker5121ce52009-01-03 21:22:43 +00004009/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004010 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004011 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004012
4013/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004014 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004015 *
4016 * - fill in handshake headers
4017 * - update handshake checksum
4018 * - DTLS: save message for resending
4019 * - then pass to the record layer
4020 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004021 * DTLS: except for HelloRequest, messages are only queued, and will only be
4022 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004023 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004024 * Inputs:
4025 * - ssl->out_msglen: 4 + actual handshake message len
4026 * (4 is the size of handshake headers for TLS)
4027 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4028 * - ssl->out_msg + 4: the handshake message body
4029 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004030 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004031 * - ssl->out_msglen: the length of the record contents
4032 * (including handshake headers but excluding record headers)
4033 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004034 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004035int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004036{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004037 int ret;
4038 const size_t hs_len = ssl->out_msglen - 4;
4039 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004040
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4042
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004043 /*
4044 * Sanity checks
4045 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004046 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004047 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4048 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004049 /* In SSLv3, the client might send a NoCertificate alert. */
4050#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4051 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4052 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4053 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4054#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4055 {
4056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4058 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004059 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004060
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004061 /* Whenever we send anything different from a
4062 * HelloRequest we should be in a handshake - double check. */
4063 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4064 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004065 ssl->handshake == NULL )
4066 {
4067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004072 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004073 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004075 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4077 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004078 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004079#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004080
Hanno Beckerb50a2532018-08-06 11:52:54 +01004081 /* Double-check that we did not exceed the bounds
4082 * of the outgoing record buffer.
4083 * This should never fail as the various message
4084 * writing functions must obey the bounds of the
4085 * outgoing record buffer, but better be safe.
4086 *
4087 * Note: We deliberately do not check for the MTU or MFL here.
4088 */
4089 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4090 {
4091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4092 "size %u, maximum %u",
4093 (unsigned) ssl->out_msglen,
4094 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4095 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4096 }
4097
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004098 /*
4099 * Fill handshake headers
4100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004102 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004103 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4104 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4105 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004106
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004107 /*
4108 * DTLS has additional fields in the Handshake layer,
4109 * between the length field and the actual payload:
4110 * uint16 message_seq;
4111 * uint24 fragment_offset;
4112 * uint24 fragment_length;
4113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004115 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004116 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004117 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004118 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004119 {
4120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4121 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004122 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004123 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4125 }
4126
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004127 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004128 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004129
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004130 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004131 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004132 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004133 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4134 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4135 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004136 }
4137 else
4138 {
4139 ssl->out_msg[4] = 0;
4140 ssl->out_msg[5] = 0;
4141 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004142
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004143 /* Handshake hashes are computed without fragmentation,
4144 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004145 memset( ssl->out_msg + 6, 0x00, 3 );
4146 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004147 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004149
Hanno Becker0207e532018-08-28 10:28:28 +01004150 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004151 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4152 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004153 }
4154
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004155 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004157 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004158 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4159 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004160 {
4161 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004164 return( ret );
4165 }
4166 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004167 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004168#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004169 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004170 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004171 {
4172 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4173 return( ret );
4174 }
4175 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004176
4177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4178
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004179 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004180}
4181
4182/*
4183 * Record layer functions
4184 */
4185
4186/*
4187 * Write current record.
4188 *
4189 * Uses:
4190 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4191 * - ssl->out_msglen: length of the record content (excl headers)
4192 * - ssl->out_msg: record content
4193 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004194int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004195{
4196 int ret, done = 0;
4197 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004198 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004199
4200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004203 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004205 {
4206 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004209 return( ret );
4210 }
4211
4212 len = ssl->out_msglen;
4213 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4217 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221 ret = mbedtls_ssl_hw_record_write( ssl );
4222 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4225 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004226 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004227
4228 if( ret == 0 )
4229 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004230 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004232 if( !done )
4233 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004234 unsigned i;
4235 size_t protected_record_size;
4236
Hanno Becker6430faf2019-05-08 11:57:13 +01004237 /* Skip writing the record content type to after the encryption,
4238 * as it may change when using the CID extension. */
4239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004241 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004242
Hanno Becker19859472018-08-06 09:40:20 +01004243 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004244 ssl->out_len[0] = (unsigned char)( len >> 8 );
4245 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004246
Paul Bakker48916f92012-09-16 19:57:18 +00004247 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004248 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004249 mbedtls_record rec;
4250
4251 rec.buf = ssl->out_iv;
4252 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4253 ( ssl->out_iv - ssl->out_buf );
4254 rec.data_len = ssl->out_msglen;
4255 rec.data_offset = ssl->out_msg - rec.buf;
4256
4257 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4258 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4259 ssl->conf->transport, rec.ver );
4260 rec.type = ssl->out_msgtype;
4261
Hanno Beckera0e20d02019-05-15 14:03:01 +01004262#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004263 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004264 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004265#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004266
Hanno Beckera18d1322018-01-03 14:27:32 +00004267 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004268 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004271 return( ret );
4272 }
4273
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004274 if( rec.data_offset != 0 )
4275 {
4276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4277 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4278 }
4279
Hanno Becker6430faf2019-05-08 11:57:13 +01004280 /* Update the record content type and CID. */
4281 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004282#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004283 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004284#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004285 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004286 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4287 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004288 }
4289
Hanno Becker5903de42019-05-03 14:46:38 +01004290 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004291
4292#if defined(MBEDTLS_SSL_PROTO_DTLS)
4293 /* In case of DTLS, double-check that we don't exceed
4294 * the remaining space in the datagram. */
4295 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4296 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004297 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004298 if( ret < 0 )
4299 return( ret );
4300
4301 if( protected_record_size > (size_t) ret )
4302 {
4303 /* Should never happen */
4304 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4305 }
4306 }
4307#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004308
Hanno Becker6430faf2019-05-08 11:57:13 +01004309 /* Now write the potentially updated record content type. */
4310 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004313 "version = [%d:%d], msglen = %d",
4314 ssl->out_hdr[0], ssl->out_hdr[1],
4315 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004318 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004319
4320 ssl->out_left += protected_record_size;
4321 ssl->out_hdr += protected_record_size;
4322 ssl_update_out_pointers( ssl, ssl->transform_out );
4323
Hanno Becker04484622018-08-06 09:49:38 +01004324 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4325 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4326 break;
4327
4328 /* The loop goes to its end iff the counter is wrapping */
4329 if( i == ssl_ep_len( ssl ) )
4330 {
4331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4332 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4333 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004334 }
4335
Hanno Becker67bc7c32018-08-06 11:33:50 +01004336#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004337 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4338 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004339 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004340 size_t remaining;
4341 ret = ssl_get_remaining_payload_in_datagram( ssl );
4342 if( ret < 0 )
4343 {
4344 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4345 ret );
4346 return( ret );
4347 }
4348
4349 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004350 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004351 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004352 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004353 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004354 else
4355 {
Hanno Becker513815a2018-08-20 11:56:09 +01004356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004357 }
4358 }
4359#endif /* MBEDTLS_SSL_PROTO_DTLS */
4360
4361 if( ( flush == SSL_FORCE_FLUSH ) &&
4362 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004365 return( ret );
4366 }
4367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004369
4370 return( 0 );
4371}
4372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004373#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004374
4375static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4376{
4377 if( ssl->in_msglen < ssl->in_hslen ||
4378 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4379 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4380 {
4381 return( 1 );
4382 }
4383 return( 0 );
4384}
Hanno Becker44650b72018-08-16 12:51:11 +01004385
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004386static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004387{
4388 return( ( ssl->in_msg[9] << 16 ) |
4389 ( ssl->in_msg[10] << 8 ) |
4390 ssl->in_msg[11] );
4391}
4392
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004393static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004394{
4395 return( ( ssl->in_msg[6] << 16 ) |
4396 ( ssl->in_msg[7] << 8 ) |
4397 ssl->in_msg[8] );
4398}
4399
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004400static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004401{
4402 uint32_t msg_len, frag_off, frag_len;
4403
4404 msg_len = ssl_get_hs_total_len( ssl );
4405 frag_off = ssl_get_hs_frag_off( ssl );
4406 frag_len = ssl_get_hs_frag_len( ssl );
4407
4408 if( frag_off > msg_len )
4409 return( -1 );
4410
4411 if( frag_len > msg_len - frag_off )
4412 return( -1 );
4413
4414 if( frag_len + 12 > ssl->in_msglen )
4415 return( -1 );
4416
4417 return( 0 );
4418}
4419
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004420/*
4421 * Mark bits in bitmask (used for DTLS HS reassembly)
4422 */
4423static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4424{
4425 unsigned int start_bits, end_bits;
4426
4427 start_bits = 8 - ( offset % 8 );
4428 if( start_bits != 8 )
4429 {
4430 size_t first_byte_idx = offset / 8;
4431
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004432 /* Special case */
4433 if( len <= start_bits )
4434 {
4435 for( ; len != 0; len-- )
4436 mask[first_byte_idx] |= 1 << ( start_bits - len );
4437
4438 /* Avoid potential issues with offset or len becoming invalid */
4439 return;
4440 }
4441
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004442 offset += start_bits; /* Now offset % 8 == 0 */
4443 len -= start_bits;
4444
4445 for( ; start_bits != 0; start_bits-- )
4446 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4447 }
4448
4449 end_bits = len % 8;
4450 if( end_bits != 0 )
4451 {
4452 size_t last_byte_idx = ( offset + len ) / 8;
4453
4454 len -= end_bits; /* Now len % 8 == 0 */
4455
4456 for( ; end_bits != 0; end_bits-- )
4457 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4458 }
4459
4460 memset( mask + offset / 8, 0xFF, len / 8 );
4461}
4462
4463/*
4464 * Check that bitmask is full
4465 */
4466static int ssl_bitmask_check( unsigned char *mask, size_t len )
4467{
4468 size_t i;
4469
4470 for( i = 0; i < len / 8; i++ )
4471 if( mask[i] != 0xFF )
4472 return( -1 );
4473
4474 for( i = 0; i < len % 8; i++ )
4475 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4476 return( -1 );
4477
4478 return( 0 );
4479}
4480
Hanno Becker56e205e2018-08-16 09:06:12 +01004481/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004482static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004483 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004484{
Hanno Becker56e205e2018-08-16 09:06:12 +01004485 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004486
Hanno Becker56e205e2018-08-16 09:06:12 +01004487 alloc_len = 12; /* Handshake header */
4488 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004489
Hanno Beckerd07df862018-08-16 09:14:58 +01004490 if( add_bitmap )
4491 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004492
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004493 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004494}
Hanno Becker56e205e2018-08-16 09:06:12 +01004495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004497
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004498static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004499{
4500 return( ( ssl->in_msg[1] << 16 ) |
4501 ( ssl->in_msg[2] << 8 ) |
4502 ssl->in_msg[3] );
4503}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004504
Simon Butcher99000142016-10-13 17:21:01 +01004505int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004510 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004512 }
4513
Hanno Becker12555c62018-08-16 12:47:53 +01004514 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004517 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004518 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004520#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004521 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004522 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004523 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004524 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004525
Hanno Becker44650b72018-08-16 12:51:11 +01004526 if( ssl_check_hs_header( ssl ) != 0 )
4527 {
4528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4529 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4530 }
4531
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004532 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004533 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4534 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4535 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4536 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004537 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004538 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4539 {
4540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4541 recv_msg_seq,
4542 ssl->handshake->in_msg_seq ) );
4543 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4544 }
4545
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004546 /* Retransmit only on last message from previous flight, to avoid
4547 * too many retransmissions.
4548 * Besides, No sane server ever retransmits HelloVerifyRequest */
4549 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004553 "message_seq = %d, start_of_flight = %d",
4554 recv_msg_seq,
4555 ssl->handshake->in_flight_start_seq ) );
4556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004560 return( ret );
4561 }
4562 }
4563 else
4564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004566 "message_seq = %d, expected = %d",
4567 recv_msg_seq,
4568 ssl->handshake->in_msg_seq ) );
4569 }
4570
Hanno Becker90333da2017-10-10 11:27:13 +01004571 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004572 }
4573 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004574
Hanno Becker6d97ef52018-08-16 13:09:04 +01004575 /* Message reassembly is handled alongside buffering of future
4576 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004577 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004578 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004579 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004582 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004583 }
4584 }
4585 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004587 /* With TLS we don't handle fragmentation (for now) */
4588 if( ssl->in_msglen < ssl->in_hslen )
4589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4591 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004592 }
4593
Simon Butcher99000142016-10-13 17:21:01 +01004594 return( 0 );
4595}
4596
4597void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4598{
Hanno Becker0271f962018-08-16 13:23:47 +01004599 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004600
Hanno Becker0271f962018-08-16 13:23:47 +01004601 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004602 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004603 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004604 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004605
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004606 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004607#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004608 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004609 ssl->handshake != NULL )
4610 {
Hanno Becker0271f962018-08-16 13:23:47 +01004611 unsigned offset;
4612 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004613
Hanno Becker0271f962018-08-16 13:23:47 +01004614 /* Increment handshake sequence number */
4615 hs->in_msg_seq++;
4616
4617 /*
4618 * Clear up handshake buffering and reassembly structure.
4619 */
4620
4621 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004622 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004623
4624 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004625 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4626 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004627 offset++, hs_buf++ )
4628 {
4629 *hs_buf = *(hs_buf + 1);
4630 }
4631
4632 /* Create a fresh last entry */
4633 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004634 }
4635#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004636}
4637
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004638/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004639 * DTLS anti-replay: RFC 6347 4.1.2.6
4640 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004641 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4642 * Bit n is set iff record number in_window_top - n has been seen.
4643 *
4644 * Usually, in_window_top is the last record number seen and the lsb of
4645 * in_window is set. The only exception is the initial state (record number 0
4646 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4649static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004650{
4651 ssl->in_window_top = 0;
4652 ssl->in_window = 0;
4653}
4654
4655static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4656{
4657 return( ( (uint64_t) buf[0] << 40 ) |
4658 ( (uint64_t) buf[1] << 32 ) |
4659 ( (uint64_t) buf[2] << 24 ) |
4660 ( (uint64_t) buf[3] << 16 ) |
4661 ( (uint64_t) buf[4] << 8 ) |
4662 ( (uint64_t) buf[5] ) );
4663}
4664
4665/*
4666 * Return 0 if sequence number is acceptable, -1 otherwise
4667 */
Hanno Becker0183d692019-07-12 08:50:37 +01004668int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004669{
4670 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4671 uint64_t bit;
4672
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004673 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004674 return( 0 );
4675
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004676 if( rec_seqnum > ssl->in_window_top )
4677 return( 0 );
4678
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004679 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004680
4681 if( bit >= 64 )
4682 return( -1 );
4683
4684 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4685 return( -1 );
4686
4687 return( 0 );
4688}
4689
4690/*
4691 * Update replay window on new validated record
4692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004694{
4695 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4696
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004697 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004698 return;
4699
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004700 if( rec_seqnum > ssl->in_window_top )
4701 {
4702 /* Update window_top and the contents of the window */
4703 uint64_t shift = rec_seqnum - ssl->in_window_top;
4704
4705 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004706 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004707 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004708 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004709 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004710 ssl->in_window |= 1;
4711 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004712
4713 ssl->in_window_top = rec_seqnum;
4714 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004715 else
4716 {
4717 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004718 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004719
4720 if( bit < 64 ) /* Always true, but be extra sure */
4721 ssl->in_window |= (uint64_t) 1 << bit;
4722 }
4723}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004725
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004726#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004727/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004728static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4729
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004730/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004731 * Without any SSL context, check if a datagram looks like a ClientHello with
4732 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004733 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004734 *
4735 * - if cookie is valid, return 0
4736 * - if ClientHello looks superficially valid but cookie is not,
4737 * fill obuf and set olen, then
4738 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4739 * - otherwise return a specific error code
4740 */
4741static int ssl_check_dtls_clihlo_cookie(
4742 mbedtls_ssl_cookie_write_t *f_cookie_write,
4743 mbedtls_ssl_cookie_check_t *f_cookie_check,
4744 void *p_cookie,
4745 const unsigned char *cli_id, size_t cli_id_len,
4746 const unsigned char *in, size_t in_len,
4747 unsigned char *obuf, size_t buf_len, size_t *olen )
4748{
4749 size_t sid_len, cookie_len;
4750 unsigned char *p;
4751
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004752 /*
4753 * Structure of ClientHello with record and handshake headers,
4754 * and expected values. We don't need to check a lot, more checks will be
4755 * done when actually parsing the ClientHello - skipping those checks
4756 * avoids code duplication and does not make cookie forging any easier.
4757 *
4758 * 0-0 ContentType type; copied, must be handshake
4759 * 1-2 ProtocolVersion version; copied
4760 * 3-4 uint16 epoch; copied, must be 0
4761 * 5-10 uint48 sequence_number; copied
4762 * 11-12 uint16 length; (ignored)
4763 *
4764 * 13-13 HandshakeType msg_type; (ignored)
4765 * 14-16 uint24 length; (ignored)
4766 * 17-18 uint16 message_seq; copied
4767 * 19-21 uint24 fragment_offset; copied, must be 0
4768 * 22-24 uint24 fragment_length; (ignored)
4769 *
4770 * 25-26 ProtocolVersion client_version; (ignored)
4771 * 27-58 Random random; (ignored)
4772 * 59-xx SessionID session_id; 1 byte len + sid_len content
4773 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4774 * ...
4775 *
4776 * Minimum length is 61 bytes.
4777 */
4778 if( in_len < 61 ||
4779 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4780 in[3] != 0 || in[4] != 0 ||
4781 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4782 {
4783 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4784 }
4785
4786 sid_len = in[59];
4787 if( sid_len > in_len - 61 )
4788 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4789
4790 cookie_len = in[60 + sid_len];
4791 if( cookie_len > in_len - 60 )
4792 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4793
4794 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4795 cli_id, cli_id_len ) == 0 )
4796 {
4797 /* Valid cookie */
4798 return( 0 );
4799 }
4800
4801 /*
4802 * If we get here, we've got an invalid cookie, let's prepare HVR.
4803 *
4804 * 0-0 ContentType type; copied
4805 * 1-2 ProtocolVersion version; copied
4806 * 3-4 uint16 epoch; copied
4807 * 5-10 uint48 sequence_number; copied
4808 * 11-12 uint16 length; olen - 13
4809 *
4810 * 13-13 HandshakeType msg_type; hello_verify_request
4811 * 14-16 uint24 length; olen - 25
4812 * 17-18 uint16 message_seq; copied
4813 * 19-21 uint24 fragment_offset; copied
4814 * 22-24 uint24 fragment_length; olen - 25
4815 *
4816 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4817 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4818 *
4819 * Minimum length is 28.
4820 */
4821 if( buf_len < 28 )
4822 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4823
4824 /* Copy most fields and adapt others */
4825 memcpy( obuf, in, 25 );
4826 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4827 obuf[25] = 0xfe;
4828 obuf[26] = 0xff;
4829
4830 /* Generate and write actual cookie */
4831 p = obuf + 28;
4832 if( f_cookie_write( p_cookie,
4833 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4834 {
4835 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4836 }
4837
4838 *olen = p - obuf;
4839
4840 /* Go back and fill length fields */
4841 obuf[27] = (unsigned char)( *olen - 28 );
4842
4843 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4844 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4845 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4846
4847 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4848 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4849
4850 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4851}
4852
4853/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004854 * Handle possible client reconnect with the same UDP quadruplet
4855 * (RFC 6347 Section 4.2.8).
4856 *
4857 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4858 * that looks like a ClientHello.
4859 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004860 * - if the input looks like a ClientHello without cookies,
4861 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004862 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004863 * - if the input looks like a ClientHello with a valid cookie,
4864 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004865 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004866 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004867 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004868 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004869 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4870 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004871 */
4872static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4873{
4874 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004875 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004876
Hanno Becker2fddd372019-07-10 14:37:41 +01004877 if( ssl->conf->f_cookie_write == NULL ||
4878 ssl->conf->f_cookie_check == NULL )
4879 {
4880 /* If we can't use cookies to verify reachability of the peer,
4881 * drop the record. */
4882 return( 0 );
4883 }
4884
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004885 ret = ssl_check_dtls_clihlo_cookie(
4886 ssl->conf->f_cookie_write,
4887 ssl->conf->f_cookie_check,
4888 ssl->conf->p_cookie,
4889 ssl->cli_id, ssl->cli_id_len,
4890 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004891 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004892
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004893 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4894
4895 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004896 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004897 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004898 * If the error is permanent we'll catch it later,
4899 * if it's not, then hopefully it'll work next time. */
4900 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004901 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004902 }
4903
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004904 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004905 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004906 /* Got a valid cookie, partially reset context */
4907 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4908 {
4909 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4910 return( ret );
4911 }
4912
4913 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004914 }
4915
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004916 return( ret );
4917}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004918#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004919
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004920static int ssl_check_record_type( uint8_t record_type )
4921{
4922 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4923 record_type != MBEDTLS_SSL_MSG_ALERT &&
4924 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4925 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4926 {
4927 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4928 }
4929
4930 return( 0 );
4931}
4932
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004933/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004934 * ContentType type;
4935 * ProtocolVersion version;
4936 * uint16 epoch; // DTLS only
4937 * uint48 sequence_number; // DTLS only
4938 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004939 *
4940 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004941 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004942 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4943 *
4944 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004945 * 1. proceed with the record if this function returns 0
4946 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4947 * 3. return CLIENT_RECONNECT if this function return that value
4948 * 4. drop the whole datagram if this function returns anything else.
4949 * Point 2 is needed when the peer is resending, and we have already received
4950 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004951 */
Hanno Becker331de3d2019-07-12 11:10:16 +01004952static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01004953 unsigned char *buf,
4954 size_t len,
4955 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00004956{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004957 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004958
Hanno Beckere5e7e782019-07-11 12:29:35 +01004959 size_t const rec_hdr_type_offset = 0;
4960 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004961
Hanno Beckere5e7e782019-07-11 12:29:35 +01004962 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
4963 rec_hdr_type_len;
4964 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00004965
Hanno Beckere5e7e782019-07-11 12:29:35 +01004966 size_t const rec_hdr_ctr_len = 8;
4967#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01004968 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004969 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
4970 rec_hdr_version_len;
4971
Hanno Beckera0e20d02019-05-15 14:03:01 +01004972#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01004973 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
4974 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01004975 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004976#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4977#endif /* MBEDTLS_SSL_PROTO_DTLS */
4978
4979 size_t rec_hdr_len_offset; /* To be determined */
4980 size_t const rec_hdr_len_len = 2;
4981
4982 /*
4983 * Check minimum lengths for record header.
4984 */
4985
4986#if defined(MBEDTLS_SSL_PROTO_DTLS)
4987 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4988 {
4989 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
4990 }
4991 else
4992#endif /* MBEDTLS_SSL_PROTO_DTLS */
4993 {
4994 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
4995 }
4996
4997 if( len < rec_hdr_len_offset + rec_hdr_len_len )
4998 {
4999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5000 (unsigned) len,
5001 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5002 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5003 }
5004
5005 /*
5006 * Parse and validate record content type
5007 */
5008
5009 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005010
5011 /* Check record content type */
5012#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5013 rec->cid_len = 0;
5014
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005015 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005016 ssl->conf->cid_len != 0 &&
5017 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005018 {
5019 /* Shift pointers to account for record header including CID
5020 * struct {
5021 * ContentType special_type = tls12_cid;
5022 * ProtocolVersion version;
5023 * uint16 epoch;
5024 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005025 * opaque cid[cid_length]; // Additional field compared to
5026 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005027 * uint16 length;
5028 * opaque enc_content[DTLSCiphertext.length];
5029 * } DTLSCiphertext;
5030 */
5031
5032 /* So far, we only support static CID lengths
5033 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005034 rec_hdr_cid_len = ssl->conf->cid_len;
5035 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005036
Hanno Beckere5e7e782019-07-11 12:29:35 +01005037 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005038 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5040 (unsigned) len,
5041 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005042 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005043 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005044
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005045 /* configured CID len is guaranteed at most 255, see
5046 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5047 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005048 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005049 }
5050 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005051#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005052 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005053 if( ssl_check_record_type( rec->type ) )
5054 {
Hanno Becker54229812019-07-12 14:40:00 +01005055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5056 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005057 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5058 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005059 }
5060
Hanno Beckere5e7e782019-07-11 12:29:35 +01005061 /*
5062 * Parse and validate record version
5063 */
5064
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005065 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5066 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005067 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5068 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005069 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005070
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005071 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5074 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005075 }
5076
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005077 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5080 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005081 }
5082
Hanno Beckere5e7e782019-07-11 12:29:35 +01005083 /*
5084 * Parse/Copy record sequence number.
5085 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005086
Hanno Beckere5e7e782019-07-11 12:29:35 +01005087#if defined(MBEDTLS_SSL_PROTO_DTLS)
5088 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005089 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005090 /* Copy explicit record sequence number from input buffer. */
5091 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5092 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005093 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005094 else
5095#endif /* MBEDTLS_SSL_PROTO_DTLS */
5096 {
5097 /* Copy implicit record sequence number from SSL context structure. */
5098 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5099 }
Paul Bakker40e46942009-01-03 21:51:57 +00005100
Hanno Beckere5e7e782019-07-11 12:29:35 +01005101 /*
5102 * Parse record length.
5103 */
5104
Hanno Beckere5e7e782019-07-11 12:29:35 +01005105 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005106 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5107 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005108 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005109
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005110 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005111 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005112 rec->type,
5113 major_ver, minor_ver, rec->data_len ) );
5114
5115 rec->buf = buf;
5116 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005117
Hanno Beckerd417cc92019-07-26 08:20:27 +01005118 if( rec->data_len == 0 )
5119 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005120
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005121 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005122 * DTLS-related tests.
5123 * Check epoch before checking length constraint because
5124 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5125 * message gets duplicated before the corresponding Finished message,
5126 * the second ChangeCipherSpec should be discarded because it belongs
5127 * to an old epoch, but not because its length is shorter than
5128 * the minimum record length for packets using the new record transform.
5129 * Note that these two kinds of failures are handled differently,
5130 * as an unexpected record is silently skipped but an invalid
5131 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005132 */
5133#if defined(MBEDTLS_SSL_PROTO_DTLS)
5134 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5135 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005136 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005137
Hanno Becker955a5c92019-07-10 17:12:07 +01005138 /* Check that the datagram is large enough to contain a record
5139 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005140 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005141 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5143 (unsigned) len,
5144 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005145 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5146 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005147
Hanno Becker37cfe732019-07-10 17:20:01 +01005148 /* Records from other, non-matching epochs are silently discarded.
5149 * (The case of same-port Client reconnects must be considered in
5150 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005151 if( rec_epoch != ssl->in_epoch )
5152 {
5153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5154 "expected %d, received %d",
5155 ssl->in_epoch, rec_epoch ) );
5156
Hanno Becker552f7472019-07-19 10:59:12 +01005157 /* Records from the next epoch are considered for buffering
5158 * (concretely: early Finished messages). */
5159 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005160 {
Hanno Becker552f7472019-07-19 10:59:12 +01005161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5162 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005163 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005164
Hanno Becker2fddd372019-07-10 14:37:41 +01005165 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005166 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005167#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005168 /* For records from the correct epoch, check whether their
5169 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005170 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005171 {
5172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5173 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5174 }
5175#endif
5176 }
5177#endif /* MBEDTLS_SSL_PROTO_DTLS */
5178
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005179 return( 0 );
5180}
Paul Bakker5121ce52009-01-03 21:22:43 +00005181
Paul Bakker5121ce52009-01-03 21:22:43 +00005182
Hanno Becker2fddd372019-07-10 14:37:41 +01005183#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5184static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5185{
5186 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5187
5188 /*
5189 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5190 * access the first byte of record content (handshake type), as we
5191 * have an active transform (possibly iv_len != 0), so use the
5192 * fact that the record header len is 13 instead.
5193 */
5194 if( rec_epoch == 0 &&
5195 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5196 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5197 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5198 ssl->in_left > 13 &&
5199 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5200 {
5201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5202 "from the same port" ) );
5203 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005204 }
5205
5206 return( 0 );
5207}
Hanno Becker2fddd372019-07-10 14:37:41 +01005208#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005209
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005210/*
5211 * If applicable, decrypt (and decompress) record content
5212 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005213static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5214 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005215{
5216 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005219 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5222 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226 ret = mbedtls_ssl_hw_record_read( ssl );
5227 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5230 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005231 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005232
5233 if( ret == 0 )
5234 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005235 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005236#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005237 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005238 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005239 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005240
Hanno Beckera18d1322018-01-03 14:27:32 +00005241 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005242 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005245
Hanno Beckera0e20d02019-05-15 14:03:01 +01005246#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005247 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5248 ssl->conf->ignore_unexpected_cid
5249 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5250 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005252 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005253 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005254#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005255
Paul Bakker5121ce52009-01-03 21:22:43 +00005256 return( ret );
5257 }
5258
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005259 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005260 {
5261 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005262 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005263 }
5264
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005265 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005266 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005267
Hanno Beckera0e20d02019-05-15 14:03:01 +01005268#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005269 /* We have already checked the record content type
5270 * in ssl_parse_record_header(), failing or silently
5271 * dropping the record in the case of an unknown type.
5272 *
5273 * Since with the use of CIDs, the record content type
5274 * might change during decryption, re-check the record
5275 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005276 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005277 {
5278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5279 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5280 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005281#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005282
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005283 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005284 {
5285#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5286 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005287 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005288 {
5289 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5291 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5292 }
5293#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5294
5295 ssl->nb_zero++;
5296
5297 /*
5298 * Three or more empty messages may be a DoS attack
5299 * (excessive CPU consumption).
5300 */
5301 if( ssl->nb_zero > 3 )
5302 {
5303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005304 "messages, possible DoS attack" ) );
5305 /* Treat the records as if they were not properly authenticated,
5306 * thereby failing the connection if we see more than allowed
5307 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005308 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5309 }
5310 }
5311 else
5312 ssl->nb_zero = 0;
5313
5314#if defined(MBEDTLS_SSL_PROTO_DTLS)
5315 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5316 {
5317 ; /* in_ctr read from peer, not maintained internally */
5318 }
5319 else
5320#endif
5321 {
5322 unsigned i;
5323 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5324 if( ++ssl->in_ctr[i - 1] != 0 )
5325 break;
5326
5327 /* The loop goes to its end iff the counter is wrapping */
5328 if( i == ssl_ep_len( ssl ) )
5329 {
5330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5331 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5332 }
5333 }
5334
Paul Bakker5121ce52009-01-03 21:22:43 +00005335 }
5336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005338 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005340 {
5341 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005344 return( ret );
5345 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005346 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005350 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005353 }
5354#endif
5355
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005356 /* Check actual (decrypted) record content length against
5357 * configured maximum. */
5358 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5359 {
5360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5361 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5362 }
5363
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005364 return( 0 );
5365}
5366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005368
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005369/*
5370 * Read a record.
5371 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005372 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5373 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5374 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005375 */
Hanno Becker1097b342018-08-15 14:09:41 +01005376
5377/* Helper functions for mbedtls_ssl_read_record(). */
5378static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005379static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5380static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005381
Hanno Becker327c93b2018-08-15 13:56:18 +01005382int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005383 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005384{
5385 int ret;
5386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005388
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005389 if( ssl->keep_current_message == 0 )
5390 {
5391 do {
Simon Butcher99000142016-10-13 17:21:01 +01005392
Hanno Becker26994592018-08-15 14:14:59 +01005393 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005394 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005395 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005396
Hanno Beckere74d5562018-08-15 14:26:08 +01005397 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005398 {
Hanno Becker40f50842018-08-15 14:48:01 +01005399#if defined(MBEDTLS_SSL_PROTO_DTLS)
5400 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005401
Hanno Becker40f50842018-08-15 14:48:01 +01005402 /* We only check for buffered messages if the
5403 * current datagram is fully consumed. */
5404 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005405 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005406 {
Hanno Becker40f50842018-08-15 14:48:01 +01005407 if( ssl_load_buffered_message( ssl ) == 0 )
5408 have_buffered = 1;
5409 }
5410
5411 if( have_buffered == 0 )
5412#endif /* MBEDTLS_SSL_PROTO_DTLS */
5413 {
5414 ret = ssl_get_next_record( ssl );
5415 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5416 continue;
5417
5418 if( ret != 0 )
5419 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005420 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005421 return( ret );
5422 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005423 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005424 }
5425
5426 ret = mbedtls_ssl_handle_message_type( ssl );
5427
Hanno Becker40f50842018-08-15 14:48:01 +01005428#if defined(MBEDTLS_SSL_PROTO_DTLS)
5429 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5430 {
5431 /* Buffer future message */
5432 ret = ssl_buffer_message( ssl );
5433 if( ret != 0 )
5434 return( ret );
5435
5436 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5437 }
5438#endif /* MBEDTLS_SSL_PROTO_DTLS */
5439
Hanno Becker90333da2017-10-10 11:27:13 +01005440 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5441 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005442
5443 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005444 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005445 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005446 return( ret );
5447 }
5448
Hanno Becker327c93b2018-08-15 13:56:18 +01005449 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005450 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005451 {
5452 mbedtls_ssl_update_handshake_status( ssl );
5453 }
Simon Butcher99000142016-10-13 17:21:01 +01005454 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005455 else
Simon Butcher99000142016-10-13 17:21:01 +01005456 {
Hanno Becker02f59072018-08-15 14:00:24 +01005457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005458 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005459 }
5460
5461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5462
5463 return( 0 );
5464}
5465
Hanno Becker40f50842018-08-15 14:48:01 +01005466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005467static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005468{
Hanno Becker40f50842018-08-15 14:48:01 +01005469 if( ssl->in_left > ssl->next_record_offset )
5470 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005471
Hanno Becker40f50842018-08-15 14:48:01 +01005472 return( 0 );
5473}
5474
5475static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5476{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005477 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005478 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005479 int ret = 0;
5480
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005481 if( hs == NULL )
5482 return( -1 );
5483
Hanno Beckere00ae372018-08-20 09:39:42 +01005484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5485
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005486 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5487 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5488 {
5489 /* Check if we have seen a ChangeCipherSpec before.
5490 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005491 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005492 {
5493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5494 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005495 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005496 }
5497
Hanno Becker39b8bc92018-08-28 17:17:13 +01005498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005499 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5500 ssl->in_msglen = 1;
5501 ssl->in_msg[0] = 1;
5502
5503 /* As long as they are equal, the exact value doesn't matter. */
5504 ssl->in_left = 0;
5505 ssl->next_record_offset = 0;
5506
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005507 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005508 goto exit;
5509 }
Hanno Becker37f95322018-08-16 13:55:32 +01005510
Hanno Beckerb8f50142018-08-28 10:01:34 +01005511#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005512 /* Debug only */
5513 {
5514 unsigned offset;
5515 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5516 {
5517 hs_buf = &hs->buffering.hs[offset];
5518 if( hs_buf->is_valid == 1 )
5519 {
5520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5521 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005522 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005523 }
5524 }
5525 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005526#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005527
5528 /* Check if we have buffered and/or fully reassembled the
5529 * next handshake message. */
5530 hs_buf = &hs->buffering.hs[0];
5531 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5532 {
5533 /* Synthesize a record containing the buffered HS message. */
5534 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5535 ( hs_buf->data[2] << 8 ) |
5536 hs_buf->data[3];
5537
5538 /* Double-check that we haven't accidentally buffered
5539 * a message that doesn't fit into the input buffer. */
5540 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5541 {
5542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5544 }
5545
5546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5547 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5548 hs_buf->data, msg_len + 12 );
5549
5550 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5551 ssl->in_hslen = msg_len + 12;
5552 ssl->in_msglen = msg_len + 12;
5553 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5554
5555 ret = 0;
5556 goto exit;
5557 }
5558 else
5559 {
5560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5561 hs->in_msg_seq ) );
5562 }
5563
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005564 ret = -1;
5565
5566exit:
5567
5568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5569 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005570}
5571
Hanno Beckera02b0b42018-08-21 17:20:27 +01005572static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5573 size_t desired )
5574{
5575 int offset;
5576 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5578 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005579
Hanno Becker01315ea2018-08-21 17:22:17 +01005580 /* Get rid of future records epoch first, if such exist. */
5581 ssl_free_buffered_record( ssl );
5582
5583 /* Check if we have enough space available now. */
5584 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5585 hs->buffering.total_bytes_buffered ) )
5586 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005588 return( 0 );
5589 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005590
Hanno Becker4f432ad2018-08-28 10:02:32 +01005591 /* We don't have enough space to buffer the next expected handshake
5592 * message. Remove buffers used for future messages to gain space,
5593 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005594 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5595 offset >= 0; offset-- )
5596 {
5597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5598 offset ) );
5599
Hanno Beckerb309b922018-08-23 13:18:05 +01005600 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005601
5602 /* Check if we have enough space available now. */
5603 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5604 hs->buffering.total_bytes_buffered ) )
5605 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005607 return( 0 );
5608 }
5609 }
5610
5611 return( -1 );
5612}
5613
Hanno Becker40f50842018-08-15 14:48:01 +01005614static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5615{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005616 int ret = 0;
5617 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5618
5619 if( hs == NULL )
5620 return( 0 );
5621
5622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5623
5624 switch( ssl->in_msgtype )
5625 {
5626 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005628
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005629 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005630 break;
5631
5632 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005633 {
5634 unsigned recv_msg_seq_offset;
5635 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5636 mbedtls_ssl_hs_buffer *hs_buf;
5637 size_t msg_len = ssl->in_hslen - 12;
5638
5639 /* We should never receive an old handshake
5640 * message - double-check nonetheless. */
5641 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5642 {
5643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5645 }
5646
5647 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5648 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5649 {
5650 /* Silently ignore -- message too far in the future */
5651 MBEDTLS_SSL_DEBUG_MSG( 2,
5652 ( "Ignore future HS message with sequence number %u, "
5653 "buffering window %u - %u",
5654 recv_msg_seq, ssl->handshake->in_msg_seq,
5655 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5656
5657 goto exit;
5658 }
5659
5660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5661 recv_msg_seq, recv_msg_seq_offset ) );
5662
5663 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5664
5665 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005666 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005667 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005668 size_t reassembly_buf_sz;
5669
Hanno Becker37f95322018-08-16 13:55:32 +01005670 hs_buf->is_fragmented =
5671 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5672
5673 /* We copy the message back into the input buffer
5674 * after reassembly, so check that it's not too large.
5675 * This is an implementation-specific limitation
5676 * and not one from the standard, hence it is not
5677 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005678 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005679 {
5680 /* Ignore message */
5681 goto exit;
5682 }
5683
Hanno Beckere0b150f2018-08-21 15:51:03 +01005684 /* Check if we have enough space to buffer the message. */
5685 if( hs->buffering.total_bytes_buffered >
5686 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5687 {
5688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5690 }
5691
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005692 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5693 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005694
5695 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5696 hs->buffering.total_bytes_buffered ) )
5697 {
5698 if( recv_msg_seq_offset > 0 )
5699 {
5700 /* If we can't buffer a future message because
5701 * of space limitations -- ignore. */
5702 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",
5703 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5704 (unsigned) hs->buffering.total_bytes_buffered ) );
5705 goto exit;
5706 }
Hanno Beckere1801392018-08-21 16:51:05 +01005707 else
5708 {
5709 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",
5710 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5711 (unsigned) hs->buffering.total_bytes_buffered ) );
5712 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005713
Hanno Beckera02b0b42018-08-21 17:20:27 +01005714 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005715 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005716 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",
5717 (unsigned) msg_len,
5718 (unsigned) reassembly_buf_sz,
5719 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005720 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005721 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5722 goto exit;
5723 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005724 }
5725
5726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5727 msg_len ) );
5728
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005729 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5730 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005731 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005732 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005733 goto exit;
5734 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005735 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005736
5737 /* Prepare final header: copy msg_type, length and message_seq,
5738 * then add standardised fragment_offset and fragment_length */
5739 memcpy( hs_buf->data, ssl->in_msg, 6 );
5740 memset( hs_buf->data + 6, 0, 3 );
5741 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5742
5743 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005744
5745 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005746 }
5747 else
5748 {
5749 /* Make sure msg_type and length are consistent */
5750 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5751 {
5752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5753 /* Ignore */
5754 goto exit;
5755 }
5756 }
5757
Hanno Becker4422bbb2018-08-20 09:40:19 +01005758 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005759 {
5760 size_t frag_len, frag_off;
5761 unsigned char * const msg = hs_buf->data + 12;
5762
5763 /*
5764 * Check and copy current fragment
5765 */
5766
5767 /* Validation of header fields already done in
5768 * mbedtls_ssl_prepare_handshake_record(). */
5769 frag_off = ssl_get_hs_frag_off( ssl );
5770 frag_len = ssl_get_hs_frag_len( ssl );
5771
5772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5773 frag_off, frag_len ) );
5774 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5775
5776 if( hs_buf->is_fragmented )
5777 {
5778 unsigned char * const bitmask = msg + msg_len;
5779 ssl_bitmask_set( bitmask, frag_off, frag_len );
5780 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5781 msg_len ) == 0 );
5782 }
5783 else
5784 {
5785 hs_buf->is_complete = 1;
5786 }
5787
5788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5789 hs_buf->is_complete ? "" : "not yet " ) );
5790 }
5791
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005792 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005793 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005794
5795 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005796 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005797 break;
5798 }
5799
5800exit:
5801
5802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5803 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005804}
5805#endif /* MBEDTLS_SSL_PROTO_DTLS */
5806
Hanno Becker1097b342018-08-15 14:09:41 +01005807static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005808{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005809 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005810 * Consume last content-layer message and potentially
5811 * update in_msglen which keeps track of the contents'
5812 * consumption state.
5813 *
5814 * (1) Handshake messages:
5815 * Remove last handshake message, move content
5816 * and adapt in_msglen.
5817 *
5818 * (2) Alert messages:
5819 * Consume whole record content, in_msglen = 0.
5820 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005821 * (3) Change cipher spec:
5822 * Consume whole record content, in_msglen = 0.
5823 *
5824 * (4) Application data:
5825 * Don't do anything - the record layer provides
5826 * the application data as a stream transport
5827 * and consumes through mbedtls_ssl_read only.
5828 *
5829 */
5830
5831 /* Case (1): Handshake messages */
5832 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005833 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005834 /* Hard assertion to be sure that no application data
5835 * is in flight, as corrupting ssl->in_msglen during
5836 * ssl->in_offt != NULL is fatal. */
5837 if( ssl->in_offt != NULL )
5838 {
5839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5840 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5841 }
5842
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005843 /*
5844 * Get next Handshake message in the current record
5845 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005846
Hanno Becker4a810fb2017-05-24 16:27:30 +01005847 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005848 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005849 * current handshake content: If DTLS handshake
5850 * fragmentation is used, that's the fragment
5851 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005852 * size here is faulty and should be changed at
5853 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005854 * (2) While it doesn't seem to cause problems, one
5855 * has to be very careful not to assume that in_hslen
5856 * is always <= in_msglen in a sensible communication.
5857 * Again, it's wrong for DTLS handshake fragmentation.
5858 * The following check is therefore mandatory, and
5859 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005860 * Additionally, ssl->in_hslen might be arbitrarily out of
5861 * bounds after handling a DTLS message with an unexpected
5862 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005863 */
5864 if( ssl->in_hslen < ssl->in_msglen )
5865 {
5866 ssl->in_msglen -= ssl->in_hslen;
5867 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5868 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005869
Hanno Becker4a810fb2017-05-24 16:27:30 +01005870 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5871 ssl->in_msg, ssl->in_msglen );
5872 }
5873 else
5874 {
5875 ssl->in_msglen = 0;
5876 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005877
Hanno Becker4a810fb2017-05-24 16:27:30 +01005878 ssl->in_hslen = 0;
5879 }
5880 /* Case (4): Application data */
5881 else if( ssl->in_offt != NULL )
5882 {
5883 return( 0 );
5884 }
5885 /* Everything else (CCS & Alerts) */
5886 else
5887 {
5888 ssl->in_msglen = 0;
5889 }
5890
Hanno Becker1097b342018-08-15 14:09:41 +01005891 return( 0 );
5892}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005893
Hanno Beckere74d5562018-08-15 14:26:08 +01005894static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5895{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005896 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005897 return( 1 );
5898
5899 return( 0 );
5900}
5901
Hanno Becker5f066e72018-08-16 14:56:31 +01005902#if defined(MBEDTLS_SSL_PROTO_DTLS)
5903
5904static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5905{
5906 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5907 if( hs == NULL )
5908 return;
5909
Hanno Becker01315ea2018-08-21 17:22:17 +01005910 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005911 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005912 hs->buffering.total_bytes_buffered -=
5913 hs->buffering.future_record.len;
5914
5915 mbedtls_free( hs->buffering.future_record.data );
5916 hs->buffering.future_record.data = NULL;
5917 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005918}
5919
5920static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5921{
5922 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5923 unsigned char * rec;
5924 size_t rec_len;
5925 unsigned rec_epoch;
5926
5927 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5928 return( 0 );
5929
5930 if( hs == NULL )
5931 return( 0 );
5932
Hanno Becker5f066e72018-08-16 14:56:31 +01005933 rec = hs->buffering.future_record.data;
5934 rec_len = hs->buffering.future_record.len;
5935 rec_epoch = hs->buffering.future_record.epoch;
5936
5937 if( rec == NULL )
5938 return( 0 );
5939
Hanno Becker4cb782d2018-08-20 11:19:05 +01005940 /* Only consider loading future records if the
5941 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005942 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005943 return( 0 );
5944
Hanno Becker5f066e72018-08-16 14:56:31 +01005945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5946
5947 if( rec_epoch != ssl->in_epoch )
5948 {
5949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5950 goto exit;
5951 }
5952
5953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5954
5955 /* Double-check that the record is not too large */
5956 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5957 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5958 {
5959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5961 }
5962
5963 memcpy( ssl->in_hdr, rec, rec_len );
5964 ssl->in_left = rec_len;
5965 ssl->next_record_offset = 0;
5966
5967 ssl_free_buffered_record( ssl );
5968
5969exit:
5970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5971 return( 0 );
5972}
5973
Hanno Becker519f15d2019-07-11 12:43:20 +01005974static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
5975 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01005976{
5977 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01005978
5979 /* Don't buffer future records outside handshakes. */
5980 if( hs == NULL )
5981 return( 0 );
5982
5983 /* Only buffer handshake records (we are only interested
5984 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01005985 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01005986 return( 0 );
5987
5988 /* Don't buffer more than one future epoch record. */
5989 if( hs->buffering.future_record.data != NULL )
5990 return( 0 );
5991
Hanno Becker01315ea2018-08-21 17:22:17 +01005992 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01005993 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01005994 hs->buffering.total_bytes_buffered ) )
5995 {
5996 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 +01005997 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01005998 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005999 return( 0 );
6000 }
6001
Hanno Becker5f066e72018-08-16 14:56:31 +01006002 /* Buffer record */
6003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6004 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006005 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006006
6007 /* ssl_parse_record_header() only considers records
6008 * of the next epoch as candidates for buffering. */
6009 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006010 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006011
6012 hs->buffering.future_record.data =
6013 mbedtls_calloc( 1, hs->buffering.future_record.len );
6014 if( hs->buffering.future_record.data == NULL )
6015 {
6016 /* If we run out of RAM trying to buffer a
6017 * record from the next epoch, just ignore. */
6018 return( 0 );
6019 }
6020
Hanno Becker519f15d2019-07-11 12:43:20 +01006021 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006022
Hanno Becker519f15d2019-07-11 12:43:20 +01006023 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006024 return( 0 );
6025}
6026
6027#endif /* MBEDTLS_SSL_PROTO_DTLS */
6028
Hanno Beckere74d5562018-08-15 14:26:08 +01006029static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006030{
6031 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006032 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006033
Hanno Becker5f066e72018-08-16 14:56:31 +01006034#if defined(MBEDTLS_SSL_PROTO_DTLS)
6035 /* We might have buffered a future record; if so,
6036 * and if the epoch matches now, load it.
6037 * On success, this call will set ssl->in_left to
6038 * the length of the buffered record, so that
6039 * the calls to ssl_fetch_input() below will
6040 * essentially be no-ops. */
6041 ret = ssl_load_buffered_record( ssl );
6042 if( ret != 0 )
6043 return( ret );
6044#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006045
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006046 /* Ensure that we have enough space available for the default form
6047 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6048 * with no space for CIDs counted in). */
6049 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6050 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006053 return( ret );
6054 }
6055
Hanno Beckere5e7e782019-07-11 12:29:35 +01006056 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6057 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006060 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006061 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006062 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6063 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006064 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006065 if( ret != 0 )
6066 return( ret );
6067
6068 /* Fall through to handling of unexpected records */
6069 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6070 }
6071
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006072 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6073 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006074#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006075 /* Reset in pointers to default state for TLS/DTLS records,
6076 * assuming no CID and no offset between record content and
6077 * record plaintext. */
6078 ssl_update_in_pointers( ssl );
6079
Hanno Becker7ae20e02019-07-12 08:33:49 +01006080 /* Setup internal message pointers from record structure. */
6081 ssl->in_msgtype = rec.type;
6082#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6083 ssl->in_len = ssl->in_cid + rec.cid_len;
6084#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6085 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6086 ssl->in_msglen = rec.data_len;
6087
Hanno Becker2fddd372019-07-10 14:37:41 +01006088 ret = ssl_check_client_reconnect( ssl );
6089 if( ret != 0 )
6090 return( ret );
6091#endif
6092
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006093 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006094 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006095
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6097 "(header)" ) );
6098 }
6099 else
6100 {
6101 /* Skip invalid record and the rest of the datagram */
6102 ssl->next_record_offset = 0;
6103 ssl->in_left = 0;
6104
6105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6106 "(header)" ) );
6107 }
6108
6109 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006110 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006111 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006112 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006113#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006114 {
6115 return( ret );
6116 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006117 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006120 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006121 {
Hanno Beckera8814792019-07-10 15:01:45 +01006122 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006123 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006124 if( ssl->next_record_offset < ssl->in_left )
6125 {
6126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6127 }
6128 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006129 else
6130#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006131 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006132 /*
6133 * Fetch record contents from underlying transport.
6134 */
Hanno Beckera3175662019-07-11 12:50:29 +01006135 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006136 if( ret != 0 )
6137 {
6138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6139 return( ret );
6140 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006141
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006142 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006143 }
6144
6145 /*
6146 * Decrypt record contents.
6147 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006148
Hanno Beckerfdf66042019-07-11 13:07:45 +01006149 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006152 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006153 {
6154 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006155 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006156 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006157 /* Except when waiting for Finished as a bad mac here
6158 * probably means something went wrong in the handshake
6159 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6160 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6161 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6162 {
6163#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6164 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6165 {
6166 mbedtls_ssl_send_alert_message( ssl,
6167 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6168 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6169 }
6170#endif
6171 return( ret );
6172 }
6173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006175 if( ssl->conf->badmac_limit != 0 &&
6176 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6179 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006180 }
6181#endif
6182
Hanno Becker4a810fb2017-05-24 16:27:30 +01006183 /* As above, invalid records cause
6184 * dismissal of the whole datagram. */
6185
6186 ssl->next_record_offset = 0;
6187 ssl->in_left = 0;
6188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006190 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006191 }
6192
6193 return( ret );
6194 }
6195 else
6196#endif
6197 {
6198 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6200 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202 mbedtls_ssl_send_alert_message( ssl,
6203 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6204 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006205 }
6206#endif
6207 return( ret );
6208 }
6209 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006210
Hanno Becker44d89b22019-07-12 09:40:44 +01006211
6212 /* Reset in pointers to default state for TLS/DTLS records,
6213 * assuming no CID and no offset between record content and
6214 * record plaintext. */
6215 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006216#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6217 ssl->in_len = ssl->in_cid + rec.cid_len;
6218#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6219 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006220
Hanno Becker8685c822019-07-12 09:37:30 +01006221 /* The record content type may change during decryption,
6222 * so re-read it. */
6223 ssl->in_msgtype = rec.type;
6224 /* Also update the input buffer, because unfortunately
6225 * the server-side ssl_parse_client_hello() reparses the
6226 * record header when receiving a ClientHello initiating
6227 * a renegotiation. */
6228 ssl->in_hdr[0] = rec.type;
6229 ssl->in_msg = rec.buf + rec.data_offset;
6230 ssl->in_msglen = rec.data_len;
6231 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6232 ssl->in_len[1] = (unsigned char)( rec.data_len );
6233
Simon Butcher99000142016-10-13 17:21:01 +01006234 return( 0 );
6235}
6236
6237int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6238{
6239 int ret;
6240
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006241 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006242 * Handle particular types of records
6243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006245 {
Simon Butcher99000142016-10-13 17:21:01 +01006246 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6247 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006248 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006249 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006250 }
6251
Hanno Beckere678eaa2018-08-21 14:57:46 +01006252 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006253 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006254 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006255 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6257 ssl->in_msglen ) );
6258 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006259 }
6260
Hanno Beckere678eaa2018-08-21 14:57:46 +01006261 if( ssl->in_msg[0] != 1 )
6262 {
6263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6264 ssl->in_msg[0] ) );
6265 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6266 }
6267
6268#if defined(MBEDTLS_SSL_PROTO_DTLS)
6269 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6270 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6271 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6272 {
6273 if( ssl->handshake == NULL )
6274 {
6275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6276 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6277 }
6278
6279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6280 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6281 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006282#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006283 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006286 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006287 if( ssl->in_msglen != 2 )
6288 {
6289 /* Note: Standard allows for more than one 2 byte alert
6290 to be packed in a single message, but Mbed TLS doesn't
6291 currently support this. */
6292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6293 ssl->in_msglen ) );
6294 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6295 }
6296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006298 ssl->in_msg[0], ssl->in_msg[1] ) );
6299
6300 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006301 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006306 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006308 }
6309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6311 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6314 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006315 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006316
6317#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6318 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6319 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6320 {
Hanno Becker90333da2017-10-10 11:27:13 +01006321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006322 /* Will be handled when trying to parse ServerHello */
6323 return( 0 );
6324 }
6325#endif
6326
6327#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6328 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6329 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6330 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6331 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6332 {
6333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6334 /* Will be handled in mbedtls_ssl_parse_certificate() */
6335 return( 0 );
6336 }
6337#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6338
6339 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006340 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006341 }
6342
Hanno Beckerc76c6192017-06-06 10:03:17 +01006343#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006344 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006345 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006346 /* Drop unexpected ApplicationData records,
6347 * except at the beginning of renegotiations */
6348 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6349 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6350#if defined(MBEDTLS_SSL_RENEGOTIATION)
6351 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6352 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006353#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006354 )
6355 {
6356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6357 return( MBEDTLS_ERR_SSL_NON_FATAL );
6358 }
6359
6360 if( ssl->handshake != NULL &&
6361 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6362 {
6363 ssl_handshake_wrapup_free_hs_transform( ssl );
6364 }
6365 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006366#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006367
Paul Bakker5121ce52009-01-03 21:22:43 +00006368 return( 0 );
6369}
6370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006372{
6373 int ret;
6374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6376 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6377 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006378 {
6379 return( ret );
6380 }
6381
6382 return( 0 );
6383}
6384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006386 unsigned char level,
6387 unsigned char message )
6388{
6389 int ret;
6390
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006391 if( ssl == NULL || ssl->conf == NULL )
6392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006395 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006398 ssl->out_msglen = 2;
6399 ssl->out_msg[0] = level;
6400 ssl->out_msg[1] = message;
6401
Hanno Becker67bc7c32018-08-06 11:33:50 +01006402 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006405 return( ret );
6406 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006408
6409 return( 0 );
6410}
6411
Hanno Beckerb9d44792019-02-08 07:19:04 +00006412#if defined(MBEDTLS_X509_CRT_PARSE_C)
6413static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6414{
6415#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6416 if( session->peer_cert != NULL )
6417 {
6418 mbedtls_x509_crt_free( session->peer_cert );
6419 mbedtls_free( session->peer_cert );
6420 session->peer_cert = NULL;
6421 }
6422#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6423 if( session->peer_cert_digest != NULL )
6424 {
6425 /* Zeroization is not necessary. */
6426 mbedtls_free( session->peer_cert_digest );
6427 session->peer_cert_digest = NULL;
6428 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6429 session->peer_cert_digest_len = 0;
6430 }
6431#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6432}
6433#endif /* MBEDTLS_X509_CRT_PARSE_C */
6434
Paul Bakker5121ce52009-01-03 21:22:43 +00006435/*
6436 * Handshake functions
6437 */
Hanno Becker21489932019-02-05 13:20:55 +00006438#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006439/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006441{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006442 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6443 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006446
Hanno Becker7177a882019-02-05 13:36:46 +00006447 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006450 ssl->state++;
6451 return( 0 );
6452 }
6453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006456}
6457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006459{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006460 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6461 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006464
Hanno Becker7177a882019-02-05 13:36:46 +00006465 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006468 ssl->state++;
6469 return( 0 );
6470 }
6471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006474}
Gilles Peskinef9828522017-05-03 12:28:43 +02006475
Hanno Becker21489932019-02-05 13:20:55 +00006476#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006477/* Some certificate support -> implement write and parse */
6478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006480{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006482 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006484 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6485 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006488
Hanno Becker7177a882019-02-05 13:36:46 +00006489 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006492 ssl->state++;
6493 return( 0 );
6494 }
6495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006497 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006498 {
6499 if( ssl->client_auth == 0 )
6500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006502 ssl->state++;
6503 return( 0 );
6504 }
6505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006507 /*
6508 * If using SSLv3 and got no cert, send an Alert message
6509 * (otherwise an empty Certificate message will be sent).
6510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6512 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006513 {
6514 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6516 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6517 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006520 goto write_msg;
6521 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006523 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524#endif /* MBEDTLS_SSL_CLI_C */
6525#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006526 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6531 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006532 }
6533 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006534#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006537
6538 /*
6539 * 0 . 0 handshake type
6540 * 1 . 3 handshake length
6541 * 4 . 6 length of all certs
6542 * 7 . 9 length of cert. 1
6543 * 10 . n-1 peer certificate
6544 * n . n+2 length of cert. 2
6545 * n+3 . ... upper level cert, etc.
6546 */
6547 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006549
Paul Bakker29087132010-03-21 21:03:34 +00006550 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006551 {
6552 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006553 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006556 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006558 }
6559
6560 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6561 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6562 ssl->out_msg[i + 2] = (unsigned char)( n );
6563
6564 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6565 i += n; crt = crt->next;
6566 }
6567
6568 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6569 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6570 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6571
6572 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6574 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006575
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006576#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006577write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006578#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006579
6580 ssl->state++;
6581
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006582 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006583 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006585 return( ret );
6586 }
6587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006589
Paul Bakkered27a042013-04-18 22:46:23 +02006590 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006591}
6592
Hanno Becker84879e32019-01-31 07:44:03 +00006593#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006594
6595#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006596static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6597 unsigned char *crt_buf,
6598 size_t crt_buf_len )
6599{
6600 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6601
6602 if( peer_crt == NULL )
6603 return( -1 );
6604
6605 if( peer_crt->raw.len != crt_buf_len )
6606 return( -1 );
6607
Hanno Becker46f34d02019-02-08 14:00:04 +00006608 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006609}
Hanno Becker177475a2019-02-05 17:02:46 +00006610#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6611static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6612 unsigned char *crt_buf,
6613 size_t crt_buf_len )
6614{
6615 int ret;
6616 unsigned char const * const peer_cert_digest =
6617 ssl->session->peer_cert_digest;
6618 mbedtls_md_type_t const peer_cert_digest_type =
6619 ssl->session->peer_cert_digest_type;
6620 mbedtls_md_info_t const * const digest_info =
6621 mbedtls_md_info_from_type( peer_cert_digest_type );
6622 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6623 size_t digest_len;
6624
6625 if( peer_cert_digest == NULL || digest_info == NULL )
6626 return( -1 );
6627
6628 digest_len = mbedtls_md_get_size( digest_info );
6629 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6630 return( -1 );
6631
6632 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6633 if( ret != 0 )
6634 return( -1 );
6635
6636 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6637}
6638#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006639#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006640
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006641/*
6642 * Once the certificate message is read, parse it into a cert chain and
6643 * perform basic checks, but leave actual verification to the caller
6644 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006645static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6646 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006647{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006648 int ret;
6649#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6650 int crt_cnt=0;
6651#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006652 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006653 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006658 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6659 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006661 }
6662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6664 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006667 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6668 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006670 }
6671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006673
Paul Bakker5121ce52009-01-03 21:22:43 +00006674 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006676 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006677 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006678
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006679 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006683 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6684 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006686 }
6687
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006688 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6689 i += 3;
6690
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006691 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006692 while( i < ssl->in_hslen )
6693 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006694 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006695 if ( i + 3 > ssl->in_hslen ) {
6696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006697 mbedtls_ssl_send_alert_message( ssl,
6698 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6699 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006700 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6701 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006702 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6703 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006704 if( ssl->in_msg[i] != 0 )
6705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006707 mbedtls_ssl_send_alert_message( ssl,
6708 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6709 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006711 }
6712
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006713 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006714 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6715 | (unsigned int) ssl->in_msg[i + 2];
6716 i += 3;
6717
6718 if( n < 128 || i + n > ssl->in_hslen )
6719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006721 mbedtls_ssl_send_alert_message( ssl,
6722 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 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006728#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6729 if( crt_cnt++ == 0 &&
6730 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6731 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006732 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006733 /* During client-side renegotiation, check that the server's
6734 * end-CRTs hasn't changed compared to the initial handshake,
6735 * mitigating the triple handshake attack. On success, reuse
6736 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6738 if( ssl_check_peer_crt_unchanged( ssl,
6739 &ssl->in_msg[i],
6740 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006741 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6743 mbedtls_ssl_send_alert_message( ssl,
6744 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6745 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6746 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006747 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006748
6749 /* Now we can safely free the original chain. */
6750 ssl_clear_peer_cert( ssl->session );
6751 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006752#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6753
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006754 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006755#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006756 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006757#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006758 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006759 * it in-place from the input buffer instead of making a copy. */
6760 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6761#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006762 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006763 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006764 case 0: /*ok*/
6765 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6766 /* Ignore certificate with an unknown algorithm: maybe a
6767 prior certificate was already trusted. */
6768 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006769
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006770 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6771 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6772 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006773
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006774 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6775 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6776 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006777
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006778 default:
6779 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6780 crt_parse_der_failed:
6781 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6782 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6783 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006784 }
6785
6786 i += n;
6787 }
6788
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006789 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006790 return( 0 );
6791}
6792
Hanno Becker4a55f632019-02-05 12:49:06 +00006793#if defined(MBEDTLS_SSL_SRV_C)
6794static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6795{
6796 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6797 return( -1 );
6798
6799#if defined(MBEDTLS_SSL_PROTO_SSL3)
6800 /*
6801 * Check if the client sent an empty certificate
6802 */
6803 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6804 {
6805 if( ssl->in_msglen == 2 &&
6806 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6807 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6808 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6809 {
6810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6811 return( 0 );
6812 }
6813
6814 return( -1 );
6815 }
6816#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6817
6818#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6819 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6820 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6821 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6822 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6823 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6824 {
6825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6826 return( 0 );
6827 }
6828
6829 return( -1 );
6830#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6831 MBEDTLS_SSL_PROTO_TLS1_2 */
6832}
6833#endif /* MBEDTLS_SSL_SRV_C */
6834
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006835/* Check if a certificate message is expected.
6836 * Return either
6837 * - SSL_CERTIFICATE_EXPECTED, or
6838 * - SSL_CERTIFICATE_SKIP
6839 * indicating whether a Certificate message is expected or not.
6840 */
6841#define SSL_CERTIFICATE_EXPECTED 0
6842#define SSL_CERTIFICATE_SKIP 1
6843static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6844 int authmode )
6845{
6846 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006847 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006848
6849 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6850 return( SSL_CERTIFICATE_SKIP );
6851
6852#if defined(MBEDTLS_SSL_SRV_C)
6853 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6854 {
6855 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6856 return( SSL_CERTIFICATE_SKIP );
6857
6858 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6859 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006860 ssl->session_negotiate->verify_result =
6861 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6862 return( SSL_CERTIFICATE_SKIP );
6863 }
6864 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006865#else
6866 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006867#endif /* MBEDTLS_SSL_SRV_C */
6868
6869 return( SSL_CERTIFICATE_EXPECTED );
6870}
6871
Hanno Becker68636192019-02-05 14:36:34 +00006872static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6873 int authmode,
6874 mbedtls_x509_crt *chain,
6875 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006876{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006877 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006878 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006879 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006880 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006881
Hanno Becker8927c832019-04-03 12:52:50 +01006882 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6883 void *p_vrfy;
6884
Hanno Becker68636192019-02-05 14:36:34 +00006885 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6886 return( 0 );
6887
Hanno Becker8927c832019-04-03 12:52:50 +01006888 if( ssl->f_vrfy != NULL )
6889 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006890 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006891 f_vrfy = ssl->f_vrfy;
6892 p_vrfy = ssl->p_vrfy;
6893 }
6894 else
6895 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006896 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006897 f_vrfy = ssl->conf->f_vrfy;
6898 p_vrfy = ssl->conf->p_vrfy;
6899 }
6900
Hanno Becker68636192019-02-05 14:36:34 +00006901 /*
6902 * Main check: verify certificate
6903 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006904#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6905 if( ssl->conf->f_ca_cb != NULL )
6906 {
6907 ((void) rs_ctx);
6908 have_ca_chain = 1;
6909
6910 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006911 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006912 chain,
6913 ssl->conf->f_ca_cb,
6914 ssl->conf->p_ca_cb,
6915 ssl->conf->cert_profile,
6916 ssl->hostname,
6917 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006918 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006919 }
6920 else
6921#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6922 {
6923 mbedtls_x509_crt *ca_chain;
6924 mbedtls_x509_crl *ca_crl;
6925
6926#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6927 if( ssl->handshake->sni_ca_chain != NULL )
6928 {
6929 ca_chain = ssl->handshake->sni_ca_chain;
6930 ca_crl = ssl->handshake->sni_ca_crl;
6931 }
6932 else
6933#endif
6934 {
6935 ca_chain = ssl->conf->ca_chain;
6936 ca_crl = ssl->conf->ca_crl;
6937 }
6938
6939 if( ca_chain != NULL )
6940 have_ca_chain = 1;
6941
6942 ret = mbedtls_x509_crt_verify_restartable(
6943 chain,
6944 ca_chain, ca_crl,
6945 ssl->conf->cert_profile,
6946 ssl->hostname,
6947 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006948 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006949 }
Hanno Becker68636192019-02-05 14:36:34 +00006950
6951 if( ret != 0 )
6952 {
6953 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6954 }
6955
6956#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6957 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6958 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6959#endif
6960
6961 /*
6962 * Secondary checks: always done, but change 'ret' only if it was 0
6963 */
6964
6965#if defined(MBEDTLS_ECP_C)
6966 {
6967 const mbedtls_pk_context *pk = &chain->pk;
6968
6969 /* If certificate uses an EC key, make sure the curve is OK */
6970 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6971 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6972 {
6973 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6974
6975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6976 if( ret == 0 )
6977 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6978 }
6979 }
6980#endif /* MBEDTLS_ECP_C */
6981
6982 if( mbedtls_ssl_check_cert_usage( chain,
6983 ciphersuite_info,
6984 ! ssl->conf->endpoint,
6985 &ssl->session_negotiate->verify_result ) != 0 )
6986 {
6987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6988 if( ret == 0 )
6989 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6990 }
6991
6992 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6993 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6994 * with details encoded in the verification flags. All other kinds
6995 * of error codes, including those from the user provided f_vrfy
6996 * functions, are treated as fatal and lead to a failure of
6997 * ssl_parse_certificate even if verification was optional. */
6998 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6999 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7000 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7001 {
7002 ret = 0;
7003 }
7004
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007005 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007006 {
7007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7008 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7009 }
7010
7011 if( ret != 0 )
7012 {
7013 uint8_t alert;
7014
7015 /* The certificate may have been rejected for several reasons.
7016 Pick one and send the corresponding alert. Which alert to send
7017 may be a subject of debate in some cases. */
7018 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7019 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7020 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7021 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7022 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7023 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7024 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7025 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7026 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7027 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7028 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7029 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7030 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7031 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7032 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7033 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7034 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7035 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7036 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7037 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7038 else
7039 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7040 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7041 alert );
7042 }
7043
7044#if defined(MBEDTLS_DEBUG_C)
7045 if( ssl->session_negotiate->verify_result != 0 )
7046 {
7047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7048 ssl->session_negotiate->verify_result ) );
7049 }
7050 else
7051 {
7052 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7053 }
7054#endif /* MBEDTLS_DEBUG_C */
7055
7056 return( ret );
7057}
7058
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007059#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7060static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7061 unsigned char *start, size_t len )
7062{
7063 int ret;
7064 /* Remember digest of the peer's end-CRT. */
7065 ssl->session_negotiate->peer_cert_digest =
7066 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7067 if( ssl->session_negotiate->peer_cert_digest == NULL )
7068 {
7069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7070 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7071 mbedtls_ssl_send_alert_message( ssl,
7072 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7073 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7074
7075 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7076 }
7077
7078 ret = mbedtls_md( mbedtls_md_info_from_type(
7079 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7080 start, len,
7081 ssl->session_negotiate->peer_cert_digest );
7082
7083 ssl->session_negotiate->peer_cert_digest_type =
7084 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7085 ssl->session_negotiate->peer_cert_digest_len =
7086 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7087
7088 return( ret );
7089}
7090
7091static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7092 unsigned char *start, size_t len )
7093{
7094 unsigned char *end = start + len;
7095 int ret;
7096
7097 /* Make a copy of the peer's raw public key. */
7098 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7099 ret = mbedtls_pk_parse_subpubkey( &start, end,
7100 &ssl->handshake->peer_pubkey );
7101 if( ret != 0 )
7102 {
7103 /* We should have parsed the public key before. */
7104 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7105 }
7106
7107 return( 0 );
7108}
7109#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7110
Hanno Becker68636192019-02-05 14:36:34 +00007111int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7112{
7113 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007114 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007115#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7116 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7117 ? ssl->handshake->sni_authmode
7118 : ssl->conf->authmode;
7119#else
7120 const int authmode = ssl->conf->authmode;
7121#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007122 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007123 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007124
7125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7126
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007127 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7128 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007129 {
7130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007131 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007132 }
7133
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007134#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7135 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007136 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007137 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007138 chain = ssl->handshake->ecrs_peer_cert;
7139 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007140 goto crt_verify;
7141 }
7142#endif
7143
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007144 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007145 {
7146 /* mbedtls_ssl_read_record may have sent an alert already. We
7147 let it decide whether to alert. */
7148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007149 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007150 }
7151
Hanno Becker4a55f632019-02-05 12:49:06 +00007152#if defined(MBEDTLS_SSL_SRV_C)
7153 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7154 {
7155 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007156
7157 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007158 ret = 0;
7159 else
7160 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007161
Hanno Becker6bdfab22019-02-05 13:11:17 +00007162 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007163 }
7164#endif /* MBEDTLS_SSL_SRV_C */
7165
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007166 /* Clear existing peer CRT structure in case we tried to
7167 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007168 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007169
7170 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7171 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007172 {
7173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7174 sizeof( mbedtls_x509_crt ) ) );
7175 mbedtls_ssl_send_alert_message( ssl,
7176 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7177 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007178
Hanno Becker3dad3112019-02-05 17:19:52 +00007179 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7180 goto exit;
7181 }
7182 mbedtls_x509_crt_init( chain );
7183
7184 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007185 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007186 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007187
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007188#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7189 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007190 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007191
7192crt_verify:
7193 if( ssl->handshake->ecrs_enabled)
7194 rs_ctx = &ssl->handshake->ecrs_ctx;
7195#endif
7196
Hanno Becker68636192019-02-05 14:36:34 +00007197 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007198 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007199 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007200 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007201
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007202#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007203 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007204 unsigned char *crt_start, *pk_start;
7205 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007206
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007207 /* We parse the CRT chain without copying, so
7208 * these pointers point into the input buffer,
7209 * and are hence still valid after freeing the
7210 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007211
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007212 crt_start = chain->raw.p;
7213 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007214
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007215 pk_start = chain->pk_raw.p;
7216 pk_len = chain->pk_raw.len;
7217
7218 /* Free the CRT structures before computing
7219 * digest and copying the peer's public key. */
7220 mbedtls_x509_crt_free( chain );
7221 mbedtls_free( chain );
7222 chain = NULL;
7223
7224 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007225 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007226 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007227
7228 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7229 if( ret != 0 )
7230 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007231 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007232#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7233 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007234 ssl->session_negotiate->peer_cert = chain;
7235 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007236#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007239
Hanno Becker6bdfab22019-02-05 13:11:17 +00007240exit:
7241
Hanno Becker3dad3112019-02-05 17:19:52 +00007242 if( ret == 0 )
7243 ssl->state++;
7244
7245#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7246 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7247 {
7248 ssl->handshake->ecrs_peer_cert = chain;
7249 chain = NULL;
7250 }
7251#endif
7252
7253 if( chain != NULL )
7254 {
7255 mbedtls_x509_crt_free( chain );
7256 mbedtls_free( chain );
7257 }
7258
Paul Bakker5121ce52009-01-03 21:22:43 +00007259 return( ret );
7260}
Hanno Becker21489932019-02-05 13:20:55 +00007261#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007264{
7265 int ret;
7266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007270 ssl->out_msglen = 1;
7271 ssl->out_msg[0] = 1;
7272
Paul Bakker5121ce52009-01-03 21:22:43 +00007273 ssl->state++;
7274
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007275 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007276 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007278 return( ret );
7279 }
7280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007282
7283 return( 0 );
7284}
7285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007287{
7288 int ret;
7289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007291
Hanno Becker327c93b2018-08-15 13:56:18 +01007292 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007295 return( ret );
7296 }
7297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007301 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7302 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007304 }
7305
Hanno Beckere678eaa2018-08-21 14:57:46 +01007306 /* CCS records are only accepted if they have length 1 and content '1',
7307 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007308
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007309 /*
7310 * Switch to our negotiated transform and session parameters for inbound
7311 * data.
7312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007314 ssl->transform_in = ssl->transform_negotiate;
7315 ssl->session_in = ssl->session_negotiate;
7316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007321 ssl_dtls_replay_reset( ssl );
7322#endif
7323
7324 /* Increment epoch */
7325 if( ++ssl->in_epoch == 0 )
7326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007328 /* This is highly unlikely to happen for legitimate reasons, so
7329 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007331 }
7332 }
7333 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007335 memset( ssl->in_ctr, 0, 8 );
7336
Hanno Becker79594fd2019-05-08 09:38:41 +01007337 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7340 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007345 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7346 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007348 }
7349 }
7350#endif
7351
Paul Bakker5121ce52009-01-03 21:22:43 +00007352 ssl->state++;
7353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007355
7356 return( 0 );
7357}
7358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7360 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007361{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007362 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7365 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7366 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007367 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007368 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7371#if defined(MBEDTLS_SHA512_C)
7372 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007373 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7374 else
7375#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376#if defined(MBEDTLS_SHA256_C)
7377 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007378 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007379 else
7380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007384 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007385 }
Paul Bakker380da532012-04-18 16:10:25 +00007386}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7391 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007392 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7393 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7396#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007397#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007398 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007399 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7400#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007401 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007402#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007403#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007405#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007406 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007407 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007408#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007409 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007410#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007411#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007412#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007413}
7414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007416 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7419 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007420 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7421 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007422#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7424#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007425#if defined(MBEDTLS_USE_PSA_CRYPTO)
7426 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7427#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007428 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007429#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007432#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007433 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007434#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007435 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007436#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007437#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007439}
7440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7442 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7443static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007444 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007445{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007446 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7447 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007448}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007449#endif
Paul Bakker380da532012-04-18 16:10:25 +00007450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7452#if defined(MBEDTLS_SHA256_C)
7453static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007454 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007455{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007456#if defined(MBEDTLS_USE_PSA_CRYPTO)
7457 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7458#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007459 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007460#endif
Paul Bakker380da532012-04-18 16:10:25 +00007461}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007462#endif
Paul Bakker380da532012-04-18 16:10:25 +00007463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464#if defined(MBEDTLS_SHA512_C)
7465static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007466 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007467{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007468#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007469 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007470#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007471 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007472#endif
Paul Bakker380da532012-04-18 16:10:25 +00007473}
Paul Bakker769075d2012-11-24 11:26:46 +01007474#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007478static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007480{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007481 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007482 mbedtls_md5_context md5;
7483 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007484
Paul Bakker5121ce52009-01-03 21:22:43 +00007485 unsigned char padbuf[48];
7486 unsigned char md5sum[16];
7487 unsigned char sha1sum[20];
7488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007490 if( !session )
7491 session = ssl->session;
7492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007494
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007495 mbedtls_md5_init( &md5 );
7496 mbedtls_sha1_init( &sha1 );
7497
7498 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7499 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007500
7501 /*
7502 * SSLv3:
7503 * hash =
7504 * MD5( master + pad2 +
7505 * MD5( handshake + sender + master + pad1 ) )
7506 * + SHA1( master + pad2 +
7507 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007508 */
7509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007511 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7512 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007513#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007516 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7517 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007518#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007521 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007522
Paul Bakker1ef83d62012-04-11 12:09:53 +00007523 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007524
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007525 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7526 mbedtls_md5_update_ret( &md5, session->master, 48 );
7527 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7528 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007529
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007530 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7531 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7532 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7533 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007534
Paul Bakker1ef83d62012-04-11 12:09:53 +00007535 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007536
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007537 mbedtls_md5_starts_ret( &md5 );
7538 mbedtls_md5_update_ret( &md5, session->master, 48 );
7539 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7540 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7541 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007542
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007543 mbedtls_sha1_starts_ret( &sha1 );
7544 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7545 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7546 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7547 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007550
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007551 mbedtls_md5_free( &md5 );
7552 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007553
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007554 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7555 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7556 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007559}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007563static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007565{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007566 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007567 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007568 mbedtls_md5_context md5;
7569 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007570 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007573 if( !session )
7574 session = ssl->session;
7575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007577
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007578 mbedtls_md5_init( &md5 );
7579 mbedtls_sha1_init( &sha1 );
7580
7581 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7582 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007583
Paul Bakker1ef83d62012-04-11 12:09:53 +00007584 /*
7585 * TLSv1:
7586 * hash = PRF( master, finished_label,
7587 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7588 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007591 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7592 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007593#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007596 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7597 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007598#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007601 ? "client finished"
7602 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007603
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007604 mbedtls_md5_finish_ret( &md5, padbuf );
7605 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007606
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007607 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007608 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007611
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007612 mbedtls_md5_free( &md5 );
7613 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007614
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007615 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007618}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7622#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007623static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007625{
7626 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007627 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007628 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007629#if defined(MBEDTLS_USE_PSA_CRYPTO)
7630 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007631 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007632 psa_status_t status;
7633#else
7634 mbedtls_sha256_context sha256;
7635#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007638 if( !session )
7639 session = ssl->session;
7640
Andrzej Kurekeb342242019-01-29 09:14:33 -05007641 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7642 ? "client finished"
7643 : "server finished";
7644
7645#if defined(MBEDTLS_USE_PSA_CRYPTO)
7646 sha256_psa = psa_hash_operation_init();
7647
7648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7649
7650 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7651 if( status != PSA_SUCCESS )
7652 {
7653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7654 return;
7655 }
7656
7657 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7658 if( status != PSA_SUCCESS )
7659 {
7660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7661 return;
7662 }
7663 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7664#else
7665
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007666 mbedtls_sha256_init( &sha256 );
7667
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007669
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007670 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007671
7672 /*
7673 * TLSv1.2:
7674 * hash = PRF( master, finished_label,
7675 * Hash( handshake ) )[0.11]
7676 */
7677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678#if !defined(MBEDTLS_SHA256_ALT)
7679 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007680 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007681#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007682
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007683 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007684 mbedtls_sha256_free( &sha256 );
7685#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007686
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007687 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007688 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007691
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007692 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007695}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007699static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007701{
7702 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007703 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007704 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007705#if defined(MBEDTLS_USE_PSA_CRYPTO)
7706 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007707 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007708 psa_status_t status;
7709#else
7710 mbedtls_sha512_context sha512;
7711#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007714 if( !session )
7715 session = ssl->session;
7716
Andrzej Kurekeb342242019-01-29 09:14:33 -05007717 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7718 ? "client finished"
7719 : "server finished";
7720
7721#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007722 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007723
7724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7725
Andrzej Kurek972fba52019-01-30 03:29:12 -05007726 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007727 if( status != PSA_SUCCESS )
7728 {
7729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7730 return;
7731 }
7732
Andrzej Kurek972fba52019-01-30 03:29:12 -05007733 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007734 if( status != PSA_SUCCESS )
7735 {
7736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7737 return;
7738 }
7739 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7740#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007741 mbedtls_sha512_init( &sha512 );
7742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007744
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007745 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007746
7747 /*
7748 * TLSv1.2:
7749 * hash = PRF( master, finished_label,
7750 * Hash( handshake ) )[0.11]
7751 */
7752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007754 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7755 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007756#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007757
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007758 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007759 mbedtls_sha512_free( &sha512 );
7760#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007761
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007762 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007763 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007766
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007767 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771#endif /* MBEDTLS_SHA512_C */
7772#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007775{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007777
7778 /*
7779 * Free our handshake params
7780 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007781 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007783 ssl->handshake = NULL;
7784
7785 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007786 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007787 */
7788 if( ssl->transform )
7789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 mbedtls_ssl_transform_free( ssl->transform );
7791 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007792 }
7793 ssl->transform = ssl->transform_negotiate;
7794 ssl->transform_negotiate = NULL;
7795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007797}
7798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007800{
7801 int resume = ssl->handshake->resume;
7802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805#if defined(MBEDTLS_SSL_RENEGOTIATION)
7806 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007809 ssl->renego_records_seen = 0;
7810 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007811#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007812
7813 /*
7814 * Free the previous session and switch in the current one
7815 */
Paul Bakker0a597072012-09-25 21:55:46 +00007816 if( ssl->session )
7817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007819 /* RFC 7366 3.1: keep the EtM state */
7820 ssl->session_negotiate->encrypt_then_mac =
7821 ssl->session->encrypt_then_mac;
7822#endif
7823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824 mbedtls_ssl_session_free( ssl->session );
7825 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007826 }
7827 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007828 ssl->session_negotiate = NULL;
7829
Paul Bakker0a597072012-09-25 21:55:46 +00007830 /*
7831 * Add cache entry
7832 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007833 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007834 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007835 resume == 0 )
7836 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007837 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007839 }
Paul Bakker0a597072012-09-25 21:55:46 +00007840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007842 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007843 ssl->handshake->flight != NULL )
7844 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007845 /* Cancel handshake timer */
7846 ssl_set_timer( ssl, 0 );
7847
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007848 /* Keep last flight around in case we need to resend it:
7849 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007851 }
7852 else
7853#endif
7854 ssl_handshake_wrapup_free_hs_transform( ssl );
7855
Paul Bakker48916f92012-09-16 19:57:18 +00007856 ssl->state++;
7857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007859}
7860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007862{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007863 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007866
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007867 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007868
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007869 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007870
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007871 /*
7872 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7873 * may define some other value. Currently (early 2016), no defined
7874 * ciphersuite does this (and this is unlikely to change as activity has
7875 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007880 ssl->verify_data_len = hash_len;
7881 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007882#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007883
Paul Bakker5121ce52009-01-03 21:22:43 +00007884 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7886 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007887
7888 /*
7889 * In case of session resuming, invert the client and server
7890 * ChangeCipherSpec messages order.
7891 */
Paul Bakker0a597072012-09-25 21:55:46 +00007892 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007895 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007897#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007899 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007901#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007902 }
7903 else
7904 ssl->state++;
7905
Paul Bakker48916f92012-09-16 19:57:18 +00007906 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007907 * Switch to our negotiated transform and session parameters for outbound
7908 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007913 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007914 {
7915 unsigned char i;
7916
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007917 /* Remember current epoch settings for resending */
7918 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007919 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007920
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007921 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007922 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007923
7924 /* Increment epoch */
7925 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007926 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007927 break;
7928
7929 /* The loop goes to its end iff the counter is wrapping */
7930 if( i == 0 )
7931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7933 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007934 }
7935 }
7936 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007938 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007939
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007940 ssl->transform_out = ssl->transform_negotiate;
7941 ssl->session_out = ssl->session_negotiate;
7942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7944 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7949 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007950 }
7951 }
7952#endif
7953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007955 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007957#endif
7958
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007959 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007960 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007962 return( ret );
7963 }
7964
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007965#if defined(MBEDTLS_SSL_PROTO_DTLS)
7966 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7967 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7968 {
7969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7970 return( ret );
7971 }
7972#endif
7973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007975
7976 return( 0 );
7977}
7978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007980#define SSL_MAX_HASH_LEN 36
7981#else
7982#define SSL_MAX_HASH_LEN 12
7983#endif
7984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007986{
Paul Bakker23986e52011-04-24 08:57:21 +00007987 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007988 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007989 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007992
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007993 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007994
Hanno Becker327c93b2018-08-15 13:56:18 +01007995 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007998 return( ret );
7999 }
8000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008004 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8005 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008007 }
8008
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008009 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010#if defined(MBEDTLS_SSL_PROTO_SSL3)
8011 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008012 hash_len = 36;
8013 else
8014#endif
8015 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8018 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008021 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8022 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008024 }
8025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008027 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008030 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8031 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008033 }
8034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008036 ssl->verify_data_len = hash_len;
8037 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008038#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008039
Paul Bakker0a597072012-09-25 21:55:46 +00008040 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008042#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008043 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008044 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008046#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008047 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008050 }
8051 else
8052 ssl->state++;
8053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008055 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008057#endif
8058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008060
8061 return( 0 );
8062}
8063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008066 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8069 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8070 mbedtls_md5_init( &handshake->fin_md5 );
8071 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008072 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8073 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8076#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008077#if defined(MBEDTLS_USE_PSA_CRYPTO)
8078 handshake->fin_sha256_psa = psa_hash_operation_init();
8079 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8080#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008082 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008083#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008086#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008087 handshake->fin_sha384_psa = psa_hash_operation_init();
8088 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008089#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008091 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008092#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008093#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008095
8096 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008097
8098#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8099 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8100 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8101#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103#if defined(MBEDTLS_DHM_C)
8104 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106#if defined(MBEDTLS_ECDH_C)
8107 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008108#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008109#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008110 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008111#if defined(MBEDTLS_SSL_CLI_C)
8112 handshake->ecjpake_cache = NULL;
8113 handshake->ecjpake_cache_len = 0;
8114#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008115#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008116
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008117#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008118 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008119#endif
8120
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008121#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8122 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8123#endif
Hanno Becker75173122019-02-06 16:18:31 +00008124
8125#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8126 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8127 mbedtls_pk_init( &handshake->peer_pubkey );
8128#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008129}
8130
Hanno Beckera18d1322018-01-03 14:27:32 +00008131void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8136 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008137
Hanno Beckerd56ed242018-01-03 15:32:51 +00008138#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139 mbedtls_md_init( &transform->md_ctx_enc );
8140 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008141#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008142}
8143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008147}
8148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008150{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008151 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008152 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008153 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008154 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008156 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008157 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008158
8159 /*
8160 * Either the pointers are now NULL or cleared properly and can be freed.
8161 * Now allocate missing structures.
8162 */
8163 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008164 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008165 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008166 }
Paul Bakker48916f92012-09-16 19:57:18 +00008167
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008168 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008169 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008170 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008171 }
Paul Bakker48916f92012-09-16 19:57:18 +00008172
Paul Bakker82788fb2014-10-20 13:59:19 +02008173 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008174 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008175 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008176 }
Paul Bakker48916f92012-09-16 19:57:18 +00008177
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008178 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008179 if( ssl->handshake == NULL ||
8180 ssl->transform_negotiate == NULL ||
8181 ssl->session_negotiate == NULL )
8182 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185 mbedtls_free( ssl->handshake );
8186 mbedtls_free( ssl->transform_negotiate );
8187 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008188
8189 ssl->handshake = NULL;
8190 ssl->transform_negotiate = NULL;
8191 ssl->session_negotiate = NULL;
8192
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008193 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008194 }
8195
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008196 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008198 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008199 ssl_handshake_params_init( ssl->handshake );
8200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008202 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8203 {
8204 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008205
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008206 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8207 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8208 else
8209 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008210
8211 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008212 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008213#endif
8214
Paul Bakker48916f92012-09-16 19:57:18 +00008215 return( 0 );
8216}
8217
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008218#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008219/* Dummy cookie callbacks for defaults */
8220static int ssl_cookie_write_dummy( void *ctx,
8221 unsigned char **p, unsigned char *end,
8222 const unsigned char *cli_id, size_t cli_id_len )
8223{
8224 ((void) ctx);
8225 ((void) p);
8226 ((void) end);
8227 ((void) cli_id);
8228 ((void) cli_id_len);
8229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008230 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008231}
8232
8233static int ssl_cookie_check_dummy( void *ctx,
8234 const unsigned char *cookie, size_t cookie_len,
8235 const unsigned char *cli_id, size_t cli_id_len )
8236{
8237 ((void) ctx);
8238 ((void) cookie);
8239 ((void) cookie_len);
8240 ((void) cli_id);
8241 ((void) cli_id_len);
8242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008244}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008245#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008246
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008247/* Once ssl->out_hdr as the address of the beginning of the
8248 * next outgoing record is set, deduce the other pointers.
8249 *
8250 * Note: For TLS, we save the implicit record sequence number
8251 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8252 * and the caller has to make sure there's space for this.
8253 */
8254
8255static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8256 mbedtls_ssl_transform *transform )
8257{
8258#if defined(MBEDTLS_SSL_PROTO_DTLS)
8259 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8260 {
8261 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008262#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008263 ssl->out_cid = ssl->out_ctr + 8;
8264 ssl->out_len = ssl->out_cid;
8265 if( transform != NULL )
8266 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008267#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008268 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008269#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008270 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008271 }
8272 else
8273#endif
8274 {
8275 ssl->out_ctr = ssl->out_hdr - 8;
8276 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008277#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008278 ssl->out_cid = ssl->out_len;
8279#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008280 ssl->out_iv = ssl->out_hdr + 5;
8281 }
8282
8283 /* Adjust out_msg to make space for explicit IV, if used. */
8284 if( transform != NULL &&
8285 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8286 {
8287 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8288 }
8289 else
8290 ssl->out_msg = ssl->out_iv;
8291}
8292
8293/* Once ssl->in_hdr as the address of the beginning of the
8294 * next incoming record is set, deduce the other pointers.
8295 *
8296 * Note: For TLS, we save the implicit record sequence number
8297 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8298 * and the caller has to make sure there's space for this.
8299 */
8300
Hanno Becker79594fd2019-05-08 09:38:41 +01008301static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008302{
Hanno Becker79594fd2019-05-08 09:38:41 +01008303 /* This function sets the pointers to match the case
8304 * of unprotected TLS/DTLS records, with both ssl->in_iv
8305 * and ssl->in_msg pointing to the beginning of the record
8306 * content.
8307 *
8308 * When decrypting a protected record, ssl->in_msg
8309 * will be shifted to point to the beginning of the
8310 * record plaintext.
8311 */
8312
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008313#if defined(MBEDTLS_SSL_PROTO_DTLS)
8314 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8315 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008316 /* This sets the header pointers to match records
8317 * without CID. When we receive a record containing
8318 * a CID, the fields are shifted accordingly in
8319 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008320 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008321#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008322 ssl->in_cid = ssl->in_ctr + 8;
8323 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008324#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008325 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008326#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008327 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008328 }
8329 else
8330#endif
8331 {
8332 ssl->in_ctr = ssl->in_hdr - 8;
8333 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008334#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008335 ssl->in_cid = ssl->in_len;
8336#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008337 ssl->in_iv = ssl->in_hdr + 5;
8338 }
8339
Hanno Becker79594fd2019-05-08 09:38:41 +01008340 /* This will be adjusted at record decryption time. */
8341 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008342}
8343
Paul Bakker5121ce52009-01-03 21:22:43 +00008344/*
8345 * Initialize an SSL context
8346 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008347void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8348{
8349 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8350}
8351
8352/*
8353 * Setup an SSL context
8354 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008355
8356static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8357{
8358 /* Set the incoming and outgoing record pointers. */
8359#if defined(MBEDTLS_SSL_PROTO_DTLS)
8360 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8361 {
8362 ssl->out_hdr = ssl->out_buf;
8363 ssl->in_hdr = ssl->in_buf;
8364 }
8365 else
8366#endif /* MBEDTLS_SSL_PROTO_DTLS */
8367 {
8368 ssl->out_hdr = ssl->out_buf + 8;
8369 ssl->in_hdr = ssl->in_buf + 8;
8370 }
8371
8372 /* Derive other internal pointers. */
8373 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008374 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008375}
8376
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008377int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008378 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008379{
Paul Bakker48916f92012-09-16 19:57:18 +00008380 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008381
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008382 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008383
8384 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008385 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008386 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008387
8388 /* Set to NULL in case of an error condition */
8389 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008390
Angus Grattond8213d02016-05-25 20:56:48 +10008391 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8392 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008393 {
Angus Grattond8213d02016-05-25 20:56:48 +10008394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008395 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008396 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008397 }
8398
8399 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8400 if( ssl->out_buf == NULL )
8401 {
8402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008403 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008404 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008405 }
8406
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008407 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008408
Paul Bakker48916f92012-09-16 19:57:18 +00008409 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008410 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008411
8412 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008413
8414error:
8415 mbedtls_free( ssl->in_buf );
8416 mbedtls_free( ssl->out_buf );
8417
8418 ssl->conf = NULL;
8419
8420 ssl->in_buf = NULL;
8421 ssl->out_buf = NULL;
8422
8423 ssl->in_hdr = NULL;
8424 ssl->in_ctr = NULL;
8425 ssl->in_len = NULL;
8426 ssl->in_iv = NULL;
8427 ssl->in_msg = NULL;
8428
8429 ssl->out_hdr = NULL;
8430 ssl->out_ctr = NULL;
8431 ssl->out_len = NULL;
8432 ssl->out_iv = NULL;
8433 ssl->out_msg = NULL;
8434
k-stachowiak9f7798e2018-07-31 16:52:32 +02008435 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008436}
8437
8438/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008439 * Reset an initialized and used SSL context for re-use while retaining
8440 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008441 *
8442 * If partial is non-zero, keep data in the input buffer and client ID.
8443 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008444 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008445static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008446{
Paul Bakker48916f92012-09-16 19:57:18 +00008447 int ret;
8448
Hanno Becker7e772132018-08-10 12:38:21 +01008449#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8450 !defined(MBEDTLS_SSL_SRV_C)
8451 ((void) partial);
8452#endif
8453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008454 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008455
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008456 /* Cancel any possibly running timer */
8457 ssl_set_timer( ssl, 0 );
8458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459#if defined(MBEDTLS_SSL_RENEGOTIATION)
8460 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008461 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008462
8463 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008464 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8465 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008466#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008468
Paul Bakker7eb013f2011-10-06 12:37:39 +00008469 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008470 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008471
8472 ssl->in_msgtype = 0;
8473 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008474#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008475 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008476 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008477#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008479 ssl_dtls_replay_reset( ssl );
8480#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008481
8482 ssl->in_hslen = 0;
8483 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008484
8485 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008486
8487 ssl->out_msgtype = 0;
8488 ssl->out_msglen = 0;
8489 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008490#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8491 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008492 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008493#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008494
Hanno Becker19859472018-08-06 09:40:20 +01008495 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8496
Paul Bakker48916f92012-09-16 19:57:18 +00008497 ssl->transform_in = NULL;
8498 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008499
Hanno Becker78640902018-08-13 16:35:15 +01008500 ssl->session_in = NULL;
8501 ssl->session_out = NULL;
8502
Angus Grattond8213d02016-05-25 20:56:48 +10008503 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008504
8505#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008506 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008507#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8508 {
8509 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008510 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008511 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8514 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8517 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8520 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008521 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008522 }
8523#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008524
Paul Bakker48916f92012-09-16 19:57:18 +00008525 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527 mbedtls_ssl_transform_free( ssl->transform );
8528 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008529 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008530 }
Paul Bakker48916f92012-09-16 19:57:18 +00008531
Paul Bakkerc0463502013-02-14 11:19:38 +01008532 if( ssl->session )
8533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008534 mbedtls_ssl_session_free( ssl->session );
8535 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008536 ssl->session = NULL;
8537 }
8538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008540 ssl->alpn_chosen = NULL;
8541#endif
8542
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008543#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008544#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008545 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008546#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008547 {
8548 mbedtls_free( ssl->cli_id );
8549 ssl->cli_id = NULL;
8550 ssl->cli_id_len = 0;
8551 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008552#endif
8553
Paul Bakker48916f92012-09-16 19:57:18 +00008554 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8555 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008556
8557 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008558}
8559
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008560/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008561 * Reset an initialized and used SSL context for re-use while retaining
8562 * all application-set variables, function pointers and data.
8563 */
8564int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8565{
8566 return( ssl_session_reset_int( ssl, 0 ) );
8567}
8568
8569/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008570 * SSL set accessors
8571 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008572void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008573{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008574 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008575}
8576
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008577void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008578{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008579 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008580}
8581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008583void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008585 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008586}
8587#endif
8588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008590void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008591{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008592 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008593}
8594#endif
8595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008597
Hanno Becker1841b0a2018-08-24 11:13:57 +01008598void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8599 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008600{
8601 ssl->disable_datagram_packing = !allow_packing;
8602}
8603
8604void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8605 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008606{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008607 conf->hs_timeout_min = min;
8608 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008609}
8610#endif
8611
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008612void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008613{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008614 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008615}
8616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008618void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008619 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008620 void *p_vrfy )
8621{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008622 conf->f_vrfy = f_vrfy;
8623 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008624}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008626
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008627void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008628 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008629 void *p_rng )
8630{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008631 conf->f_rng = f_rng;
8632 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008633}
8634
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008635void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008636 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008637 void *p_dbg )
8638{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008639 conf->f_dbg = f_dbg;
8640 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008641}
8642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008644 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008645 mbedtls_ssl_send_t *f_send,
8646 mbedtls_ssl_recv_t *f_recv,
8647 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008648{
8649 ssl->p_bio = p_bio;
8650 ssl->f_send = f_send;
8651 ssl->f_recv = f_recv;
8652 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008653}
8654
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008655#if defined(MBEDTLS_SSL_PROTO_DTLS)
8656void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8657{
8658 ssl->mtu = mtu;
8659}
8660#endif
8661
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008662void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008663{
8664 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008665}
8666
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008667void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8668 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008669 mbedtls_ssl_set_timer_t *f_set_timer,
8670 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008671{
8672 ssl->p_timer = p_timer;
8673 ssl->f_set_timer = f_set_timer;
8674 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008675
8676 /* Make sure we start with no timer running */
8677 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008678}
8679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008681void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008682 void *p_cache,
8683 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8684 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008685{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008686 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008687 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008688 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692#if defined(MBEDTLS_SSL_CLI_C)
8693int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008694{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008695 int ret;
8696
8697 if( ssl == NULL ||
8698 session == NULL ||
8699 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008700 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008702 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008703 }
8704
Hanno Becker52055ae2019-02-06 14:30:46 +00008705 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8706 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008707 return( ret );
8708
Paul Bakker0a597072012-09-25 21:55:46 +00008709 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008710
8711 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008712}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008713#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008714
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008715void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008716 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008717{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008718 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8719 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8720 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8721 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008722}
8723
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008724void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008725 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008726 int major, int minor )
8727{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008728 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008729 return;
8730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008732 return;
8733
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008734 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008735}
8736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008737#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008738void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008739 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008740{
8741 conf->cert_profile = profile;
8742}
8743
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008744/* Append a new keycert entry to a (possibly empty) list */
8745static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8746 mbedtls_x509_crt *cert,
8747 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008748{
niisato8ee24222018-06-25 19:05:48 +09008749 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008750
niisato8ee24222018-06-25 19:05:48 +09008751 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8752 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008753 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008754
niisato8ee24222018-06-25 19:05:48 +09008755 new_cert->cert = cert;
8756 new_cert->key = key;
8757 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008758
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008759 /* Update head is the list was null, else add to the end */
8760 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008761 {
niisato8ee24222018-06-25 19:05:48 +09008762 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008763 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008764 else
8765 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008766 mbedtls_ssl_key_cert *cur = *head;
8767 while( cur->next != NULL )
8768 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008769 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008770 }
8771
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008772 return( 0 );
8773}
8774
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008775int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008776 mbedtls_x509_crt *own_cert,
8777 mbedtls_pk_context *pk_key )
8778{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008779 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008780}
8781
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008782void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008783 mbedtls_x509_crt *ca_chain,
8784 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008785{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008786 conf->ca_chain = ca_chain;
8787 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008788
8789#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8790 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8791 * cannot be used together. */
8792 conf->f_ca_cb = NULL;
8793 conf->p_ca_cb = NULL;
8794#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008795}
Hanno Becker5adaad92019-03-27 16:54:37 +00008796
8797#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8798void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008799 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008800 void *p_ca_cb )
8801{
8802 conf->f_ca_cb = f_ca_cb;
8803 conf->p_ca_cb = p_ca_cb;
8804
8805 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8806 * cannot be used together. */
8807 conf->ca_chain = NULL;
8808 conf->ca_crl = NULL;
8809}
8810#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008811#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008812
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008813#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8814int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8815 mbedtls_x509_crt *own_cert,
8816 mbedtls_pk_context *pk_key )
8817{
8818 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8819 own_cert, pk_key ) );
8820}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008821
8822void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8823 mbedtls_x509_crt *ca_chain,
8824 mbedtls_x509_crl *ca_crl )
8825{
8826 ssl->handshake->sni_ca_chain = ca_chain;
8827 ssl->handshake->sni_ca_crl = ca_crl;
8828}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008829
8830void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8831 int authmode )
8832{
8833 ssl->handshake->sni_authmode = authmode;
8834}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008835#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8836
Hanno Becker8927c832019-04-03 12:52:50 +01008837#if defined(MBEDTLS_X509_CRT_PARSE_C)
8838void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8839 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8840 void *p_vrfy )
8841{
8842 ssl->f_vrfy = f_vrfy;
8843 ssl->p_vrfy = p_vrfy;
8844}
8845#endif
8846
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008847#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008848/*
8849 * Set EC J-PAKE password for current handshake
8850 */
8851int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8852 const unsigned char *pw,
8853 size_t pw_len )
8854{
8855 mbedtls_ecjpake_role role;
8856
Janos Follath8eb64132016-06-03 15:40:57 +01008857 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8859
8860 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8861 role = MBEDTLS_ECJPAKE_SERVER;
8862 else
8863 role = MBEDTLS_ECJPAKE_CLIENT;
8864
8865 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8866 role,
8867 MBEDTLS_MD_SHA256,
8868 MBEDTLS_ECP_DP_SECP256R1,
8869 pw, pw_len ) );
8870}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008871#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008873#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008874
8875static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8876{
8877 /* Remove reference to existing PSK, if any. */
8878#if defined(MBEDTLS_USE_PSA_CRYPTO)
8879 if( conf->psk_opaque != 0 )
8880 {
8881 /* The maintenance of the PSK key slot is the
8882 * user's responsibility. */
8883 conf->psk_opaque = 0;
8884 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008885 /* This and the following branch should never
8886 * be taken simultaenously as we maintain the
8887 * invariant that raw and opaque PSKs are never
8888 * configured simultaneously. As a safeguard,
8889 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008890#endif /* MBEDTLS_USE_PSA_CRYPTO */
8891 if( conf->psk != NULL )
8892 {
8893 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8894
8895 mbedtls_free( conf->psk );
8896 conf->psk = NULL;
8897 conf->psk_len = 0;
8898 }
8899
8900 /* Remove reference to PSK identity, if any. */
8901 if( conf->psk_identity != NULL )
8902 {
8903 mbedtls_free( conf->psk_identity );
8904 conf->psk_identity = NULL;
8905 conf->psk_identity_len = 0;
8906 }
8907}
8908
Hanno Becker7390c712018-11-15 13:33:04 +00008909/* This function assumes that PSK identity in the SSL config is unset.
8910 * It checks that the provided identity is well-formed and attempts
8911 * to make a copy of it in the SSL config.
8912 * On failure, the PSK identity in the config remains unset. */
8913static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8914 unsigned char const *psk_identity,
8915 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008916{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008917 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008918 if( psk_identity == NULL ||
8919 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008920 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008921 {
8922 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8923 }
8924
Hanno Becker7390c712018-11-15 13:33:04 +00008925 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8926 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008927 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008928
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008929 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008930 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008931
8932 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008933}
8934
Hanno Becker7390c712018-11-15 13:33:04 +00008935int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8936 const unsigned char *psk, size_t psk_len,
8937 const unsigned char *psk_identity, size_t psk_identity_len )
8938{
8939 int ret;
8940 /* Remove opaque/raw PSK + PSK Identity */
8941 ssl_conf_remove_psk( conf );
8942
8943 /* Check and set raw PSK */
8944 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8945 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8946 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8947 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8948 conf->psk_len = psk_len;
8949 memcpy( conf->psk, psk, conf->psk_len );
8950
8951 /* Check and set PSK Identity */
8952 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8953 if( ret != 0 )
8954 ssl_conf_remove_psk( conf );
8955
8956 return( ret );
8957}
8958
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008959static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8960{
8961#if defined(MBEDTLS_USE_PSA_CRYPTO)
8962 if( ssl->handshake->psk_opaque != 0 )
8963 {
8964 ssl->handshake->psk_opaque = 0;
8965 }
8966 else
8967#endif /* MBEDTLS_USE_PSA_CRYPTO */
8968 if( ssl->handshake->psk != NULL )
8969 {
8970 mbedtls_platform_zeroize( ssl->handshake->psk,
8971 ssl->handshake->psk_len );
8972 mbedtls_free( ssl->handshake->psk );
8973 ssl->handshake->psk_len = 0;
8974 }
8975}
8976
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008977int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8978 const unsigned char *psk, size_t psk_len )
8979{
8980 if( psk == NULL || ssl->handshake == NULL )
8981 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8982
8983 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8985
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008986 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008987
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008988 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008989 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008990
8991 ssl->handshake->psk_len = psk_len;
8992 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8993
8994 return( 0 );
8995}
8996
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008997#if defined(MBEDTLS_USE_PSA_CRYPTO)
8998int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008999 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009000 const unsigned char *psk_identity,
9001 size_t psk_identity_len )
9002{
Hanno Becker7390c712018-11-15 13:33:04 +00009003 int ret;
9004 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009005 ssl_conf_remove_psk( conf );
9006
Hanno Becker7390c712018-11-15 13:33:04 +00009007 /* Check and set opaque PSK */
9008 if( psk_slot == 0 )
9009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009010 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009011
9012 /* Check and set PSK Identity */
9013 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9014 psk_identity_len );
9015 if( ret != 0 )
9016 ssl_conf_remove_psk( conf );
9017
9018 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009019}
9020
9021int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009022 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009023{
9024 if( psk_slot == 0 || ssl->handshake == NULL )
9025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9026
9027 ssl_remove_psk( ssl );
9028 ssl->handshake->psk_opaque = psk_slot;
9029 return( 0 );
9030}
9031#endif /* MBEDTLS_USE_PSA_CRYPTO */
9032
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009033void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009035 size_t),
9036 void *p_psk )
9037{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009038 conf->f_psk = f_psk;
9039 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009040}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009042
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009043#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009044
9045#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009046int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009047{
9048 int ret;
9049
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009050 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9051 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9052 {
9053 mbedtls_mpi_free( &conf->dhm_P );
9054 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009055 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009057
9058 return( 0 );
9059}
Hanno Becker470a8c42017-10-04 15:28:46 +01009060#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009061
Hanno Beckera90658f2017-10-04 15:29:08 +01009062int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9063 const unsigned char *dhm_P, size_t P_len,
9064 const unsigned char *dhm_G, size_t G_len )
9065{
9066 int ret;
9067
9068 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9069 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9070 {
9071 mbedtls_mpi_free( &conf->dhm_P );
9072 mbedtls_mpi_free( &conf->dhm_G );
9073 return( ret );
9074 }
9075
9076 return( 0 );
9077}
Paul Bakker5121ce52009-01-03 21:22:43 +00009078
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009079int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009080{
9081 int ret;
9082
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009083 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9084 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9085 {
9086 mbedtls_mpi_free( &conf->dhm_P );
9087 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009088 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009089 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009090
9091 return( 0 );
9092}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009093#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009094
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009095#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9096/*
9097 * Set the minimum length for Diffie-Hellman parameters
9098 */
9099void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9100 unsigned int bitlen )
9101{
9102 conf->dhm_min_bitlen = bitlen;
9103}
9104#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9105
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009106#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009107/*
9108 * Set allowed/preferred hashes for handshake signatures
9109 */
9110void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9111 const int *hashes )
9112{
9113 conf->sig_hashes = hashes;
9114}
Hanno Becker947194e2017-04-07 13:25:49 +01009115#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009116
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009117#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009118/*
9119 * Set the allowed elliptic curves
9120 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009121void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009122 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009124 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009125}
Hanno Becker947194e2017-04-07 13:25:49 +01009126#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009127
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009128#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009129int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009130{
Hanno Becker947194e2017-04-07 13:25:49 +01009131 /* Initialize to suppress unnecessary compiler warning */
9132 size_t hostname_len = 0;
9133
9134 /* Check if new hostname is valid before
9135 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009136 if( hostname != NULL )
9137 {
9138 hostname_len = strlen( hostname );
9139
9140 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9142 }
9143
9144 /* Now it's clear that we will overwrite the old hostname,
9145 * so we can free it safely */
9146
9147 if( ssl->hostname != NULL )
9148 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009149 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009150 mbedtls_free( ssl->hostname );
9151 }
9152
9153 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009154
Paul Bakker5121ce52009-01-03 21:22:43 +00009155 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009156 {
9157 ssl->hostname = NULL;
9158 }
9159 else
9160 {
9161 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009162 if( ssl->hostname == NULL )
9163 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009164
Hanno Becker947194e2017-04-07 13:25:49 +01009165 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009166
Hanno Becker947194e2017-04-07 13:25:49 +01009167 ssl->hostname[hostname_len] = '\0';
9168 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009169
9170 return( 0 );
9171}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009172#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009173
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009174#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009175void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009177 const unsigned char *, size_t),
9178 void *p_sni )
9179{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009180 conf->f_sni = f_sni;
9181 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009183#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009185#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009186int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009187{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009188 size_t cur_len, tot_len;
9189 const char **p;
9190
9191 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009192 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9193 * MUST NOT be truncated."
9194 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009195 */
9196 tot_len = 0;
9197 for( p = protos; *p != NULL; p++ )
9198 {
9199 cur_len = strlen( *p );
9200 tot_len += cur_len;
9201
9202 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009204 }
9205
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009206 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009207
9208 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009209}
9210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009211const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009212{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009213 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009216
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009217void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009218{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009219 conf->max_major_ver = major;
9220 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009221}
9222
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009223void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009224{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009225 conf->min_major_ver = major;
9226 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009227}
9228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009229#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009230void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009231{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009232 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009233}
9234#endif
9235
Janos Follath088ce432017-04-10 12:42:31 +01009236#if defined(MBEDTLS_SSL_SRV_C)
9237void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9238 char cert_req_ca_list )
9239{
9240 conf->cert_req_ca_list = cert_req_ca_list;
9241}
9242#endif
9243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009244#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009245void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009246{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009247 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009248}
9249#endif
9250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009251#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009252void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009253{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009254 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009255}
9256#endif
9257
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009258#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009259void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009260{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009261 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009262}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009263#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009266int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009267{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009269 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009272 }
9273
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009274 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009275
9276 return( 0 );
9277}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009280#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009281void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009282{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009283 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009284}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009288void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009289{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009290 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009291}
9292#endif
9293
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009294void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009295{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009296 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009297}
9298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009300void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009301{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009302 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009303}
9304
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009305void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009306{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009307 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009308}
9309
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009310void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009311 const unsigned char period[8] )
9312{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009313 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009318#if defined(MBEDTLS_SSL_CLI_C)
9319void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009320{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009321 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009322}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009323#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009324
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009325#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009326void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9327 mbedtls_ssl_ticket_write_t *f_ticket_write,
9328 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9329 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009330{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009331 conf->f_ticket_write = f_ticket_write;
9332 conf->f_ticket_parse = f_ticket_parse;
9333 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009334}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009335#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009337
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009338#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9339void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9340 mbedtls_ssl_export_keys_t *f_export_keys,
9341 void *p_export_keys )
9342{
9343 conf->f_export_keys = f_export_keys;
9344 conf->p_export_keys = p_export_keys;
9345}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009346
9347void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9348 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9349 void *p_export_keys )
9350{
9351 conf->f_export_keys_ext = f_export_keys_ext;
9352 conf->p_export_keys = p_export_keys;
9353}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009354#endif
9355
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009356#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009357void mbedtls_ssl_conf_async_private_cb(
9358 mbedtls_ssl_config *conf,
9359 mbedtls_ssl_async_sign_t *f_async_sign,
9360 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9361 mbedtls_ssl_async_resume_t *f_async_resume,
9362 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009363 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009364{
9365 conf->f_async_sign_start = f_async_sign;
9366 conf->f_async_decrypt_start = f_async_decrypt;
9367 conf->f_async_resume = f_async_resume;
9368 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009369 conf->p_async_config_data = async_config_data;
9370}
9371
Gilles Peskine8f97af72018-04-26 11:46:10 +02009372void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9373{
9374 return( conf->p_async_config_data );
9375}
9376
Gilles Peskine1febfef2018-04-30 11:54:39 +02009377void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009378{
9379 if( ssl->handshake == NULL )
9380 return( NULL );
9381 else
9382 return( ssl->handshake->user_async_ctx );
9383}
9384
Gilles Peskine1febfef2018-04-30 11:54:39 +02009385void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009386 void *ctx )
9387{
9388 if( ssl->handshake != NULL )
9389 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009390}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009391#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009392
Paul Bakker5121ce52009-01-03 21:22:43 +00009393/*
9394 * SSL get accessors
9395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009397{
9398 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9399}
9400
Hanno Becker8b170a02017-10-10 11:51:19 +01009401int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9402{
9403 /*
9404 * Case A: We're currently holding back
9405 * a message for further processing.
9406 */
9407
9408 if( ssl->keep_current_message == 1 )
9409 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009410 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009411 return( 1 );
9412 }
9413
9414 /*
9415 * Case B: Further records are pending in the current datagram.
9416 */
9417
9418#if defined(MBEDTLS_SSL_PROTO_DTLS)
9419 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9420 ssl->in_left > ssl->next_record_offset )
9421 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009422 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009423 return( 1 );
9424 }
9425#endif /* MBEDTLS_SSL_PROTO_DTLS */
9426
9427 /*
9428 * Case C: A handshake message is being processed.
9429 */
9430
Hanno Becker8b170a02017-10-10 11:51:19 +01009431 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9432 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009433 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009434 return( 1 );
9435 }
9436
9437 /*
9438 * Case D: An application data message is being processed
9439 */
9440 if( ssl->in_offt != NULL )
9441 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009442 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009443 return( 1 );
9444 }
9445
9446 /*
9447 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009448 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009449 * we implement support for multiple alerts in single records.
9450 */
9451
9452 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9453 return( 0 );
9454}
9455
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009456uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009457{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009458 if( ssl->session != NULL )
9459 return( ssl->session->verify_result );
9460
9461 if( ssl->session_negotiate != NULL )
9462 return( ssl->session_negotiate->verify_result );
9463
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009464 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009465}
9466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009467const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009468{
Paul Bakker926c8e42013-03-06 10:23:34 +01009469 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009470 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009473}
9474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009478 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009479 {
9480 switch( ssl->minor_ver )
9481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009482 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009483 return( "DTLSv1.0" );
9484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009486 return( "DTLSv1.2" );
9487
9488 default:
9489 return( "unknown (DTLS)" );
9490 }
9491 }
9492#endif
9493
Paul Bakker43ca69c2011-01-15 17:35:19 +00009494 switch( ssl->minor_ver )
9495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009497 return( "SSLv3.0" );
9498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009500 return( "TLSv1.0" );
9501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009503 return( "TLSv1.1" );
9504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009506 return( "TLSv1.2" );
9507
Paul Bakker43ca69c2011-01-15 17:35:19 +00009508 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009509 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009510 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009511}
9512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009514{
Hanno Becker3136ede2018-08-17 15:28:19 +01009515 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009517 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009518
Hanno Becker5903de42019-05-03 14:46:38 +01009519 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9520
Hanno Becker78640902018-08-13 16:35:15 +01009521 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009522 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524#if defined(MBEDTLS_ZLIB_SUPPORT)
9525 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9526 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009527#endif
9528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531 case MBEDTLS_MODE_GCM:
9532 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009533 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009535 transform_expansion = transform->minlen;
9536 break;
9537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009539
9540 block_size = mbedtls_cipher_get_block_size(
9541 &transform->cipher_ctx_enc );
9542
Hanno Becker3136ede2018-08-17 15:28:19 +01009543 /* Expansion due to the addition of the MAC. */
9544 transform_expansion += transform->maclen;
9545
9546 /* Expansion due to the addition of CBC padding;
9547 * Theoretically up to 256 bytes, but we never use
9548 * more than the block size of the underlying cipher. */
9549 transform_expansion += block_size;
9550
9551 /* For TLS 1.1 or higher, an explicit IV is added
9552 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009553#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9554 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009555 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009556#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009557
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009558 break;
9559
9560 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009563 }
9564
Hanno Beckera0e20d02019-05-15 14:03:01 +01009565#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009566 if( transform->out_cid_len != 0 )
9567 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009568#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009569
Hanno Becker5903de42019-05-03 14:46:38 +01009570 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009571}
9572
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009573#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9574size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9575{
9576 size_t max_len;
9577
9578 /*
9579 * Assume mfl_code is correct since it was checked when set
9580 */
Angus Grattond8213d02016-05-25 20:56:48 +10009581 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009582
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009583 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009584 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009585 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009586 {
Angus Grattond8213d02016-05-25 20:56:48 +10009587 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009588 }
9589
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009590 /* During a handshake, use the value being negotiated */
9591 if( ssl->session_negotiate != NULL &&
9592 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9593 {
9594 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9595 }
9596
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009597 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009598}
9599#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9600
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009601#if defined(MBEDTLS_SSL_PROTO_DTLS)
9602static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9603{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009604 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9605 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9606 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9607 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9608 return ( 0 );
9609
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009610 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9611 return( ssl->mtu );
9612
9613 if( ssl->mtu == 0 )
9614 return( ssl->handshake->mtu );
9615
9616 return( ssl->mtu < ssl->handshake->mtu ?
9617 ssl->mtu : ssl->handshake->mtu );
9618}
9619#endif /* MBEDTLS_SSL_PROTO_DTLS */
9620
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009621int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9622{
9623 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9624
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009625#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9626 !defined(MBEDTLS_SSL_PROTO_DTLS)
9627 (void) ssl;
9628#endif
9629
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009630#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9631 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9632
9633 if( max_len > mfl )
9634 max_len = mfl;
9635#endif
9636
9637#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009638 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009639 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009640 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009641 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9642 const size_t overhead = (size_t) ret;
9643
9644 if( ret < 0 )
9645 return( ret );
9646
9647 if( mtu <= overhead )
9648 {
9649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9650 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9651 }
9652
9653 if( max_len > mtu - overhead )
9654 max_len = mtu - overhead;
9655 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009656#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009657
Hanno Becker0defedb2018-08-10 12:35:02 +01009658#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9659 !defined(MBEDTLS_SSL_PROTO_DTLS)
9660 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009661#endif
9662
9663 return( (int) max_len );
9664}
9665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666#if defined(MBEDTLS_X509_CRT_PARSE_C)
9667const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009668{
9669 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009670 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009671
Hanno Beckere6824572019-02-07 13:18:46 +00009672#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009673 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009674#else
9675 return( NULL );
9676#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009680#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009681int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9682 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009683{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009684 if( ssl == NULL ||
9685 dst == NULL ||
9686 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009687 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009690 }
9691
Hanno Becker52055ae2019-02-06 14:30:46 +00009692 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009695
Paul Bakker5121ce52009-01-03 21:22:43 +00009696/*
Paul Bakker1961b702013-01-25 14:49:24 +01009697 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009700{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009702
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009703 if( ssl == NULL || ssl->conf == NULL )
9704 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009707 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009711 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009713#endif
9714
Paul Bakker1961b702013-01-25 14:49:24 +01009715 return( ret );
9716}
9717
9718/*
9719 * Perform the SSL handshake
9720 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009721int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009722{
9723 int ret = 0;
9724
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009725 if( ssl == NULL || ssl->conf == NULL )
9726 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009730 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009733
9734 if( ret != 0 )
9735 break;
9736 }
9737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009739
9740 return( ret );
9741}
9742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743#if defined(MBEDTLS_SSL_RENEGOTIATION)
9744#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009745/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009746 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009749{
9750 int ret;
9751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009753
9754 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009755 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9756 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009757
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009758 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009759 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009760 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009761 return( ret );
9762 }
9763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009765
9766 return( 0 );
9767}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009769
9770/*
9771 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772 * - any side: calling mbedtls_ssl_renegotiate(),
9773 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9774 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009775 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009776 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009780{
9781 int ret;
9782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009784
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009785 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9786 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009787
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009788 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9789 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009791 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009793 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009794 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009795 ssl->handshake->out_msg_seq = 1;
9796 else
9797 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009798 }
9799#endif
9800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9802 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009807 return( ret );
9808 }
9809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009811
9812 return( 0 );
9813}
9814
9815/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009816 * Renegotiate current connection on client,
9817 * or request renegotiation on server
9818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009820{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009822
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009823 if( ssl == NULL || ssl->conf == NULL )
9824 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009827 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009828 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9831 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009834
9835 /* Did we already try/start sending HelloRequest? */
9836 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009838
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009839 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009840 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009844 /*
9845 * On client, either start the renegotiation process or,
9846 * if already in progress, continue the handshake
9847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009852
9853 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009856 return( ret );
9857 }
9858 }
9859 else
9860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009864 return( ret );
9865 }
9866 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009868
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009869 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009870}
9871
9872/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009873 * Check record counters and renegotiate if they're above the limit.
9874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009875static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009876{
Andres AG2196c7f2016-12-15 17:01:16 +00009877 size_t ep_len = ssl_ep_len( ssl );
9878 int in_ctr_cmp;
9879 int out_ctr_cmp;
9880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009881 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9882 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009883 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009884 {
9885 return( 0 );
9886 }
9887
Andres AG2196c7f2016-12-15 17:01:16 +00009888 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9889 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009890 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009891 ssl->conf->renego_period + ep_len, 8 - ep_len );
9892
9893 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009894 {
9895 return( 0 );
9896 }
9897
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009900}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009901#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009902
9903/*
9904 * Receive application data decrypted from the SSL layer
9905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009907{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009908 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009909 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009910
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009911 if( ssl == NULL || ssl->conf == NULL )
9912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009917 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009920 return( ret );
9921
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009922 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009924 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009925 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009926 return( ret );
9927 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009928 }
9929#endif
9930
Hanno Becker4a810fb2017-05-24 16:27:30 +01009931 /*
9932 * Check if renegotiation is necessary and/or handshake is
9933 * in process. If yes, perform/continue, and fall through
9934 * if an unexpected packet is received while the client
9935 * is waiting for the ServerHello.
9936 *
9937 * (There is no equivalent to the last condition on
9938 * the server-side as it is not treated as within
9939 * a handshake while waiting for the ClientHello
9940 * after a renegotiation request.)
9941 */
9942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009944 ret = ssl_check_ctr_renegotiate( ssl );
9945 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9946 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009949 return( ret );
9950 }
9951#endif
9952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009956 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9957 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009959 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009960 return( ret );
9961 }
9962 }
9963
Hanno Beckere41158b2017-10-23 13:30:32 +01009964 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009965 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009966 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009967 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009968 if( ssl->f_get_timer != NULL &&
9969 ssl->f_get_timer( ssl->p_timer ) == -1 )
9970 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009971 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009972 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009973
Hanno Becker327c93b2018-08-15 13:56:18 +01009974 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009975 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009976 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9977 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009978
Hanno Becker4a810fb2017-05-24 16:27:30 +01009979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9980 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009981 }
9982
9983 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009985 {
9986 /*
9987 * OpenSSL sends empty messages to randomize the IV
9988 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009989 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009991 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009992 return( 0 );
9993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009995 return( ret );
9996 }
9997 }
9998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009999 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010002
Hanno Becker4a810fb2017-05-24 16:27:30 +010010003 /*
10004 * - For client-side, expect SERVER_HELLO_REQUEST.
10005 * - For server-side, expect CLIENT_HELLO.
10006 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10007 */
10008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010009#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010010 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010011 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010012 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010015
10016 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010017#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010018 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010019 {
10020 continue;
10021 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010022#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010024 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010025#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010026
Hanno Becker4a810fb2017-05-24 16:27:30 +010010027#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010028 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010029 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010032
10033 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010035 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010036 {
10037 continue;
10038 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010041 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010042#endif /* MBEDTLS_SSL_SRV_C */
10043
Hanno Becker21df7f92017-10-17 11:03:26 +010010044#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010045 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010046 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10047 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10048 ssl->conf->allow_legacy_renegotiation ==
10049 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10050 {
10051 /*
10052 * Accept renegotiation request
10053 */
Paul Bakker48916f92012-09-16 19:57:18 +000010054
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010055 /* DTLS clients need to know renego is server-initiated */
10056#if defined(MBEDTLS_SSL_PROTO_DTLS)
10057 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10058 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10059 {
10060 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10061 }
10062#endif
10063 ret = ssl_start_renegotiation( ssl );
10064 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10065 ret != 0 )
10066 {
10067 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10068 return( ret );
10069 }
10070 }
10071 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010072#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010073 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010074 /*
10075 * Refuse renegotiation
10076 */
10077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080#if defined(MBEDTLS_SSL_PROTO_SSL3)
10081 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010082 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010083 /* SSLv3 does not have a "no_renegotiation" warning, so
10084 we send a fatal alert and abort the connection. */
10085 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10086 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10087 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010088 }
10089 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10091#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10092 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10093 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10096 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10097 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010098 {
10099 return( ret );
10100 }
Paul Bakker48916f92012-09-16 19:57:18 +000010101 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010102 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010103#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10104 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10107 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010108 }
Paul Bakker48916f92012-09-16 19:57:18 +000010109 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010110
Hanno Becker90333da2017-10-10 11:27:13 +010010111 /* At this point, we don't know whether the renegotiation has been
10112 * completed or not. The cases to consider are the following:
10113 * 1) The renegotiation is complete. In this case, no new record
10114 * has been read yet.
10115 * 2) The renegotiation is incomplete because the client received
10116 * an application data record while awaiting the ServerHello.
10117 * 3) The renegotiation is incomplete because the client received
10118 * a non-handshake, non-application data message while awaiting
10119 * the ServerHello.
10120 * In each of these case, looping will be the proper action:
10121 * - For 1), the next iteration will read a new record and check
10122 * if it's application data.
10123 * - For 2), the loop condition isn't satisfied as application data
10124 * is present, hence continue is the same as break
10125 * - For 3), the loop condition is satisfied and read_record
10126 * will re-deliver the message that was held back by the client
10127 * when expecting the ServerHello.
10128 */
10129 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010130 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010131#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010132 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010133 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010134 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010135 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010136 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010139 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010140 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010141 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010142 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010143 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10147 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010150 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010151 }
10152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010153 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10156 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010157 }
10158
10159 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010160
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010161 /* We're going to return something now, cancel timer,
10162 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010163 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010164 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010165
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010166#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010167 /* If we requested renego but received AppData, resend HelloRequest.
10168 * Do it now, after setting in_offt, to avoid taking this branch
10169 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010170#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010171 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010172 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010173 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010174 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010177 return( ret );
10178 }
10179 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010181#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010182 }
10183
10184 n = ( len < ssl->in_msglen )
10185 ? len : ssl->in_msglen;
10186
10187 memcpy( buf, ssl->in_offt, n );
10188 ssl->in_msglen -= n;
10189
10190 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010191 {
10192 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010193 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010194 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010195 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010196 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010197 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010198 /* more data available */
10199 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010200 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010203
Paul Bakker23986e52011-04-24 08:57:21 +000010204 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010205}
10206
10207/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010208 * Send application data to be encrypted by the SSL layer, taking care of max
10209 * fragment length and buffer size.
10210 *
10211 * According to RFC 5246 Section 6.2.1:
10212 *
10213 * Zero-length fragments of Application data MAY be sent as they are
10214 * potentially useful as a traffic analysis countermeasure.
10215 *
10216 * Therefore, it is possible that the input message length is 0 and the
10217 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010218 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010219static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010220 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010221{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010222 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10223 const size_t max_len = (size_t) ret;
10224
10225 if( ret < 0 )
10226 {
10227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10228 return( ret );
10229 }
10230
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010231 if( len > max_len )
10232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010234 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010237 "maximum fragment length: %d > %d",
10238 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010240 }
10241 else
10242#endif
10243 len = max_len;
10244 }
Paul Bakker887bd502011-06-08 13:10:54 +000010245
Paul Bakker5121ce52009-01-03 21:22:43 +000010246 if( ssl->out_left != 0 )
10247 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010248 /*
10249 * The user has previously tried to send the data and
10250 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10251 * written. In this case, we expect the high-level write function
10252 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010254 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010257 return( ret );
10258 }
10259 }
Paul Bakker887bd502011-06-08 13:10:54 +000010260 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010261 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010262 /*
10263 * The user is trying to send a message the first time, so we need to
10264 * copy the data into the internal buffers and setup the data structure
10265 * to keep track of partial writes
10266 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010267 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010268 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010269 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010270
Hanno Becker67bc7c32018-08-06 11:33:50 +010010271 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010274 return( ret );
10275 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010276 }
10277
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010278 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010279}
10280
10281/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010282 * Write application data, doing 1/n-1 splitting if necessary.
10283 *
10284 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010285 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010286 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010288#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010289static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010290 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010291{
10292 int ret;
10293
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010294 if( ssl->conf->cbc_record_splitting ==
10295 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010296 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10298 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10299 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010300 {
10301 return( ssl_write_real( ssl, buf, len ) );
10302 }
10303
10304 if( ssl->split_done == 0 )
10305 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010306 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010307 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010308 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010309 }
10310
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010311 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10312 return( ret );
10313 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010314
10315 return( ret + 1 );
10316}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010317#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010318
10319/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010320 * Write application data (public-facing wrapper)
10321 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010322int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010323{
10324 int ret;
10325
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010327
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010328 if( ssl == NULL || ssl->conf == NULL )
10329 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10330
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010331#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010332 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10333 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010334 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010335 return( ret );
10336 }
10337#endif
10338
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010339 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010340 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010341 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010342 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010344 return( ret );
10345 }
10346 }
10347
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010348#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010349 ret = ssl_write_split( ssl, buf, len );
10350#else
10351 ret = ssl_write_real( ssl, buf, len );
10352#endif
10353
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010355
10356 return( ret );
10357}
10358
10359/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010360 * Notify the peer that the connection is being closed
10361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010362int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010363{
10364 int ret;
10365
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010366 if( ssl == NULL || ssl->conf == NULL )
10367 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010370
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010371 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010372 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010374 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010376 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10377 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10378 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010381 return( ret );
10382 }
10383 }
10384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010386
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010387 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010388}
10389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010390void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010391{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010392 if( transform == NULL )
10393 return;
10394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010395#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010396 deflateEnd( &transform->ctx_deflate );
10397 inflateEnd( &transform->ctx_inflate );
10398#endif
10399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10401 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010402
Hanno Beckerd56ed242018-01-03 15:32:51 +000010403#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404 mbedtls_md_free( &transform->md_ctx_enc );
10405 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010406#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010407
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010408 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010409}
10410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411#if defined(MBEDTLS_X509_CRT_PARSE_C)
10412static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010413{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010414 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010415
10416 while( cur != NULL )
10417 {
10418 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010420 cur = next;
10421 }
10422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010423#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010424
Hanno Becker0271f962018-08-16 13:23:47 +010010425#if defined(MBEDTLS_SSL_PROTO_DTLS)
10426
10427static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10428{
10429 unsigned offset;
10430 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10431
10432 if( hs == NULL )
10433 return;
10434
Hanno Becker283f5ef2018-08-24 09:34:47 +010010435 ssl_free_buffered_record( ssl );
10436
Hanno Becker0271f962018-08-16 13:23:47 +010010437 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010438 ssl_buffering_free_slot( ssl, offset );
10439}
10440
10441static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10442 uint8_t slot )
10443{
10444 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10445 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010446
10447 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10448 return;
10449
Hanno Beckere605b192018-08-21 15:59:07 +010010450 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010451 {
Hanno Beckere605b192018-08-21 15:59:07 +010010452 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010453 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010454 mbedtls_free( hs_buf->data );
10455 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010456 }
10457}
10458
10459#endif /* MBEDTLS_SSL_PROTO_DTLS */
10460
Gilles Peskine9b562d52018-04-25 20:32:43 +020010461void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010462{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010463 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10464
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010465 if( handshake == NULL )
10466 return;
10467
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010468#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10469 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10470 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010471 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010472 handshake->async_in_progress = 0;
10473 }
10474#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10475
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010476#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10477 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10478 mbedtls_md5_free( &handshake->fin_md5 );
10479 mbedtls_sha1_free( &handshake->fin_sha1 );
10480#endif
10481#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10482#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010483#if defined(MBEDTLS_USE_PSA_CRYPTO)
10484 psa_hash_abort( &handshake->fin_sha256_psa );
10485#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010486 mbedtls_sha256_free( &handshake->fin_sha256 );
10487#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010488#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010489#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010490#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010491 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010492#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010493 mbedtls_sha512_free( &handshake->fin_sha512 );
10494#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010495#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010496#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010498#if defined(MBEDTLS_DHM_C)
10499 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010500#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010501#if defined(MBEDTLS_ECDH_C)
10502 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010503#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010504#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010505 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010506#if defined(MBEDTLS_SSL_CLI_C)
10507 mbedtls_free( handshake->ecjpake_cache );
10508 handshake->ecjpake_cache = NULL;
10509 handshake->ecjpake_cache_len = 0;
10510#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010511#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010512
Janos Follath4ae5c292016-02-10 11:27:43 +000010513#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10514 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010515 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010517#endif
10518
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010519#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10520 if( handshake->psk != NULL )
10521 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010522 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010523 mbedtls_free( handshake->psk );
10524 }
10525#endif
10526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010527#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10528 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010529 /*
10530 * Free only the linked list wrapper, not the keys themselves
10531 * since the belong to the SNI callback
10532 */
10533 if( handshake->sni_key_cert != NULL )
10534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010535 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010536
10537 while( cur != NULL )
10538 {
10539 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010540 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010541 cur = next;
10542 }
10543 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010544#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010545
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010546#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010547 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010548 if( handshake->ecrs_peer_cert != NULL )
10549 {
10550 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10551 mbedtls_free( handshake->ecrs_peer_cert );
10552 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010553#endif
10554
Hanno Becker75173122019-02-06 16:18:31 +000010555#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10556 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10557 mbedtls_pk_free( &handshake->peer_pubkey );
10558#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010560#if defined(MBEDTLS_SSL_PROTO_DTLS)
10561 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010562 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010563 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010564#endif
10565
Hanno Becker4a63ed42019-01-08 11:39:35 +000010566#if defined(MBEDTLS_ECDH_C) && \
10567 defined(MBEDTLS_USE_PSA_CRYPTO)
10568 psa_destroy_key( handshake->ecdh_psa_privkey );
10569#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10570
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010571 mbedtls_platform_zeroize( handshake,
10572 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010573}
10574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010575void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010576{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010577 if( session == NULL )
10578 return;
10579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010580#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010581 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010582#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010583
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010584#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010585 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010586#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010587
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010588 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010589}
10590
Paul Bakker5121ce52009-01-03 21:22:43 +000010591/*
10592 * Free an SSL context
10593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010594void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010595{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010596 if( ssl == NULL )
10597 return;
10598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010600
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010601 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010602 {
Angus Grattond8213d02016-05-25 20:56:48 +100010603 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010604 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010605 }
10606
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010607 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010608 {
Angus Grattond8213d02016-05-25 20:56:48 +100010609 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010610 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010611 }
10612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010613#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010614 if( ssl->compress_buf != NULL )
10615 {
Angus Grattond8213d02016-05-25 20:56:48 +100010616 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010618 }
10619#endif
10620
Paul Bakker48916f92012-09-16 19:57:18 +000010621 if( ssl->transform )
10622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010623 mbedtls_ssl_transform_free( ssl->transform );
10624 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010625 }
10626
10627 if( ssl->handshake )
10628 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010629 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10631 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010633 mbedtls_free( ssl->handshake );
10634 mbedtls_free( ssl->transform_negotiate );
10635 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010636 }
10637
Paul Bakkerc0463502013-02-14 11:19:38 +010010638 if( ssl->session )
10639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010640 mbedtls_ssl_session_free( ssl->session );
10641 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010642 }
10643
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010644#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010645 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010646 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010647 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010648 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010649 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010650#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10653 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10656 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010657 }
10658#endif
10659
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010660#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010661 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010662#endif
10663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010665
Paul Bakker86f04f42013-02-14 11:20:09 +010010666 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010667 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010668}
10669
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010670/*
10671 * Initialze mbedtls_ssl_config
10672 */
10673void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10674{
10675 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10676}
10677
Simon Butcherc97b6972015-12-27 23:48:17 +000010678#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010679static int ssl_preset_default_hashes[] = {
10680#if defined(MBEDTLS_SHA512_C)
10681 MBEDTLS_MD_SHA512,
10682 MBEDTLS_MD_SHA384,
10683#endif
10684#if defined(MBEDTLS_SHA256_C)
10685 MBEDTLS_MD_SHA256,
10686 MBEDTLS_MD_SHA224,
10687#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010688#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010689 MBEDTLS_MD_SHA1,
10690#endif
10691 MBEDTLS_MD_NONE
10692};
Simon Butcherc97b6972015-12-27 23:48:17 +000010693#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010694
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010695static int ssl_preset_suiteb_ciphersuites[] = {
10696 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10697 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10698 0
10699};
10700
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010701#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010702static int ssl_preset_suiteb_hashes[] = {
10703 MBEDTLS_MD_SHA256,
10704 MBEDTLS_MD_SHA384,
10705 MBEDTLS_MD_NONE
10706};
10707#endif
10708
10709#if defined(MBEDTLS_ECP_C)
10710static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010711#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010712 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010713#endif
10714#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010715 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010716#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010717 MBEDTLS_ECP_DP_NONE
10718};
10719#endif
10720
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010721/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010722 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010723 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010724int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010725 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010726{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010727#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010728 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010729#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010730
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010731 /* Use the functions here so that they are covered in tests,
10732 * but otherwise access member directly for efficiency */
10733 mbedtls_ssl_conf_endpoint( conf, endpoint );
10734 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010735
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010736 /*
10737 * Things that are common to all presets
10738 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010739#if defined(MBEDTLS_SSL_CLI_C)
10740 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10741 {
10742 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10743#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10744 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10745#endif
10746 }
10747#endif
10748
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010749#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010750 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010751#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010752
10753#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10754 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10755#endif
10756
10757#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10758 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10759#endif
10760
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010761#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10762 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10763#endif
10764
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010765#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010766 conf->f_cookie_write = ssl_cookie_write_dummy;
10767 conf->f_cookie_check = ssl_cookie_check_dummy;
10768#endif
10769
10770#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10771 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10772#endif
10773
Janos Follath088ce432017-04-10 12:42:31 +010010774#if defined(MBEDTLS_SSL_SRV_C)
10775 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10776#endif
10777
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010778#if defined(MBEDTLS_SSL_PROTO_DTLS)
10779 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10780 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10781#endif
10782
10783#if defined(MBEDTLS_SSL_RENEGOTIATION)
10784 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010785 memset( conf->renego_period, 0x00, 2 );
10786 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010787#endif
10788
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010789#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10790 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10791 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010792 const unsigned char dhm_p[] =
10793 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10794 const unsigned char dhm_g[] =
10795 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10796
Hanno Beckera90658f2017-10-04 15:29:08 +010010797 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10798 dhm_p, sizeof( dhm_p ),
10799 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010800 {
10801 return( ret );
10802 }
10803 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010804#endif
10805
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010806 /*
10807 * Preset-specific defaults
10808 */
10809 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010810 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010811 /*
10812 * NSA Suite B
10813 */
10814 case MBEDTLS_SSL_PRESET_SUITEB:
10815 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10816 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10817 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10818 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10819
10820 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10821 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10822 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10823 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10824 ssl_preset_suiteb_ciphersuites;
10825
10826#if defined(MBEDTLS_X509_CRT_PARSE_C)
10827 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010828#endif
10829
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010830#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010831 conf->sig_hashes = ssl_preset_suiteb_hashes;
10832#endif
10833
10834#if defined(MBEDTLS_ECP_C)
10835 conf->curve_list = ssl_preset_suiteb_curves;
10836#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010837 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010838
10839 /*
10840 * Default
10841 */
10842 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010843 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10844 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10845 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10846 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10847 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10848 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10849 MBEDTLS_SSL_MIN_MINOR_VERSION :
10850 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010851 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10852 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10853
10854#if defined(MBEDTLS_SSL_PROTO_DTLS)
10855 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10856 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10857#endif
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 mbedtls_ssl_list_ciphersuites();
10864
10865#if defined(MBEDTLS_X509_CRT_PARSE_C)
10866 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10867#endif
10868
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010869#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010870 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010871#endif
10872
10873#if defined(MBEDTLS_ECP_C)
10874 conf->curve_list = mbedtls_ecp_grp_id_list();
10875#endif
10876
10877#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10878 conf->dhm_min_bitlen = 1024;
10879#endif
10880 }
10881
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010882 return( 0 );
10883}
10884
10885/*
10886 * Free mbedtls_ssl_config
10887 */
10888void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10889{
10890#if defined(MBEDTLS_DHM_C)
10891 mbedtls_mpi_free( &conf->dhm_P );
10892 mbedtls_mpi_free( &conf->dhm_G );
10893#endif
10894
10895#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10896 if( conf->psk != NULL )
10897 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010898 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010899 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010900 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010901 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010902 }
10903
10904 if( conf->psk_identity != NULL )
10905 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010906 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010907 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010908 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010909 conf->psk_identity_len = 0;
10910 }
10911#endif
10912
10913#if defined(MBEDTLS_X509_CRT_PARSE_C)
10914 ssl_key_cert_free( conf->key_cert );
10915#endif
10916
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010917 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010918}
10919
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010920#if defined(MBEDTLS_PK_C) && \
10921 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010922/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010923 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010925unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010926{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010927#if defined(MBEDTLS_RSA_C)
10928 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10929 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010931#if defined(MBEDTLS_ECDSA_C)
10932 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10933 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010934#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010935 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010936}
10937
Hanno Becker7e5437a2017-04-28 17:15:26 +010010938unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10939{
10940 switch( type ) {
10941 case MBEDTLS_PK_RSA:
10942 return( MBEDTLS_SSL_SIG_RSA );
10943 case MBEDTLS_PK_ECDSA:
10944 case MBEDTLS_PK_ECKEY:
10945 return( MBEDTLS_SSL_SIG_ECDSA );
10946 default:
10947 return( MBEDTLS_SSL_SIG_ANON );
10948 }
10949}
10950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010951mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010952{
10953 switch( sig )
10954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010955#if defined(MBEDTLS_RSA_C)
10956 case MBEDTLS_SSL_SIG_RSA:
10957 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010959#if defined(MBEDTLS_ECDSA_C)
10960 case MBEDTLS_SSL_SIG_ECDSA:
10961 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010962#endif
10963 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010964 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010965 }
10966}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010967#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010968
Hanno Becker7e5437a2017-04-28 17:15:26 +010010969#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10970 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10971
10972/* Find an entry in a signature-hash set matching a given hash algorithm. */
10973mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10974 mbedtls_pk_type_t sig_alg )
10975{
10976 switch( sig_alg )
10977 {
10978 case MBEDTLS_PK_RSA:
10979 return( set->rsa );
10980 case MBEDTLS_PK_ECDSA:
10981 return( set->ecdsa );
10982 default:
10983 return( MBEDTLS_MD_NONE );
10984 }
10985}
10986
10987/* Add a signature-hash-pair to a signature-hash set */
10988void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10989 mbedtls_pk_type_t sig_alg,
10990 mbedtls_md_type_t md_alg )
10991{
10992 switch( sig_alg )
10993 {
10994 case MBEDTLS_PK_RSA:
10995 if( set->rsa == MBEDTLS_MD_NONE )
10996 set->rsa = md_alg;
10997 break;
10998
10999 case MBEDTLS_PK_ECDSA:
11000 if( set->ecdsa == MBEDTLS_MD_NONE )
11001 set->ecdsa = md_alg;
11002 break;
11003
11004 default:
11005 break;
11006 }
11007}
11008
11009/* Allow exactly one hash algorithm for each signature. */
11010void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11011 mbedtls_md_type_t md_alg )
11012{
11013 set->rsa = md_alg;
11014 set->ecdsa = md_alg;
11015}
11016
11017#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11018 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11019
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011020/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011021 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011023mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011024{
11025 switch( hash )
11026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011027#if defined(MBEDTLS_MD5_C)
11028 case MBEDTLS_SSL_HASH_MD5:
11029 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011031#if defined(MBEDTLS_SHA1_C)
11032 case MBEDTLS_SSL_HASH_SHA1:
11033 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011035#if defined(MBEDTLS_SHA256_C)
11036 case MBEDTLS_SSL_HASH_SHA224:
11037 return( MBEDTLS_MD_SHA224 );
11038 case MBEDTLS_SSL_HASH_SHA256:
11039 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011041#if defined(MBEDTLS_SHA512_C)
11042 case MBEDTLS_SSL_HASH_SHA384:
11043 return( MBEDTLS_MD_SHA384 );
11044 case MBEDTLS_SSL_HASH_SHA512:
11045 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011046#endif
11047 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011048 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011049 }
11050}
11051
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011052/*
11053 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11054 */
11055unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11056{
11057 switch( md )
11058 {
11059#if defined(MBEDTLS_MD5_C)
11060 case MBEDTLS_MD_MD5:
11061 return( MBEDTLS_SSL_HASH_MD5 );
11062#endif
11063#if defined(MBEDTLS_SHA1_C)
11064 case MBEDTLS_MD_SHA1:
11065 return( MBEDTLS_SSL_HASH_SHA1 );
11066#endif
11067#if defined(MBEDTLS_SHA256_C)
11068 case MBEDTLS_MD_SHA224:
11069 return( MBEDTLS_SSL_HASH_SHA224 );
11070 case MBEDTLS_MD_SHA256:
11071 return( MBEDTLS_SSL_HASH_SHA256 );
11072#endif
11073#if defined(MBEDTLS_SHA512_C)
11074 case MBEDTLS_MD_SHA384:
11075 return( MBEDTLS_SSL_HASH_SHA384 );
11076 case MBEDTLS_MD_SHA512:
11077 return( MBEDTLS_SSL_HASH_SHA512 );
11078#endif
11079 default:
11080 return( MBEDTLS_SSL_HASH_NONE );
11081 }
11082}
11083
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011084#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011085/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011086 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011087 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011088 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011089int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011090{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011091 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011092
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011093 if( ssl->conf->curve_list == NULL )
11094 return( -1 );
11095
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011096 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011097 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011098 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011099
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011100 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011101}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011102#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011103
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011104#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011105/*
11106 * Check if a hash proposed by the peer is in our list.
11107 * Return 0 if we're willing to use it, -1 otherwise.
11108 */
11109int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11110 mbedtls_md_type_t md )
11111{
11112 const int *cur;
11113
11114 if( ssl->conf->sig_hashes == NULL )
11115 return( -1 );
11116
11117 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11118 if( *cur == (int) md )
11119 return( 0 );
11120
11121 return( -1 );
11122}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011123#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011125#if defined(MBEDTLS_X509_CRT_PARSE_C)
11126int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11127 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011128 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011129 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011130{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011131 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011132#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011133 int usage = 0;
11134#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011135#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011136 const char *ext_oid;
11137 size_t ext_len;
11138#endif
11139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011140#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11141 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011142 ((void) cert);
11143 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011144 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011145#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011147#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11148 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011149 {
11150 /* Server part of the key exchange */
11151 switch( ciphersuite->key_exchange )
11152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011153 case MBEDTLS_KEY_EXCHANGE_RSA:
11154 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011155 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011156 break;
11157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011158 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11159 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11160 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11161 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011162 break;
11163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011164 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11165 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011166 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011167 break;
11168
11169 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011170 case MBEDTLS_KEY_EXCHANGE_NONE:
11171 case MBEDTLS_KEY_EXCHANGE_PSK:
11172 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11173 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011174 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011175 usage = 0;
11176 }
11177 }
11178 else
11179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011180 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11181 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011182 }
11183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011184 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011185 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011186 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011187 ret = -1;
11188 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011189#else
11190 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011191#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011193#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11194 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011196 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11197 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011198 }
11199 else
11200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011201 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11202 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011203 }
11204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011205 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011206 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011207 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011208 ret = -1;
11209 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011210#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011211
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011212 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011213}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011214#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011215
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011216/*
11217 * Convert version numbers to/from wire format
11218 * and, for DTLS, to/from TLS equivalent.
11219 *
11220 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011221 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011222 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11223 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011225void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011226 unsigned char ver[2] )
11227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011228#if defined(MBEDTLS_SSL_PROTO_DTLS)
11229 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011231 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011232 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11233
11234 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11235 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11236 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011237 else
11238#else
11239 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011240#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011241 {
11242 ver[0] = (unsigned char) major;
11243 ver[1] = (unsigned char) minor;
11244 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011245}
11246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011247void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011248 const unsigned char ver[2] )
11249{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011250#if defined(MBEDTLS_SSL_PROTO_DTLS)
11251 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011252 {
11253 *major = 255 - ver[0] + 2;
11254 *minor = 255 - ver[1] + 1;
11255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011256 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011257 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11258 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011259 else
11260#else
11261 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011262#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011263 {
11264 *major = ver[0];
11265 *minor = ver[1];
11266 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011267}
11268
Simon Butcher99000142016-10-13 17:21:01 +010011269int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11270{
11271#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11272 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11273 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11274
11275 switch( md )
11276 {
11277#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11278#if defined(MBEDTLS_MD5_C)
11279 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011280 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011281#endif
11282#if defined(MBEDTLS_SHA1_C)
11283 case MBEDTLS_SSL_HASH_SHA1:
11284 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11285 break;
11286#endif
11287#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11288#if defined(MBEDTLS_SHA512_C)
11289 case MBEDTLS_SSL_HASH_SHA384:
11290 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11291 break;
11292#endif
11293#if defined(MBEDTLS_SHA256_C)
11294 case MBEDTLS_SSL_HASH_SHA256:
11295 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11296 break;
11297#endif
11298 default:
11299 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11300 }
11301
11302 return 0;
11303#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11304 (void) ssl;
11305 (void) md;
11306
11307 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11308#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11309}
11310
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011311#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11312 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11313int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11314 unsigned char *output,
11315 unsigned char *data, size_t data_len )
11316{
11317 int ret = 0;
11318 mbedtls_md5_context mbedtls_md5;
11319 mbedtls_sha1_context mbedtls_sha1;
11320
11321 mbedtls_md5_init( &mbedtls_md5 );
11322 mbedtls_sha1_init( &mbedtls_sha1 );
11323
11324 /*
11325 * digitally-signed struct {
11326 * opaque md5_hash[16];
11327 * opaque sha_hash[20];
11328 * };
11329 *
11330 * md5_hash
11331 * MD5(ClientHello.random + ServerHello.random
11332 * + ServerParams);
11333 * sha_hash
11334 * SHA(ClientHello.random + ServerHello.random
11335 * + ServerParams);
11336 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011337 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011338 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011340 goto exit;
11341 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011342 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011343 ssl->handshake->randbytes, 64 ) ) != 0 )
11344 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011346 goto exit;
11347 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011348 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011349 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011351 goto exit;
11352 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011353 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011354 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011356 goto exit;
11357 }
11358
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011359 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011360 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011362 goto exit;
11363 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011364 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011365 ssl->handshake->randbytes, 64 ) ) != 0 )
11366 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011368 goto exit;
11369 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011370 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011371 data_len ) ) != 0 )
11372 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011374 goto exit;
11375 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011376 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011377 output + 16 ) ) != 0 )
11378 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011380 goto exit;
11381 }
11382
11383exit:
11384 mbedtls_md5_free( &mbedtls_md5 );
11385 mbedtls_sha1_free( &mbedtls_sha1 );
11386
11387 if( ret != 0 )
11388 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11389 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11390
11391 return( ret );
11392
11393}
11394#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11395 MBEDTLS_SSL_PROTO_TLS1_1 */
11396
11397#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11398 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011399
11400#if defined(MBEDTLS_USE_PSA_CRYPTO)
11401int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11402 unsigned char *hash, size_t *hashlen,
11403 unsigned char *data, size_t data_len,
11404 mbedtls_md_type_t md_alg )
11405{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011406 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011407 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011408 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11409
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011410 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011411
11412 if( ( status = psa_hash_setup( &hash_operation,
11413 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011414 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011415 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011416 goto exit;
11417 }
11418
Andrzej Kurek814feff2019-01-14 04:35:19 -050011419 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11420 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011421 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011422 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011423 goto exit;
11424 }
11425
Andrzej Kurek814feff2019-01-14 04:35:19 -050011426 if( ( status = psa_hash_update( &hash_operation,
11427 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011428 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011429 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011430 goto exit;
11431 }
11432
Andrzej Kurek814feff2019-01-14 04:35:19 -050011433 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11434 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011435 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011436 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011437 goto exit;
11438 }
11439
11440exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011441 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011442 {
11443 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11444 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011445 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011446 {
11447 case PSA_ERROR_NOT_SUPPORTED:
11448 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011449 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011450 case PSA_ERROR_BUFFER_TOO_SMALL:
11451 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11452 case PSA_ERROR_INSUFFICIENT_MEMORY:
11453 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11454 default:
11455 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11456 }
11457 }
11458 return( 0 );
11459}
11460
11461#else
11462
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011463int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011464 unsigned char *hash, size_t *hashlen,
11465 unsigned char *data, size_t data_len,
11466 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011467{
11468 int ret = 0;
11469 mbedtls_md_context_t ctx;
11470 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011471 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011472
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011474
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011475 mbedtls_md_init( &ctx );
11476
11477 /*
11478 * digitally-signed struct {
11479 * opaque client_random[32];
11480 * opaque server_random[32];
11481 * ServerDHParams params;
11482 * };
11483 */
11484 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11485 {
11486 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11487 goto exit;
11488 }
11489 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11490 {
11491 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11492 goto exit;
11493 }
11494 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11495 {
11496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11497 goto exit;
11498 }
11499 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11500 {
11501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11502 goto exit;
11503 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011504 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011505 {
11506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11507 goto exit;
11508 }
11509
11510exit:
11511 mbedtls_md_free( &ctx );
11512
11513 if( ret != 0 )
11514 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11515 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11516
11517 return( ret );
11518}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011519#endif /* MBEDTLS_USE_PSA_CRYPTO */
11520
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011521#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11522 MBEDTLS_SSL_PROTO_TLS1_2 */
11523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011524#endif /* MBEDTLS_SSL_TLS_C */