blob: 3930b471c262c604d87a59fb373efa3dbf4bf778 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Hanno Becker2a43f6f2018-08-10 11:12:52 +010062static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010063static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010064
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010070 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010071#else
72 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010073#endif
74 return( 0 );
75}
76
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077/*
78 * Start a timer.
79 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020083 if( ssl->f_set_timer == NULL )
84 return;
85
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
87 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088}
89
90/*
91 * Return -1 is timer is expired, 0 if it isn't.
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020094{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020095 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020096 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020097
98 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020099 {
100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200102 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103
104 return( 0 );
105}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200106
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100107static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
108 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100109static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100110
Hanno Beckercfe45792019-07-03 16:13:00 +0100111#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +0100112static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
113 unsigned char *buf,
114 size_t len,
115 mbedtls_record *rec );
116
Hanno Beckercfe45792019-07-03 16:13:00 +0100117int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
118 unsigned char *buf,
119 size_t buflen )
120{
Hanno Becker54229812019-07-12 14:40:00 +0100121 int ret = 0;
122 mbedtls_record rec;
123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
124 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
125
126 /* We don't support record checking in TLS because
127 * (a) there doesn't seem to be a usecase for it, and
128 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
129 * and we'd need to backup the transform here.
130 */
131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
132 {
133 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
134 goto exit;
135 }
136#if defined(MBEDTLS_SSL_PROTO_DTLS)
137 else
138 {
139 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
140 if( ret != 0 )
141 {
142 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
143 goto exit;
144 }
145
146 if( ssl->transform_in != NULL )
147 {
148 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
149 if( ret != 0 )
150 {
151 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
152 goto exit;
153 }
154 }
155 }
156#endif /* MBEDTLS_SSL_PROTO_DTLS */
157
158exit:
159 /* On success, we have decrypted the buffer in-place, so make
160 * sure we don't leak any plaintext data. */
161 mbedtls_platform_zeroize( buf, buflen );
162
163 /* For the purpose of this API, treat messages with unexpected CID
164 * as well as such from future epochs as unexpected. */
165 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
166 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
167 {
168 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
169 }
170
171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
172 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100173}
174#endif /* MBEDTLS_SSL_RECORD_CHECKING */
175
Hanno Becker67bc7c32018-08-06 11:33:50 +0100176#define SSL_DONT_FORCE_FLUSH 0
177#define SSL_FORCE_FLUSH 1
178
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200179#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100180
Hanno Beckera0e20d02019-05-15 14:03:01 +0100181#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100182/* Top-level Connection ID API */
183
Hanno Becker8367ccc2019-05-14 11:30:10 +0100184int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
185 size_t len,
186 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100187{
188 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
190
Hanno Becker611ac772019-05-14 11:45:26 +0100191 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
192 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
193 {
194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
195 }
196
197 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100198 conf->cid_len = len;
199 return( 0 );
200}
201
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100202int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
203 int enable,
204 unsigned char const *own_cid,
205 size_t own_cid_len )
206{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100207 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
209
Hanno Beckerca092242019-04-25 16:01:49 +0100210 ssl->negotiate_cid = enable;
211 if( enable == MBEDTLS_SSL_CID_DISABLED )
212 {
213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
214 return( 0 );
215 }
216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100217 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100218
Hanno Beckerad4a1372019-05-03 13:06:44 +0100219 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100220 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
222 (unsigned) own_cid_len,
223 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
225 }
226
227 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100228 /* Truncation is not an issue here because
229 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
230 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100231
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100232 return( 0 );
233}
234
235int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
236 int *enabled,
237 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
238 size_t *peer_cid_len )
239{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100240 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100241
Hanno Becker76a79ab2019-05-03 14:38:32 +0100242 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
243 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
244 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100246 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100247
Hanno Beckerc5f24222019-05-03 12:54:52 +0100248 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
249 * were used, but client and server requested the empty CID.
250 * This is indistinguishable from not using the CID extension
251 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100252 if( ssl->transform_in->in_cid_len == 0 &&
253 ssl->transform_in->out_cid_len == 0 )
254 {
255 return( 0 );
256 }
257
Hanno Becker615ef172019-05-22 16:50:35 +0100258 if( peer_cid_len != NULL )
259 {
260 *peer_cid_len = ssl->transform_in->out_cid_len;
261 if( peer_cid != NULL )
262 {
263 memcpy( peer_cid, ssl->transform_in->out_cid,
264 ssl->transform_in->out_cid_len );
265 }
266 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100267
268 *enabled = MBEDTLS_SSL_CID_ENABLED;
269
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100270 return( 0 );
271}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100272#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100273
Hanno Beckerd5847772018-08-28 10:09:23 +0100274/* Forward declarations for functions related to message buffering. */
275static void ssl_buffering_free( mbedtls_ssl_context *ssl );
276static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
277 uint8_t slot );
278static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
279static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
280static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
281static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100282static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
283 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100284static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100285
Hanno Beckera67dee22018-08-22 10:05:20 +0100286static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100287static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100288{
Hanno Becker11682cc2018-08-22 14:41:02 +0100289 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100290
291 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100292 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100293
294 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
295}
296
Hanno Becker67bc7c32018-08-06 11:33:50 +0100297static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
298{
Hanno Becker11682cc2018-08-22 14:41:02 +0100299 size_t const bytes_written = ssl->out_left;
300 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100301
302 /* Double-check that the write-index hasn't gone
303 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100304 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100305 {
306 /* Should never happen... */
307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
308 }
309
310 return( (int) ( mtu - bytes_written ) );
311}
312
313static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
314{
315 int ret;
316 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400317 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100318
319#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
320 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
321
322 if( max_len > mfl )
323 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100324
325 /* By the standard (RFC 6066 Sect. 4), the MFL extension
326 * only limits the maximum record payload size, so in theory
327 * we would be allowed to pack multiple records of payload size
328 * MFL into a single datagram. However, this would mean that there's
329 * no way to explicitly communicate MTU restrictions to the peer.
330 *
331 * The following reduction of max_len makes sure that we never
332 * write datagrams larger than MFL + Record Expansion Overhead.
333 */
334 if( max_len <= ssl->out_left )
335 return( 0 );
336
337 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100338#endif
339
340 ret = ssl_get_remaining_space_in_datagram( ssl );
341 if( ret < 0 )
342 return( ret );
343 remaining = (size_t) ret;
344
345 ret = mbedtls_ssl_get_record_expansion( ssl );
346 if( ret < 0 )
347 return( ret );
348 expansion = (size_t) ret;
349
350 if( remaining <= expansion )
351 return( 0 );
352
353 remaining -= expansion;
354 if( remaining >= max_len )
355 remaining = max_len;
356
357 return( (int) remaining );
358}
359
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200360/*
361 * Double the retransmit timeout value, within the allowed range,
362 * returning -1 if the maximum value has already been reached.
363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200365{
366 uint32_t new_timeout;
367
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200368 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200369 return( -1 );
370
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200371 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
372 * in the following way: after the initial transmission and a first
373 * retransmission, back off to a temporary estimated MTU of 508 bytes.
374 * This value is guaranteed to be deliverable (if not guaranteed to be
375 * delivered) of any compliant IPv4 (and IPv6) network, and should work
376 * on most non-IP stacks too. */
377 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400378 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200379 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
381 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200382
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200383 new_timeout = 2 * ssl->handshake->retransmit_timeout;
384
385 /* Avoid arithmetic overflow and range overflow */
386 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200387 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200388 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200389 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200390 }
391
392 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200394 ssl->handshake->retransmit_timeout ) );
395
396 return( 0 );
397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200400{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200401 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200403 ssl->handshake->retransmit_timeout ) );
404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200408/*
409 * Convert max_fragment_length codes to length.
410 * RFC 6066 says:
411 * enum{
412 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
413 * } MaxFragmentLength;
414 * and we add 0 -> extension unused
415 */
Angus Grattond8213d02016-05-25 20:56:48 +1000416static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200417{
Angus Grattond8213d02016-05-25 20:56:48 +1000418 switch( mfl )
419 {
420 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
421 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
422 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
423 return 512;
424 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
425 return 1024;
426 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
427 return 2048;
428 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
429 return 4096;
430 default:
431 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
432 }
433}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200435
Hanno Becker52055ae2019-02-06 14:30:46 +0000436int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
437 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200438{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ssl_session_free( dst );
440 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000443
444#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200445 if( src->peer_cert != NULL )
446 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200447 int ret;
448
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200449 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200450 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200451 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200456 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200459 dst->peer_cert = NULL;
460 return( ret );
461 }
462 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000463#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000464 if( src->peer_cert_digest != NULL )
465 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000466 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000467 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000468 if( dst->peer_cert_digest == NULL )
469 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
470
471 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
472 src->peer_cert_digest_len );
473 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000474 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000475 }
476#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200479
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200480#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200481 if( src->ticket != NULL )
482 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200483 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200484 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200485 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200486
487 memcpy( dst->ticket, src->ticket, src->ticket_len );
488 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200489#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200490
491 return( 0 );
492}
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
495int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200496 const unsigned char *key_enc, const unsigned char *key_dec,
497 size_t keylen,
498 const unsigned char *iv_enc, const unsigned char *iv_dec,
499 size_t ivlen,
500 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200501 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
503int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
504int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
505int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
506int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
507#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000508
Paul Bakker5121ce52009-01-03 21:22:43 +0000509/*
510 * Key material generation
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200513static int ssl3_prf( const unsigned char *secret, size_t slen,
514 const char *label,
515 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000516 unsigned char *dstbuf, size_t dlen )
517{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100518 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000519 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200520 mbedtls_md5_context md5;
521 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000522 unsigned char padding[16];
523 unsigned char sha1sum[20];
524 ((void)label);
525
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200526 mbedtls_md5_init( &md5 );
527 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200528
Paul Bakker5f70b252012-09-13 14:23:06 +0000529 /*
530 * SSLv3:
531 * block =
532 * MD5( secret + SHA1( 'A' + secret + random ) ) +
533 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
534 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
535 * ...
536 */
537 for( i = 0; i < dlen / 16; i++ )
538 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200539 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000540
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100541 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100542 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100543 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100544 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100545 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100546 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100547 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100548 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100549 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100550 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000551
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100552 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100553 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100554 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100555 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100556 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100557 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100558 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100559 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000560 }
561
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100562exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200563 mbedtls_md5_free( &md5 );
564 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000565
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500566 mbedtls_platform_zeroize( padding, sizeof( padding ) );
567 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000568
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100569 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200574static int tls1_prf( const unsigned char *secret, size_t slen,
575 const char *label,
576 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000577 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578{
Paul Bakker23986e52011-04-24 08:57:21 +0000579 size_t nb, hs;
580 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200581 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300582 unsigned char *tmp;
583 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 const mbedtls_md_info_t *md_info;
586 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100587 int ret;
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Ron Eldor3b350852019-05-07 18:31:49 +0300591 tmp_len = 20 + strlen( label ) + rlen;
592 tmp = mbedtls_calloc( 1, tmp_len );
593 if( tmp == NULL )
594 {
595 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
596 goto exit;
597 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 hs = ( slen + 1 ) / 2;
600 S1 = secret;
601 S2 = secret + slen - hs;
602
603 nb = strlen( label );
604 memcpy( tmp + 20, label, nb );
605 memcpy( tmp + 20 + nb, random, rlen );
606 nb += rlen;
607
608 /*
609 * First compute P_md5(secret,label+random)[0..dlen]
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300612 {
613 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
614 goto exit;
615 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300618 {
619 goto exit;
620 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
623 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
624 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626 for( i = 0; i < dlen; i += 16 )
627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_md_hmac_reset ( &md_ctx );
629 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
630 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
634 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
637
638 for( j = 0; j < k; j++ )
639 dstbuf[i + j] = h_i[j];
640 }
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100643
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 /*
645 * XOR out with P_sha1(secret,label+random)[0..dlen]
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300648 {
649 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
650 goto exit;
651 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300654 {
655 goto exit;
656 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
659 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
660 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
662 for( i = 0; i < dlen; i += 20 )
663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_md_hmac_reset ( &md_ctx );
665 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
666 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_hmac_reset ( &md_ctx );
669 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
670 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
673
674 for( j = 0; j < k; j++ )
675 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
676 }
677
Ron Eldor3b350852019-05-07 18:31:49 +0300678exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100680
Ron Eldor3b350852019-05-07 18:31:49 +0300681 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500682 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Ron Eldor3b350852019-05-07 18:31:49 +0300684 mbedtls_free( tmp );
685 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500690#if defined(MBEDTLS_USE_PSA_CRYPTO)
691static int tls_prf_generic( mbedtls_md_type_t md_type,
692 const unsigned char *secret, size_t slen,
693 const char *label,
694 const unsigned char *random, size_t rlen,
695 unsigned char *dstbuf, size_t dlen )
696{
697 psa_status_t status;
698 psa_algorithm_t alg;
Janos Follath53b8ec22019-08-08 10:28:27 +0100699 psa_key_attributes_t key_attributes;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500700 psa_key_handle_t master_slot;
Janos Follathda6ac012019-08-16 13:47:29 +0100701 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100702 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500703
Andrzej Kurekc929a822019-01-14 03:51:11 -0500704 if( md_type == MBEDTLS_MD_SHA384 )
705 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
706 else
707 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
708
Janos Follath53b8ec22019-08-08 10:28:27 +0100709 key_attributes = psa_key_attributes_init();
710 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
711 psa_set_key_algorithm( &key_attributes, alg );
712 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500713
Janos Follath53b8ec22019-08-08 10:28:27 +0100714 status = psa_import_key( &key_attributes, secret, slen, &master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500715 if( status != PSA_SUCCESS )
716 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
717
Janos Follathda6ac012019-08-16 13:47:29 +0100718 status = psa_key_derivation( &derivation,
Andrzej Kurekc929a822019-01-14 03:51:11 -0500719 master_slot, alg,
720 random, rlen,
721 (unsigned char const *) label,
722 (size_t) strlen( label ),
723 dlen );
724 if( status != PSA_SUCCESS )
725 {
Janos Follathda6ac012019-08-16 13:47:29 +0100726 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500727 psa_destroy_key( master_slot );
728 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
729 }
730
Janos Follathda6ac012019-08-16 13:47:29 +0100731 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500732 if( status != PSA_SUCCESS )
733 {
Janos Follathda6ac012019-08-16 13:47:29 +0100734 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500735 psa_destroy_key( master_slot );
736 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
737 }
738
Janos Follathda6ac012019-08-16 13:47:29 +0100739 status = psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500740 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500741 {
742 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500743 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500744 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500745
746 status = psa_destroy_key( master_slot );
747 if( status != PSA_SUCCESS )
748 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
749
Andrzej Kurek33171262019-01-15 03:25:18 -0500750 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500751}
752
753#else /* MBEDTLS_USE_PSA_CRYPTO */
754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100756 const unsigned char *secret, size_t slen,
757 const char *label,
758 const unsigned char *random, size_t rlen,
759 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000760{
761 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100762 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300763 unsigned char *tmp;
764 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
Ron Eldor3b350852019-05-07 18:31:49 +0300777 tmp_len = md_len + strlen( label ) + rlen;
778 tmp = mbedtls_calloc( 1, tmp_len );
779 if( tmp == NULL )
780 {
781 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
782 goto exit;
783 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000784
785 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100786 memcpy( tmp + md_len, label, nb );
787 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000788 nb += rlen;
789
790 /*
791 * Compute P_<hash>(secret, label + random)[0..dlen]
792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300794 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
797 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
798 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100799
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100800 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_md_hmac_reset ( &md_ctx );
803 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
804 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_hmac_reset ( &md_ctx );
807 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
808 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000809
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100810 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000811
812 for( j = 0; j < k; j++ )
813 dstbuf[i + j] = h_i[j];
814 }
815
Ron Eldor3b350852019-05-07 18:31:49 +0300816exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100818
Ron Eldor3b350852019-05-07 18:31:49 +0300819 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500820 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000821
Ron Eldor3b350852019-05-07 18:31:49 +0300822 mbedtls_free( tmp );
823
824 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000825}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500826#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100828static int tls_prf_sha256( const unsigned char *secret, size_t slen,
829 const char *label,
830 const unsigned char *random, size_t rlen,
831 unsigned char *dstbuf, size_t dlen )
832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100834 label, random, rlen, dstbuf, dlen ) );
835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200839static int tls_prf_sha384( const unsigned char *secret, size_t slen,
840 const char *label,
841 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000842 unsigned char *dstbuf, size_t dlen )
843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100845 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_SHA512_C */
848#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
853 defined(MBEDTLS_SSL_PROTO_TLS1_1)
854static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#endif
Paul Bakker380da532012-04-18 16:10:25 +0000856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200858static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200860#endif
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200863static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200865#endif
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
868#if defined(MBEDTLS_SHA256_C)
869static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200870static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_SHA512_C)
875static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200876static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000880
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100881#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100882 defined(MBEDTLS_USE_PSA_CRYPTO)
883static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
884{
885 if( ssl->conf->f_psk != NULL )
886 {
887 /* If we've used a callback to select the PSK,
888 * the static configuration is irrelevant. */
889 if( ssl->handshake->psk_opaque != 0 )
890 return( 1 );
891
892 return( 0 );
893 }
894
895 if( ssl->conf->psk_opaque != 0 )
896 return( 1 );
897
898 return( 0 );
899}
900#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100901 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100902
Ron Eldorcf280092019-05-14 20:19:13 +0300903#if defined(MBEDTLS_SSL_EXPORT_KEYS)
904static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
905{
906#if defined(MBEDTLS_SSL_PROTO_SSL3)
907 if( tls_prf == ssl3_prf )
908 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300909 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300910 }
911 else
912#endif
913#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
914 if( tls_prf == tls1_prf )
915 {
916 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
917 }
918 else
919#endif
920#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
921#if defined(MBEDTLS_SHA512_C)
922 if( tls_prf == tls_prf_sha384 )
923 {
924 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
925 }
926 else
927#endif
928#if defined(MBEDTLS_SHA256_C)
929 if( tls_prf == tls_prf_sha256 )
930 {
931 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
932 }
933 else
934#endif
935#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
936 return( MBEDTLS_SSL_TLS_PRF_NONE );
937}
938#endif /* MBEDTLS_SSL_EXPORT_KEYS */
939
Ron Eldor51d3ab52019-05-12 14:54:30 +0300940int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
941 const unsigned char *secret, size_t slen,
942 const char *label,
943 const unsigned char *random, size_t rlen,
944 unsigned char *dstbuf, size_t dlen )
945{
946 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
947
948 switch( prf )
949 {
950#if defined(MBEDTLS_SSL_PROTO_SSL3)
951 case MBEDTLS_SSL_TLS_PRF_SSL3:
952 tls_prf = ssl3_prf;
953 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300954#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300955#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
956 case MBEDTLS_SSL_TLS_PRF_TLS1:
957 tls_prf = tls1_prf;
958 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300959#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
960
961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300962#if defined(MBEDTLS_SHA512_C)
963 case MBEDTLS_SSL_TLS_PRF_SHA384:
964 tls_prf = tls_prf_sha384;
965 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300966#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300967#if defined(MBEDTLS_SHA256_C)
968 case MBEDTLS_SSL_TLS_PRF_SHA256:
969 tls_prf = tls_prf_sha256;
970 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300971#endif /* MBEDTLS_SHA256_C */
972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300973 default:
974 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
975 }
976
977 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
978}
979
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +0200980/* Type for the TLS PRF */
981typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
982 const unsigned char *, size_t,
983 unsigned char *, size_t);
984
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200985/*
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200986 * Populate a transform structure with session keys and all the other
987 * necessary information.
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200988 *
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200989 * Parameters:
990 * - [in/out]: transform: structure to populate
991 * [in] must be just initialised with mbedtls_ssl_transform_init()
992 * [out] fully populate, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +0200993 * - [in] session: used: ciphersuite, encrypt_then_mac, master, compression
994 * - [in] tls_prf: pointer to PRF to use for key derivation
995 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
996 * - [in] ssl: used members: minor_ver, conf->endpoint
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200997 */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200998static int ssl_populate_transform( mbedtls_ssl_transform *transform,
999 const mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001000 ssl_tls_prf_t tls_prf,
1001 const unsigned char randbytes[64],
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001002 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001003{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001004 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001005#if defined(MBEDTLS_USE_PSA_CRYPTO)
1006 int psa_fallthrough;
1007#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001008 unsigned char keyblk[256];
1009 unsigned char *key1;
1010 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001011 unsigned char *mac_enc;
1012 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001013 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001014 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001015 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001016 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 const mbedtls_cipher_info_t *cipher_info;
1018 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001019
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001020 /* Copy info about negotiated version and extensions */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001021#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1022 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001023 transform->encrypt_then_mac = session->encrypt_then_mac;
1024#endif
1025 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001026
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001027 /*
1028 * Get various info structures
1029 */
1030 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1031 if( ciphersuite_info == NULL )
1032 {
1033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
1034 session->ciphersuite ) );
1035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1036 }
1037
Hanno Beckere694c3e2017-12-27 21:34:08 +00001038 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001039 if( cipher_info == NULL )
1040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001042 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001044 }
1045
Hanno Beckere694c3e2017-12-27 21:34:08 +00001046 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001047 if( md_info == NULL )
1048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001050 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001052 }
1053
Hanno Beckera0e20d02019-05-15 14:03:01 +01001054#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001055 /* Copy own and peer's CID if the use of the CID
1056 * extension has been negotiated. */
1057 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1058 {
1059 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001060
Hanno Becker05154c32019-05-03 15:23:51 +01001061 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001062 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001063 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001064 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001065
1066 transform->out_cid_len = ssl->handshake->peer_cid_len;
1067 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1068 ssl->handshake->peer_cid_len );
1069 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1070 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001071 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001072#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001073
Paul Bakker5121ce52009-01-03 21:22:43 +00001074 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001075 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 */
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001077 ret = tls_prf( session->master, 48, "key expansion",
1078 randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001079 if( ret != 0 )
1080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001082 return( ret );
1083 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1086 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1087 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001088 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 /*
1092 * Determine the appropriate key, IV and MAC length.
1093 */
Paul Bakker68884e32013-01-07 18:20:04 +01001094
Hanno Becker88aaf652017-12-27 08:17:40 +00001095 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001096
Hanno Becker8031d062018-01-03 15:32:31 +00001097#if defined(MBEDTLS_GCM_C) || \
1098 defined(MBEDTLS_CCM_C) || \
1099 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001101 cipher_info->mode == MBEDTLS_MODE_CCM ||
1102 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001103 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001104 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001105
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001106 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001107 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001108 transform->taglen =
1109 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001110
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001111 /* All modes haves 96-bit IVs;
1112 * GCM and CCM has 4 implicit and 8 explicit bytes
1113 * ChachaPoly has all 12 bytes implicit
1114 */
Paul Bakker68884e32013-01-07 18:20:04 +01001115 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001116 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1117 transform->fixed_ivlen = 12;
1118 else
1119 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001120
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001121 /* Minimum length of encrypted record */
1122 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001123 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001124 }
1125 else
Hanno Becker8031d062018-01-03 15:32:31 +00001126#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1127#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1128 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1129 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001130 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001131 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1133 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001136 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001139 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001140 mac_key_len = mbedtls_md_get_size( md_info );
1141 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001144 /*
1145 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1146 * (rfc 6066 page 13 or rfc 2104 section 4),
1147 * so we only need to adjust the length here.
1148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001152
1153#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1154 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001155 * HMAC implementation which also truncates the key
1156 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001157 mac_key_len = transform->maclen;
1158#endif
1159 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001161
1162 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001163 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001165 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001167 transform->minlen = transform->maclen;
1168 else
Paul Bakker68884e32013-01-07 18:20:04 +01001169 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001170 /*
1171 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001172 * 1. if EtM is in use: one block plus MAC
1173 * otherwise: * first multiple of blocklen greater than maclen
1174 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1177 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001178 {
1179 transform->minlen = transform->maclen
1180 + cipher_info->block_size;
1181 }
1182 else
1183#endif
1184 {
1185 transform->minlen = transform->maclen
1186 + cipher_info->block_size
1187 - transform->maclen % cipher_info->block_size;
1188 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1191 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1192 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001193 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001194 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001195#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1197 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1198 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001199 {
1200 transform->minlen += transform->ivlen;
1201 }
1202 else
1203#endif
1204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001206 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1207 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001208 }
Paul Bakker68884e32013-01-07 18:20:04 +01001209 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 }
Hanno Becker8031d062018-01-03 15:32:31 +00001211 else
1212#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1213 {
1214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1216 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001217
Hanno Becker88aaf652017-12-27 08:17:40 +00001218 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1219 (unsigned) keylen,
1220 (unsigned) transform->minlen,
1221 (unsigned) transform->ivlen,
1222 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001223
1224 /*
1225 * Finally setup the cipher contexts, IVs and MAC secrets.
1226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001228 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001230 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001231 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
Paul Bakker68884e32013-01-07 18:20:04 +01001233 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001234 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001235
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001236 /*
1237 * This is not used in TLS v1.1.
1238 */
Paul Bakker48916f92012-09-16 19:57:18 +00001239 iv_copy_len = ( transform->fixed_ivlen ) ?
1240 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001241 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1242 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001243 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 }
1245 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#endif /* MBEDTLS_SSL_CLI_C */
1247#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001248 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001250 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001251 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Hanno Becker81c7b182017-11-09 18:39:33 +00001253 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001254 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001256 /*
1257 * This is not used in TLS v1.1.
1258 */
Paul Bakker48916f92012-09-16 19:57:18 +00001259 iv_copy_len = ( transform->fixed_ivlen ) ?
1260 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001261 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1262 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001263 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001265 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001269 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1270 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001271 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Hanno Beckerd56ed242018-01-03 15:32:51 +00001273#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#if defined(MBEDTLS_SSL_PROTO_SSL3)
1275 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001276 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001277 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001280 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1281 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001282 }
1283
Hanno Becker81c7b182017-11-09 18:39:33 +00001284 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1285 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001286 }
1287 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1289#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1290 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1291 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001292 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001293 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1294 For AEAD-based ciphersuites, there is nothing to do here. */
1295 if( mac_key_len != 0 )
1296 {
1297 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1298 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1299 }
Paul Bakker68884e32013-01-07 18:20:04 +01001300 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001301 else
1302#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001305 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1306 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001307 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001308#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1311 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001312 {
1313 int ret = 0;
1314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001316
Hanno Becker88aaf652017-12-27 08:17:40 +00001317 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001318 transform->iv_enc, transform->iv_dec,
1319 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001320 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001321 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001324 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1325 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001326 }
1327 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001328#else
1329 ((void) mac_dec);
1330 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001332
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001333#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1334 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001335 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001336 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1337 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001338 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001339 iv_copy_len );
1340 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001341
1342 if( ssl->conf->f_export_keys_ext != NULL )
1343 {
1344 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1345 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001346 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001347 iv_copy_len,
Manuel Pégourié-Gonnard344460c2019-07-25 13:17:38 +02001348 /* work around bug in exporter type */
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001349 (unsigned char *) randbytes + 32,
1350 (unsigned char *) randbytes,
1351 tls_prf_get_type( tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001352 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001353#endif
1354
Hanno Beckerf704bef2018-11-16 15:21:18 +00001355#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001356
1357 /* Only use PSA-based ciphers for TLS-1.2.
1358 * That's relevant at least for TLS-1.0, where
1359 * we assume that mbedtls_cipher_crypt() updates
1360 * the structure field for the IV, which the PSA-based
1361 * implementation currently doesn't. */
1362#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1363 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001364 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001365 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001366 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001367 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1368 {
1369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001370 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001371 }
1372
1373 if( ret == 0 )
1374 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001375 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001376 psa_fallthrough = 0;
1377 }
1378 else
1379 {
1380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1381 psa_fallthrough = 1;
1382 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001383 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001384 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001385 psa_fallthrough = 1;
1386#else
1387 psa_fallthrough = 1;
1388#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001389
Hanno Beckercb1cc802018-11-17 22:27:38 +00001390 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001391#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001392 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001393 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001396 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001397 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001398
Hanno Beckerf704bef2018-11-16 15:21:18 +00001399#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001400 /* Only use PSA-based ciphers for TLS-1.2.
1401 * That's relevant at least for TLS-1.0, where
1402 * we assume that mbedtls_cipher_crypt() updates
1403 * the structure field for the IV, which the PSA-based
1404 * implementation currently doesn't. */
1405#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1406 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001407 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001408 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001409 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001410 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1411 {
1412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001413 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001414 }
1415
1416 if( ret == 0 )
1417 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001419 psa_fallthrough = 0;
1420 }
1421 else
1422 {
1423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1424 psa_fallthrough = 1;
1425 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001426 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001427 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001428 psa_fallthrough = 1;
1429#else
1430 psa_fallthrough = 1;
1431#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001432
Hanno Beckercb1cc802018-11-17 22:27:38 +00001433 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001434#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001435 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001436 cipher_info ) ) != 0 )
1437 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001439 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001440 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001443 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001447 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001448 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001451 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001455 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001456 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#if defined(MBEDTLS_CIPHER_MODE_CBC)
1459 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1462 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001465 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001466 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1469 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001472 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001476
Paul Bakker5121ce52009-01-03 21:22:43 +00001477
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001478 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001483
Paul Bakker48916f92012-09-16 19:57:18 +00001484 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1485 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001486
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001487 if( deflateInit( &transform->ctx_deflate,
1488 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001489 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001492 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1493 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001494 }
1495 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001497
Ron Eldore6992702019-05-07 18:27:13 +03001498end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001499 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Ron Eldore6992702019-05-07 18:27:13 +03001500 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001501}
1502
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001503/*
1504 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
1505 *
1506 * Inputs:
1507 * - SSL/TLS minor version
1508 * - hash associated with the ciphersuite (only used by TLS 1.2)
1509 *
1510 * Ouputs:
1511 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1512 */
1513static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1514 int minor_ver,
1515 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001516{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001517#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1518 (void) hash;
1519#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001520
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001521#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001522 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001523 {
1524 handshake->tls_prf = ssl3_prf;
1525 handshake->calc_verify = ssl_calc_verify_ssl;
1526 handshake->calc_finished = ssl_calc_finished_ssl;
1527 }
1528 else
1529#endif
1530#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001531 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001532 {
1533 handshake->tls_prf = tls1_prf;
1534 handshake->calc_verify = ssl_calc_verify_tls;
1535 handshake->calc_finished = ssl_calc_finished_tls;
1536 }
1537 else
1538#endif
1539#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1540#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001541 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1542 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001543 {
1544 handshake->tls_prf = tls_prf_sha384;
1545 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1546 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1547 }
1548 else
1549#endif
1550#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001551 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001552 {
1553 handshake->tls_prf = tls_prf_sha256;
1554 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1555 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1556 }
1557 else
1558#endif
1559#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1560 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001561 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1562 }
1563
1564 return( 0 );
1565}
1566
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001567/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001568 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001569 *
1570 * Parameters:
1571 * [in/out] handshake
1572 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001573 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001574 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001575 * [out] master
1576 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1577 * debug: conf->f_dbg, conf->p_dbg
1578 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001579 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001580 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001581static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001582 unsigned char *master,
Manuel Pégourié-Gonnard0d56aaa2019-05-03 09:58:33 +02001583 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001584{
1585 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001586
1587 /* cf. RFC 5246, Section 8.1:
1588 * "The master secret is always exactly 48 bytes in length." */
1589 size_t const master_secret_len = 48;
1590
1591#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1592 unsigned char session_hash[48];
1593#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1594
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001595 /* The label for the KDF used for key expansion.
1596 * This is either "master secret" or "extended master secret"
1597 * depending on whether the Extended Master Secret extension
1598 * is used. */
1599 char const *lbl = "master secret";
1600
1601 /* The salt for the KDF used for key expansion.
1602 * - If the Extended Master Secret extension is not used,
1603 * this is ClientHello.Random + ServerHello.Random
1604 * (see Sect. 8.1 in RFC 5246).
1605 * - If the Extended Master Secret extension is used,
1606 * this is the transcript of the handshake so far.
1607 * (see Sect. 4 in RFC 7627). */
1608 unsigned char const *salt = handshake->randbytes;
1609 size_t salt_len = 64;
1610
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001611#if !defined(MBEDTLS_DEBUG_C) && \
1612 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1613 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1614 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
1615 (void) ssl;
1616#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001617
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001618 if( handshake->resume != 0 )
1619 {
1620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001621 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001622 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001623
1624#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001625 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001626 {
1627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001628
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001629 lbl = "extended master secret";
1630 salt = session_hash;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001631 handshake->calc_verify( ssl, session_hash, &salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001632
1633 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
1634 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001635#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1636
1637#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001638 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1639 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001640 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001641 ssl_use_opaque_psk( ssl ) == 1 )
1642 {
1643 /* Perform PSK-to-MS expansion in a single step. */
1644 psa_status_t status;
1645 psa_algorithm_t alg;
1646 psa_key_handle_t psk;
1647 psa_key_derivation_operation_t derivation =
1648 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001649 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001650
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001652
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001653 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001654 if( handshake->psk_opaque != 0 )
1655 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001656
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001657 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001658 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001659 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001660 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1661
1662 status = psa_key_derivation( &derivation, psk, alg,
1663 salt, salt_len,
1664 (unsigned char const *) lbl,
1665 (size_t) strlen( lbl ),
1666 master_secret_len );
1667 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001668 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001669 psa_key_derivation_abort( &derivation );
1670 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001671 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001672
1673 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001674 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001675 master_secret_len );
1676 if( status != PSA_SUCCESS )
1677 {
1678 psa_key_derivation_abort( &derivation );
1679 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1680 }
1681
1682 status = psa_key_derivation_abort( &derivation );
1683 if( status != PSA_SUCCESS )
1684 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1685 }
1686 else
1687#endif
1688 {
1689 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1690 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001691 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001692 master_secret_len );
1693 if( ret != 0 )
1694 {
1695 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1696 return( ret );
1697 }
1698
1699 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1700 handshake->premaster,
1701 handshake->pmslen );
1702
1703 mbedtls_platform_zeroize( handshake->premaster,
1704 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001705 }
1706
1707 return( 0 );
1708}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001709
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001710int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1711{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001712 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001713 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1714 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001715
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1717
1718 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001719 ret = ssl_set_handshake_prfs( ssl->handshake,
1720 ssl->minor_ver,
1721 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001722 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001723 {
1724 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001725 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001726 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001727
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001728 /* Compute master secret if needed */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001729 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001730 ssl->session_negotiate->master,
1731 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001732 if( ret != 0 )
1733 {
1734 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1735 return( ret );
1736 }
1737
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001738 /* Swap the client and server random values:
1739 * - MS derivation wanted client+server (RFC 5246 8.1)
1740 * - key derivation wants server+client (RFC 5246 6.3) */
1741 {
1742 unsigned char tmp[64];
1743 memcpy( tmp, ssl->handshake->randbytes, 64 );
1744 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1745 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1746 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1747 }
1748
1749 /* Populate transform structure */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001750 ret = ssl_populate_transform( ssl->transform_negotiate,
1751 ssl->session_negotiate,
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001752 ssl->handshake->tls_prf,
1753 ssl->handshake->randbytes,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001754 ssl );
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001755 if( ret != 0 )
1756 {
1757 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1758 return( ret );
1759 }
1760
1761 /* We no longer need Server/ClientHello.random values */
1762 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1763 sizeof( ssl->handshake->randbytes ) );
1764
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001765 /* Allocate compression buffer */
1766#if defined(MBEDTLS_ZLIB_SUPPORT)
1767 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1768 ssl->compress_buf == NULL )
1769 {
1770 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1771 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1772 if( ssl->compress_buf == NULL )
1773 {
1774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
1775 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
1776 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1777 }
1778 }
1779#endif
1780
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1782
1783 return( 0 );
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001784}
1785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001787void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1788 unsigned char hash[36],
1789 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001790{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001791 mbedtls_md5_context md5;
1792 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 unsigned char pad_1[48];
1794 unsigned char pad_2[48];
1795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001798 mbedtls_md5_init( &md5 );
1799 mbedtls_sha1_init( &sha1 );
1800
1801 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1802 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001803
Paul Bakker380da532012-04-18 16:10:25 +00001804 memset( pad_1, 0x36, 48 );
1805 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001806
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001807 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1808 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1809 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001810
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001811 mbedtls_md5_starts_ret( &md5 );
1812 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1813 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1814 mbedtls_md5_update_ret( &md5, hash, 16 );
1815 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001816
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001817 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1818 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1819 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001820
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001821 mbedtls_sha1_starts_ret( &sha1 );
1822 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1823 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1824 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1825 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001826
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001827 *hlen = 36;
1828
1829 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001831
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001832 mbedtls_md5_free( &md5 );
1833 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001834
Paul Bakker380da532012-04-18 16:10:25 +00001835 return;
1836}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001840void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1841 unsigned char hash[36],
1842 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001843{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001844 mbedtls_md5_context md5;
1845 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001848
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001849 mbedtls_md5_init( &md5 );
1850 mbedtls_sha1_init( &sha1 );
1851
1852 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1853 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001854
Andrzej Kurekeb342242019-01-29 09:14:33 -05001855 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001856 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001857
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001858 *hlen = 36;
1859
1860 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001862
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001863 mbedtls_md5_free( &md5 );
1864 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001865
Paul Bakker380da532012-04-18 16:10:25 +00001866 return;
1867}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1871#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001872void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1873 unsigned char hash[32],
1874 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001875{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001876#if defined(MBEDTLS_USE_PSA_CRYPTO)
1877 size_t hash_size;
1878 psa_status_t status;
1879 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1880
1881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1882 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1883 if( status != PSA_SUCCESS )
1884 {
1885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1886 return;
1887 }
1888
1889 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1890 if( status != PSA_SUCCESS )
1891 {
1892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1893 return;
1894 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001895
1896 *hlen = 32;
1897 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1899#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001900 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001901
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001902 mbedtls_sha256_init( &sha256 );
1903
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001905
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001906 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001907 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001908
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001909 *hlen = 32;
1910
1911 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001913
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001914 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001915#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001916 return;
1917}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001921void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1922 unsigned char hash[48],
1923 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001924{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001925#if defined(MBEDTLS_USE_PSA_CRYPTO)
1926 size_t hash_size;
1927 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001928 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001929
1930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001931 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001932 if( status != PSA_SUCCESS )
1933 {
1934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1935 return;
1936 }
1937
Andrzej Kurek972fba52019-01-30 03:29:12 -05001938 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001939 if( status != PSA_SUCCESS )
1940 {
1941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1942 return;
1943 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001944
1945 *hlen = 48;
1946 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1948#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001949 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001950
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001951 mbedtls_sha512_init( &sha512 );
1952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001954
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001955 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001956 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001958 *hlen = 48;
1959
1960 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001962
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001963 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001964#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 return;
1966}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967#endif /* MBEDTLS_SHA512_C */
1968#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1971int 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 +02001972{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001973 unsigned char *p = ssl->handshake->premaster;
1974 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001975 const unsigned char *psk = ssl->conf->psk;
1976 size_t psk_len = ssl->conf->psk_len;
1977
1978 /* If the psk callback was called, use its result */
1979 if( ssl->handshake->psk != NULL )
1980 {
1981 psk = ssl->handshake->psk;
1982 psk_len = ssl->handshake->psk_len;
1983 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001984
1985 /*
1986 * PMS = struct {
1987 * opaque other_secret<0..2^16-1>;
1988 * opaque psk<0..2^16-1>;
1989 * };
1990 * with "other_secret" depending on the particular key exchange
1991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1993 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001994 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001995 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001997
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001998 *(p++) = (unsigned char)( psk_len >> 8 );
1999 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002000
2001 if( end < p || (size_t)( end - p ) < psk_len )
2002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2003
2004 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002005 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002006 }
2007 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2009#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2010 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002011 {
2012 /*
2013 * other_secret already set by the ClientKeyExchange message,
2014 * and is 48 bytes long
2015 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002016 if( end - p < 2 )
2017 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2018
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002019 *p++ = 0;
2020 *p++ = 48;
2021 p += 48;
2022 }
2023 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2025#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2026 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002027 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002028 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002029 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002030
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002031 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002033 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002034 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002037 return( ret );
2038 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002039 *(p++) = (unsigned char)( len >> 8 );
2040 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002041 p += len;
2042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002044 }
2045 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2047#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2048 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002049 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002050 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002051 size_t zlen;
2052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002054 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002055 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002058 return( ret );
2059 }
2060
2061 *(p++) = (unsigned char)( zlen >> 8 );
2062 *(p++) = (unsigned char)( zlen );
2063 p += zlen;
2064
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002065 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2066 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002067 }
2068 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002073 }
2074
2075 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002076 if( end - p < 2 )
2077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002078
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002079 *(p++) = (unsigned char)( psk_len >> 8 );
2080 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002081
2082 if( end < p || (size_t)( end - p ) < psk_len )
2083 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2084
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002085 memcpy( p, psk, psk_len );
2086 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002087
2088 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2089
2090 return( 0 );
2091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002095/*
2096 * SSLv3.0 MAC functions
2097 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002098#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002099static void ssl_mac( mbedtls_md_context_t *md_ctx,
2100 const unsigned char *secret,
2101 const unsigned char *buf, size_t len,
2102 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002103 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002104{
2105 unsigned char header[11];
2106 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002107 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2109 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002110
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002111 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002113 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002114 else
Paul Bakker68884e32013-01-07 18:20:04 +01002115 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002116
2117 memcpy( header, ctr, 8 );
2118 header[ 8] = (unsigned char) type;
2119 header[ 9] = (unsigned char)( len >> 8 );
2120 header[10] = (unsigned char)( len );
2121
Paul Bakker68884e32013-01-07 18:20:04 +01002122 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 mbedtls_md_starts( md_ctx );
2124 mbedtls_md_update( md_ctx, secret, md_size );
2125 mbedtls_md_update( md_ctx, padding, padlen );
2126 mbedtls_md_update( md_ctx, header, 11 );
2127 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002128 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002129
Paul Bakker68884e32013-01-07 18:20:04 +01002130 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 mbedtls_md_starts( md_ctx );
2132 mbedtls_md_update( md_ctx, secret, md_size );
2133 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002134 mbedtls_md_update( md_ctx, out, md_size );
2135 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002138
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002139/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002140 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002141#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002142 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2143 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2144 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2145/* This function makes sure every byte in the memory region is accessed
2146 * (in ascending addresses order) */
2147static void ssl_read_memory( unsigned char *p, size_t len )
2148{
2149 unsigned char acc = 0;
2150 volatile unsigned char force;
2151
2152 for( ; len != 0; p++, len-- )
2153 acc ^= *p;
2154
2155 force = acc;
2156 (void) force;
2157}
2158#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2159
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002160/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002162 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002163
Hanno Beckera0e20d02019-05-15 14:03:01 +01002164#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002165/* This functions transforms a DTLS plaintext fragment and a record content
2166 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002167 *
2168 * struct {
2169 * opaque content[DTLSPlaintext.length];
2170 * ContentType real_type;
2171 * uint8 zeros[length_of_padding];
2172 * } DTLSInnerPlaintext;
2173 *
2174 * Input:
2175 * - `content`: The beginning of the buffer holding the
2176 * plaintext to be wrapped.
2177 * - `*content_size`: The length of the plaintext in Bytes.
2178 * - `max_len`: The number of Bytes available starting from
2179 * `content`. This must be `>= *content_size`.
2180 * - `rec_type`: The desired record content type.
2181 *
2182 * Output:
2183 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2184 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2185 *
2186 * Returns:
2187 * - `0` on success.
2188 * - A negative error code if `max_len` didn't offer enough space
2189 * for the expansion.
2190 */
2191static int ssl_cid_build_inner_plaintext( unsigned char *content,
2192 size_t *content_size,
2193 size_t remaining,
2194 uint8_t rec_type )
2195{
2196 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002197 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2198 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2199 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002200
2201 /* Write real content type */
2202 if( remaining == 0 )
2203 return( -1 );
2204 content[ len ] = rec_type;
2205 len++;
2206 remaining--;
2207
2208 if( remaining < pad )
2209 return( -1 );
2210 memset( content + len, 0, pad );
2211 len += pad;
2212 remaining -= pad;
2213
2214 *content_size = len;
2215 return( 0 );
2216}
2217
Hanno Becker07dc97d2019-05-20 15:08:01 +01002218/* This function parses a DTLSInnerPlaintext structure.
2219 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002220static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2221 size_t *content_size,
2222 uint8_t *rec_type )
2223{
2224 size_t remaining = *content_size;
2225
2226 /* Determine length of padding by skipping zeroes from the back. */
2227 do
2228 {
2229 if( remaining == 0 )
2230 return( -1 );
2231 remaining--;
2232 } while( content[ remaining ] == 0 );
2233
2234 *content_size = remaining;
2235 *rec_type = content[ remaining ];
2236
2237 return( 0 );
2238}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002239#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002240
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002241/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002242 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002243static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002244 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002245 mbedtls_record *rec )
2246{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002247 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002248 *
2249 * additional_data = seq_num + TLSCompressed.type +
2250 * TLSCompressed.version + TLSCompressed.length;
2251 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002252 * For the CID extension, this is extended as follows
2253 * (quoting draft-ietf-tls-dtls-connection-id-05,
2254 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002255 *
2256 * additional_data = seq_num + DTLSPlaintext.type +
2257 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002258 * cid +
2259 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002260 * length_of_DTLSInnerPlaintext;
2261 */
2262
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002263 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2264 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002265 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002266
Hanno Beckera0e20d02019-05-15 14:03:01 +01002267#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002268 if( rec->cid_len != 0 )
2269 {
2270 memcpy( add_data + 11, rec->cid, rec->cid_len );
2271 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2272 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2273 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2274 *add_data_len = 13 + 1 + rec->cid_len;
2275 }
2276 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002277#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002278 {
2279 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2280 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2281 *add_data_len = 13;
2282 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002283}
2284
Hanno Beckera18d1322018-01-03 14:27:32 +00002285int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2286 mbedtls_ssl_transform *transform,
2287 mbedtls_record *rec,
2288 int (*f_rng)(void *, unsigned char *, size_t),
2289 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002292 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002293 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002294 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002295 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002296 size_t post_avail;
2297
2298 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002299#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002300 ((void) ssl);
2301#endif
2302
2303 /* The PRNG is used for dynamic IV generation that's used
2304 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2305#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2306 ( defined(MBEDTLS_AES_C) || \
2307 defined(MBEDTLS_ARIA_C) || \
2308 defined(MBEDTLS_CAMELLIA_C) ) && \
2309 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2310 ((void) f_rng);
2311 ((void) p_rng);
2312#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002315
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002316 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002317 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2319 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2320 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002321 if( rec == NULL
2322 || rec->buf == NULL
2323 || rec->buf_len < rec->data_offset
2324 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002325#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002326 || rec->cid_len != 0
2327#endif
2328 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002329 {
2330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002332 }
2333
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002334 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002335 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002337 data, rec->data_len );
2338
2339 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2340
2341 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2342 {
2343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2344 (unsigned) rec->data_len,
2345 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2346 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2347 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002348
Hanno Beckera0e20d02019-05-15 14:03:01 +01002349#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002350 /*
2351 * Add CID information
2352 */
2353 rec->cid_len = transform->out_cid_len;
2354 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2355 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002356
2357 if( rec->cid_len != 0 )
2358 {
2359 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002360 * Wrap plaintext into DTLSInnerPlaintext structure.
2361 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002362 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002363 * Note that this changes `rec->data_len`, and hence
2364 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002365 */
2366 if( ssl_cid_build_inner_plaintext( data,
2367 &rec->data_len,
2368 post_avail,
2369 rec->type ) != 0 )
2370 {
2371 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2372 }
2373
2374 rec->type = MBEDTLS_SSL_MSG_CID;
2375 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002376#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002377
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002378 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2379
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002381 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 */
Hanno Becker52344c22018-01-03 15:24:20 +00002383#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 if( mode == MBEDTLS_MODE_STREAM ||
2385 ( mode == MBEDTLS_MODE_CBC
2386#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002387 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002388#endif
2389 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002391 if( post_avail < transform->maclen )
2392 {
2393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2394 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2395 }
2396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002398 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002399 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002400 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002401 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2402 data, rec->data_len, rec->ctr, rec->type, mac );
2403 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002404 }
2405 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2408 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002409 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002410 {
Hanno Becker992b6872017-11-09 18:57:39 +00002411 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2412
Hanno Beckercab87e62019-04-29 13:52:53 +01002413 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002414
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002415 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002416 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002417 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2418 data, rec->data_len );
2419 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2420 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2421
2422 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002423 }
2424 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002425#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2428 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002429 }
2430
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002431 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2432 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002433
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002434 rec->data_len += transform->maclen;
2435 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002436 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002437 }
Hanno Becker52344c22018-01-03 15:24:20 +00002438#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002439
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002440 /*
2441 * Encrypt
2442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2444 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002446 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002447 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002449 "including %d bytes of padding",
2450 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002452 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2453 transform->iv_enc, transform->ivlen,
2454 data, rec->data_len,
2455 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002458 return( ret );
2459 }
2460
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002461 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2464 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002465 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 }
Paul Bakker68884e32013-01-07 18:20:04 +01002467 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002469
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002470#if defined(MBEDTLS_GCM_C) || \
2471 defined(MBEDTLS_CCM_C) || \
2472 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002474 mode == MBEDTLS_MODE_CCM ||
2475 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002476 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002477 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002478 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002479 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002480
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002481 /* Check that there's space for both the authentication tag
2482 * and the explicit IV before and after the record content. */
2483 if( post_avail < transform->taglen ||
2484 rec->data_offset < explicit_iv_len )
2485 {
2486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2487 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2488 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002489
Paul Bakker68884e32013-01-07 18:20:04 +01002490 /*
2491 * Generate IV
2492 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002493 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2494 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002495 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002496 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002497 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2498 explicit_iv_len );
2499 /* Prefix record content with explicit IV. */
2500 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002501 }
2502 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2503 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002504 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002505 unsigned char i;
2506
2507 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2508
2509 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002510 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002511 }
2512 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002513 {
2514 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002517 }
2518
Hanno Beckercab87e62019-04-29 13:52:53 +01002519 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002520
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002521 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2522 iv, transform->ivlen );
2523 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002524 data - explicit_iv_len, explicit_iv_len );
2525 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002526 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002528 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002529 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002530
Paul Bakker68884e32013-01-07 18:20:04 +01002531 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002532 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002533 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002534
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002535 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002536 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002537 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002538 data, rec->data_len, /* source */
2539 data, &rec->data_len, /* destination */
2540 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002543 return( ret );
2544 }
2545
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002546 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2547 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002548
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002549 rec->data_len += transform->taglen + explicit_iv_len;
2550 rec->data_offset -= explicit_iv_len;
2551 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002552 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002553 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2556#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002557 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002560 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002561 size_t padlen, i;
2562 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002563
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002564 /* Currently we're always using minimal padding
2565 * (up to 255 bytes would be allowed). */
2566 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2567 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 padlen = 0;
2569
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002570 /* Check there's enough space in the buffer for the padding. */
2571 if( post_avail < padlen + 1 )
2572 {
2573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2574 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2575 }
2576
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002578 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002580 rec->data_len += padlen + 1;
2581 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002584 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002585 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2586 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002587 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002588 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002589 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002590 if( f_rng == NULL )
2591 {
2592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2593 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2594 }
2595
2596 if( rec->data_offset < transform->ivlen )
2597 {
2598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2599 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2600 }
2601
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002602 /*
2603 * Generate IV
2604 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002605 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002606 if( ret != 0 )
2607 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002608
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002609 memcpy( data - transform->ivlen, transform->iv_enc,
2610 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002611
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002612 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002616 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002617 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002618 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002620 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2621 transform->iv_enc,
2622 transform->ivlen,
2623 data, rec->data_len,
2624 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002627 return( ret );
2628 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002629
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002630 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002634 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002637 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002638 {
2639 /*
2640 * Save IV in SSL3 and TLS1
2641 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002642 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2643 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002645 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002646#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002647 {
2648 data -= transform->ivlen;
2649 rec->data_offset -= transform->ivlen;
2650 rec->data_len += transform->ivlen;
2651 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002654 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002655 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002656 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2657
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002658 /*
2659 * MAC(MAC_write_key, seq_num +
2660 * TLSCipherText.type +
2661 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002662 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002663 * IV + // except for TLS 1.0
2664 * ENC(content + padding + padding_length));
2665 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002666
2667 if( post_avail < transform->maclen)
2668 {
2669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2670 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2671 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002672
Hanno Beckercab87e62019-04-29 13:52:53 +01002673 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002676 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002677 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002678
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002679 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002680 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002681 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2682 data, rec->data_len );
2683 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2684 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002685
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002686 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002687
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002688 rec->data_len += transform->maclen;
2689 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002690 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002691 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002694 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002696 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2699 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002700 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002702 /* Make extra sure authentication was performed, exactly once */
2703 if( auth_done != 1 )
2704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2706 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002707 }
2708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
2711 return( 0 );
2712}
2713
Hanno Becker605949f2019-07-12 08:23:59 +01002714int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002715 mbedtls_ssl_transform *transform,
2716 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002717{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002718 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002720 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002721#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002722 size_t padlen = 0, correct = 1;
2723#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002724 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002725 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002726 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002727
Hanno Beckera18d1322018-01-03 14:27:32 +00002728#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002729 ((void) ssl);
2730#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002733 if( rec == NULL ||
2734 rec->buf == NULL ||
2735 rec->buf_len < rec->data_offset ||
2736 rec->buf_len - rec->data_offset < rec->data_len )
2737 {
2738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002740 }
2741
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002742 data = rec->buf + rec->data_offset;
2743 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Hanno Beckera0e20d02019-05-15 14:03:01 +01002745#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002746 /*
2747 * Match record's CID with incoming CID.
2748 */
Hanno Becker938489a2019-05-08 13:02:22 +01002749 if( rec->cid_len != transform->in_cid_len ||
2750 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2751 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002752 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002753 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002754#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2757 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002758 {
2759 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002760 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2761 transform->iv_dec,
2762 transform->ivlen,
2763 data, rec->data_len,
2764 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002767 return( ret );
2768 }
2769
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002770 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002774 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002775 }
Paul Bakker68884e32013-01-07 18:20:04 +01002776 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002778#if defined(MBEDTLS_GCM_C) || \
2779 defined(MBEDTLS_CCM_C) || \
2780 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002782 mode == MBEDTLS_MODE_CCM ||
2783 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002784 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002785 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002786 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002787
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002788 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002789 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002790 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002791
2792 /* Check that there's enough space for the explicit IV
2793 * (at the beginning of the record) and the MAC (at the
2794 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002795 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002798 "+ taglen (%d)", rec->data_len,
2799 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002801 }
Paul Bakker68884e32013-01-07 18:20:04 +01002802
Hanno Beckerd96a6522019-07-10 13:55:25 +01002803#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002804 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2805 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002806 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002807
Hanno Beckerd96a6522019-07-10 13:55:25 +01002808 /* Fixed */
2809 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2810 /* Explicit */
2811 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002812 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002813 else
2814#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2815#if defined(MBEDTLS_CHACHAPOLY_C)
2816 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002817 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002818 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002819 unsigned char i;
2820
2821 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2822
2823 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002824 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002825 }
2826 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002827#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002828 {
2829 /* Reminder if we ever add an AEAD mode with a different size */
2830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2831 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2832 }
2833
Hanno Beckerd96a6522019-07-10 13:55:25 +01002834 /* Group changes to data, data_len, and add_data, because
2835 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002836 data += explicit_iv_len;
2837 rec->data_offset += explicit_iv_len;
2838 rec->data_len -= explicit_iv_len + transform->taglen;
2839
Hanno Beckercab87e62019-04-29 13:52:53 +01002840 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002841 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002842 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002843
Hanno Beckerd96a6522019-07-10 13:55:25 +01002844 /* Because of the check above, we know that there are
2845 * explicit_iv_len Bytes preceeding data, and taglen
2846 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002847 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002848 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002849
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002850 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002851 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002852 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002853
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002854 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002855 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002856 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002857 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2858 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002859 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002860 data, rec->data_len,
2861 data, &olen,
2862 data + rec->data_len,
2863 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2868 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002869
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002870 return( ret );
2871 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002872 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002873
Hanno Beckerd96a6522019-07-10 13:55:25 +01002874 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002875 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002879 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002881 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2883#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002884 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002886 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002887 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002888
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 /*
Paul Bakker45829992013-01-03 14:52:21 +01002890 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002893 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2894 {
2895 /* The ciphertext is prefixed with the CBC IV. */
2896 minlen += transform->ivlen;
2897 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002898#endif
Paul Bakker45829992013-01-03 14:52:21 +01002899
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002900 /* Size considerations:
2901 *
2902 * - The CBC cipher text must not be empty and hence
2903 * at least of size transform->ivlen.
2904 *
2905 * Together with the potential IV-prefix, this explains
2906 * the first of the two checks below.
2907 *
2908 * - The record must contain a MAC, either in plain or
2909 * encrypted, depending on whether Encrypt-then-MAC
2910 * is used or not.
2911 * - If it is, the message contains the IV-prefix,
2912 * the CBC ciphertext, and the MAC.
2913 * - If it is not, the padded plaintext, and hence
2914 * the CBC ciphertext, has at least length maclen + 1
2915 * because there is at least the padding length byte.
2916 *
2917 * As the CBC ciphertext is not empty, both cases give the
2918 * lower bound minlen + maclen + 1 on the record size, which
2919 * we test for in the second check below.
2920 */
2921 if( rec->data_len < minlen + transform->ivlen ||
2922 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002925 "+ 1 ) ( + expl IV )", rec->data_len,
2926 transform->ivlen,
2927 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002929 }
2930
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002931 /*
2932 * Authenticate before decrypt if enabled
2933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002935 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002936 {
Hanno Becker992b6872017-11-09 18:57:39 +00002937 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002940
Hanno Beckerd96a6522019-07-10 13:55:25 +01002941 /* Update data_len in tandem with add_data.
2942 *
2943 * The subtraction is safe because of the previous check
2944 * data_len >= minlen + maclen + 1.
2945 *
2946 * Afterwards, we know that data + data_len is followed by at
2947 * least maclen Bytes, which justifies the call to
2948 * mbedtls_ssl_safer_memcmp() below.
2949 *
2950 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002951 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002952 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002953
Hanno Beckerd96a6522019-07-10 13:55:25 +01002954 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002955 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2956 add_data_len );
2957 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2958 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002959 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2960 data, rec->data_len );
2961 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2962 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002963
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002964 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2965 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002966 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002967 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002968
Hanno Beckerd96a6522019-07-10 13:55:25 +01002969 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002970 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2971 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002975 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002976 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002977 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002979
2980 /*
2981 * Check length sanity
2982 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002983
2984 /* We know from above that data_len > minlen >= 0,
2985 * so the following check in particular implies that
2986 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002987 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002990 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002992 }
2993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002995 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002996 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002997 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002998 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002999 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01003000 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003001 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003002
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003003 data += transform->ivlen;
3004 rec->data_offset += transform->ivlen;
3005 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003006 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003008
Hanno Beckerd96a6522019-07-10 13:55:25 +01003009 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3010
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003011 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3012 transform->iv_dec, transform->ivlen,
3013 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003016 return( ret );
3017 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003018
Hanno Beckerd96a6522019-07-10 13:55:25 +01003019 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003020 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003024 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003027 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003028 {
3029 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01003030 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3031 * records is equivalent to CBC decryption of the concatenation
3032 * of the records; in other words, IVs are maintained across
3033 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003034 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003035 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3036 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003038#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003040 /* Safe since data_len >= minlen + maclen + 1, so after having
3041 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003042 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3043 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003044 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003045
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003046 if( auth_done == 1 )
3047 {
3048 correct *= ( rec->data_len >= padlen + 1 );
3049 padlen *= ( rec->data_len >= padlen + 1 );
3050 }
3051 else
Paul Bakker45829992013-01-03 14:52:21 +01003052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003054 if( rec->data_len < transform->maclen + padlen + 1 )
3055 {
3056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3057 rec->data_len,
3058 transform->maclen,
3059 padlen + 1 ) );
3060 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003061#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003062
3063 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3064 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003065 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003067 padlen++;
3068
3069 /* Regardless of the validity of the padding,
3070 * we have data_len >= padlen here. */
3071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003073 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003074 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003075 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077#if defined(MBEDTLS_SSL_DEBUG_ALL)
3078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003079 "should be no more than %d",
3080 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003081#endif
Paul Bakker45829992013-01-03 14:52:21 +01003082 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003083 }
3084 }
3085 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3087#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3088 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003089 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003091 /* The padding check involves a series of up to 256
3092 * consecutive memory reads at the end of the record
3093 * plaintext buffer. In order to hide the length and
3094 * validity of the padding, always perform exactly
3095 * `min(256,plaintext_len)` reads (but take into account
3096 * only the last `padlen` bytes for the padding check). */
3097 size_t pad_count = 0;
3098 size_t real_count = 0;
3099 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003100
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003101 /* Index of first padding byte; it has been ensured above
3102 * that the subtraction is safe. */
3103 size_t const padding_idx = rec->data_len - padlen;
3104 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3105 size_t const start_idx = rec->data_len - num_checks;
3106 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003107
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003108 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003109 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003110 real_count |= ( idx >= padding_idx );
3111 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003112 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003113 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003116 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003118#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003119 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003120 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003121 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3123 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3126 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003127 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003128
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003129 /* If the padding was found to be invalid, padlen == 0
3130 * and the subtraction is safe. If the padding was found valid,
3131 * padlen hasn't been changed and the previous assertion
3132 * data_len >= padlen still holds. */
3133 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003134 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003135 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003137 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003141 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003142
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003143#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003145 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003146#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003147
3148 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003149 * Authenticate if not done yet.
3150 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003151 */
Hanno Becker52344c22018-01-03 15:24:20 +00003152#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003153 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003154 {
Hanno Becker992b6872017-11-09 18:57:39 +00003155 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003156
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003157 /* If the initial value of padlen was such that
3158 * data_len < maclen + padlen + 1, then padlen
3159 * got reset to 1, and the initial check
3160 * data_len >= minlen + maclen + 1
3161 * guarantees that at this point we still
3162 * have at least data_len >= maclen.
3163 *
3164 * If the initial value of padlen was such that
3165 * data_len >= maclen + padlen + 1, then we have
3166 * subtracted either padlen + 1 (if the padding was correct)
3167 * or 0 (if the padding was incorrect) since then,
3168 * hence data_len >= maclen in any case.
3169 */
3170 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003171 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003174 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003175 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003176 ssl_mac( &transform->md_ctx_dec,
3177 transform->mac_dec,
3178 data, rec->data_len,
3179 rec->ctr, rec->type,
3180 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003181 }
3182 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3184#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3185 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003186 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003187 {
3188 /*
3189 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003190 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003191 *
3192 * Known timing attacks:
3193 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3194 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003195 * To compensate for different timings for the MAC calculation
3196 * depending on how much padding was removed (which is determined
3197 * by padlen), process extra_run more blocks through the hash
3198 * function.
3199 *
3200 * The formula in the paper is
3201 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3202 * where L1 is the size of the header plus the decrypted message
3203 * plus CBC padding and L2 is the size of the header plus the
3204 * decrypted message. This is for an underlying hash function
3205 * with 64-byte blocks.
3206 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3207 * correctly. We round down instead of up, so -56 is the correct
3208 * value for our calculations instead of -55.
3209 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003210 * Repeat the formula rather than defining a block_size variable.
3211 * This avoids requiring division by a variable at runtime
3212 * (which would be marginally less efficient and would require
3213 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003214 */
3215 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003216 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003217
3218 /*
3219 * The next two sizes are the minimum and maximum values of
3220 * in_msglen over all padlen values.
3221 *
3222 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003223 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003224 *
3225 * Note that max_len + maclen is never more than the buffer
3226 * length, as we previously did in_msglen -= maclen too.
3227 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003228 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003229 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3230
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003231 memset( tmp, 0, sizeof( tmp ) );
3232
3233 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003234 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003235#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3236 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003237 case MBEDTLS_MD_MD5:
3238 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003239 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003240 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003241 extra_run =
3242 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3243 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003244 break;
3245#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003246#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003247 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003248 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003249 extra_run =
3250 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3251 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003252 break;
3253#endif
3254 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003256 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3257 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003258
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003259 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003260
Hanno Beckercab87e62019-04-29 13:52:53 +01003261 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3262 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003263 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3264 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003265 /* Make sure we access everything even when padlen > 0. This
3266 * makes the synchronisation requirements for just-in-time
3267 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003268 ssl_read_memory( data + rec->data_len, padlen );
3269 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003270
3271 /* Call mbedtls_md_process at least once due to cache attacks
3272 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003273 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003274 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003275
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003276 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003277
3278 /* Make sure we access all the memory that could contain the MAC,
3279 * before we check it in the next code block. This makes the
3280 * synchronisation requirements for just-in-time Prime+Probe
3281 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003282 ssl_read_memory( data + min_len,
3283 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003284 }
3285 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3287 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003293#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003294 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3295 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003297
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003298 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3299 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301#if defined(MBEDTLS_SSL_DEBUG_ALL)
3302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003303#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003304 correct = 0;
3305 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003306 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003307 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003308
3309 /*
3310 * Finally check the correct flag
3311 */
3312 if( correct == 0 )
3313 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003314#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003315
3316 /* Make extra sure authentication was performed, exactly once */
3317 if( auth_done != 1 )
3318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003321 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003322
Hanno Beckera0e20d02019-05-15 14:03:01 +01003323#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003324 if( rec->cid_len != 0 )
3325 {
3326 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3327 &rec->type );
3328 if( ret != 0 )
3329 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3330 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003331#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
3335 return( 0 );
3336}
3337
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003338#undef MAC_NONE
3339#undef MAC_PLAINTEXT
3340#undef MAC_CIPHERTEXT
3341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003343/*
3344 * Compression/decompression functions
3345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003347{
3348 int ret;
3349 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003350 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003351 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003352 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003355
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003356 if( len_pre == 0 )
3357 return( 0 );
3358
Paul Bakker2770fbd2012-07-03 13:30:23 +00003359 memcpy( msg_pre, ssl->out_msg, len_pre );
3360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003362 ssl->out_msglen ) );
3363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003365 ssl->out_msg, ssl->out_msglen );
3366
Paul Bakker48916f92012-09-16 19:57:18 +00003367 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3368 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3369 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003370 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003371
Paul Bakker48916f92012-09-16 19:57:18 +00003372 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003373 if( ret != Z_OK )
3374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3376 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003377 }
3378
Angus Grattond8213d02016-05-25 20:56:48 +10003379 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003380 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003383 ssl->out_msglen ) );
3384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003386 ssl->out_msg, ssl->out_msglen );
3387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003389
3390 return( 0 );
3391}
3392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003394{
3395 int ret;
3396 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003397 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003398 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003399 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003402
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003403 if( len_pre == 0 )
3404 return( 0 );
3405
Paul Bakker2770fbd2012-07-03 13:30:23 +00003406 memcpy( msg_pre, ssl->in_msg, len_pre );
3407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003409 ssl->in_msglen ) );
3410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003412 ssl->in_msg, ssl->in_msglen );
3413
Paul Bakker48916f92012-09-16 19:57:18 +00003414 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3415 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3416 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003417 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003418 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003419
Paul Bakker48916f92012-09-16 19:57:18 +00003420 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003421 if( ret != Z_OK )
3422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3424 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003425 }
3426
Angus Grattond8213d02016-05-25 20:56:48 +10003427 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003428 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003431 ssl->in_msglen ) );
3432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003434 ssl->in_msg, ssl->in_msglen );
3435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003437
3438 return( 0 );
3439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3443static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445#if defined(MBEDTLS_SSL_PROTO_DTLS)
3446static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003447{
3448 /* If renegotiation is not enforced, retransmit until we would reach max
3449 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003450 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003451 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003452 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003453 unsigned char doublings = 1;
3454
3455 while( ratio != 0 )
3456 {
3457 ++doublings;
3458 ratio >>= 1;
3459 }
3460
3461 if( ++ssl->renego_records_seen > doublings )
3462 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003464 return( 0 );
3465 }
3466 }
3467
3468 return( ssl_write_hello_request( ssl ) );
3469}
3470#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003472
Paul Bakker5121ce52009-01-03 21:22:43 +00003473/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003474 * Fill the input message buffer by appending data to it.
3475 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003476 *
3477 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3478 * available (from this read and/or a previous one). Otherwise, an error code
3479 * is returned (possibly EOF or WANT_READ).
3480 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003481 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3482 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3483 * since we always read a whole datagram at once.
3484 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003485 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003486 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003489{
Paul Bakker23986e52011-04-24 08:57:21 +00003490 int ret;
3491 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003494
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003495 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003498 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003500 }
3501
Angus Grattond8213d02016-05-25 20:56:48 +10003502 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3505 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003506 }
3507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003509 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003510 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003511 uint32_t timeout;
3512
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003513 /* Just to be sure */
3514 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3515 {
3516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3517 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3519 }
3520
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003521 /*
3522 * The point is, we need to always read a full datagram at once, so we
3523 * sometimes read more then requested, and handle the additional data.
3524 * It could be the rest of the current record (while fetching the
3525 * header) and/or some other records in the same datagram.
3526 */
3527
3528 /*
3529 * Move to the next record in the already read datagram if applicable
3530 */
3531 if( ssl->next_record_offset != 0 )
3532 {
3533 if( ssl->in_left < ssl->next_record_offset )
3534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3536 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003537 }
3538
3539 ssl->in_left -= ssl->next_record_offset;
3540
3541 if( ssl->in_left != 0 )
3542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003544 ssl->next_record_offset ) );
3545 memmove( ssl->in_hdr,
3546 ssl->in_hdr + ssl->next_record_offset,
3547 ssl->in_left );
3548 }
3549
3550 ssl->next_record_offset = 0;
3551 }
3552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003554 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003555
3556 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003557 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003558 */
3559 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003562 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003563 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003564
3565 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003566 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003567 * are not at the beginning of a new record, the caller did something
3568 * wrong.
3569 */
3570 if( ssl->in_left != 0 )
3571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3573 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003574 }
3575
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003576 /*
3577 * Don't even try to read if time's out already.
3578 * This avoids by-passing the timer when repeatedly receiving messages
3579 * that will end up being dropped.
3580 */
3581 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003582 {
3583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003584 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003585 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003586 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003587 {
Angus Grattond8213d02016-05-25 20:56:48 +10003588 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003591 timeout = ssl->handshake->retransmit_timeout;
3592 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003593 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003596
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003597 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003598 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3599 timeout );
3600 else
3601 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003604
3605 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003607 }
3608
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003609 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003612 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003615 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003616 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003619 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003620 }
3621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003625 return( ret );
3626 }
3627
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003628 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003629 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003631 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003633 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003634 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003637 return( ret );
3638 }
3639
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003640 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003641 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003643 }
3644
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 if( ret < 0 )
3646 return( ret );
3647
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003648 ssl->in_left = ret;
3649 }
3650 else
3651#endif
3652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003654 ssl->in_left, nb_want ) );
3655
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003656 while( ssl->in_left < nb_want )
3657 {
3658 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003659
3660 if( ssl_check_timer( ssl ) != 0 )
3661 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3662 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003663 {
3664 if( ssl->f_recv_timeout != NULL )
3665 {
3666 ret = ssl->f_recv_timeout( ssl->p_bio,
3667 ssl->in_hdr + ssl->in_left, len,
3668 ssl->conf->read_timeout );
3669 }
3670 else
3671 {
3672 ret = ssl->f_recv( ssl->p_bio,
3673 ssl->in_hdr + ssl->in_left, len );
3674 }
3675 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003678 ssl->in_left, nb_want ) );
3679 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003680
3681 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003683
3684 if( ret < 0 )
3685 return( ret );
3686
mohammad160352aecb92018-03-28 23:41:40 -07003687 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003688 {
Darryl Green11999bb2018-03-13 15:22:58 +00003689 MBEDTLS_SSL_DEBUG_MSG( 1,
3690 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003691 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003692 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3693 }
3694
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003695 ssl->in_left += ret;
3696 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003697 }
3698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003700
3701 return( 0 );
3702}
3703
3704/*
3705 * Flush any data not yet written
3706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003708{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003709 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003710 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003713
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003714 if( ssl->f_send == NULL )
3715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003717 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003719 }
3720
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003721 /* Avoid incrementing counter if data is flushed */
3722 if( ssl->out_left == 0 )
3723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003725 return( 0 );
3726 }
3727
Paul Bakker5121ce52009-01-03 21:22:43 +00003728 while( ssl->out_left > 0 )
3729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003731 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
Hanno Becker2b1e3542018-08-06 11:19:13 +01003733 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003734 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003736 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003737
3738 if( ret <= 0 )
3739 return( ret );
3740
mohammad160352aecb92018-03-28 23:41:40 -07003741 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003742 {
Darryl Green11999bb2018-03-13 15:22:58 +00003743 MBEDTLS_SSL_DEBUG_MSG( 1,
3744 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003745 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003746 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3747 }
3748
Paul Bakker5121ce52009-01-03 21:22:43 +00003749 ssl->out_left -= ret;
3750 }
3751
Hanno Becker2b1e3542018-08-06 11:19:13 +01003752#if defined(MBEDTLS_SSL_PROTO_DTLS)
3753 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003754 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003755 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003756 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003757 else
3758#endif
3759 {
3760 ssl->out_hdr = ssl->out_buf + 8;
3761 }
3762 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003765
3766 return( 0 );
3767}
3768
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003769/*
3770 * Functions to handle the DTLS retransmission state machine
3771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003773/*
3774 * Append current handshake message to current outgoing flight
3775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003777{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003778 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3780 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3781 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003782
3783 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003784 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003785 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003788 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003789 }
3790
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003791 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003792 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003795 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003796 }
3797
3798 /* Copy current handshake message with headers */
3799 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3800 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003801 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003802 msg->next = NULL;
3803
3804 /* Append to the current flight */
3805 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003806 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003807 else
3808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003809 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003810 while( cur->next != NULL )
3811 cur = cur->next;
3812 cur->next = msg;
3813 }
3814
Hanno Becker3b235902018-08-06 09:54:53 +01003815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003816 return( 0 );
3817}
3818
3819/*
3820 * Free the current flight of handshake messages
3821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003823{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 mbedtls_ssl_flight_item *cur = flight;
3825 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003826
3827 while( cur != NULL )
3828 {
3829 next = cur->next;
3830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 mbedtls_free( cur->p );
3832 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003833
3834 cur = next;
3835 }
3836}
3837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3839static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003840#endif
3841
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003842/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003843 * Swap transform_out and out_ctr with the alternative ones
3844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003846{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003848 unsigned char tmp_out_ctr[8];
3849
3850 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003853 return;
3854 }
3855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003857
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003858 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003859 tmp_transform = ssl->transform_out;
3860 ssl->transform_out = ssl->handshake->alt_transform_out;
3861 ssl->handshake->alt_transform_out = tmp_transform;
3862
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003863 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003864 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3865 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003866 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003867
3868 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003869 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3872 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3877 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003878 }
3879 }
3880#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003881}
3882
3883/*
3884 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003885 */
3886int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3887{
3888 int ret = 0;
3889
3890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3891
3892 ret = mbedtls_ssl_flight_transmit( ssl );
3893
3894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3895
3896 return( ret );
3897}
3898
3899/*
3900 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003901 *
3902 * Need to remember the current message in case flush_output returns
3903 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003904 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003905 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003906int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003907{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003908 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003912 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003914
3915 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003916 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003917 ssl_swap_epochs( ssl );
3918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003920 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003921
3922 while( ssl->handshake->cur_msg != NULL )
3923 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003924 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003925 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003926
Hanno Beckere1dcb032018-08-17 16:47:58 +01003927 int const is_finished =
3928 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3929 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3930
Hanno Becker04da1892018-08-14 13:22:10 +01003931 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3932 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3933
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003934 /* Swap epochs before sending Finished: we can't do it after
3935 * sending ChangeCipherSpec, in case write returns WANT_READ.
3936 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003937 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003938 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003940 ssl_swap_epochs( ssl );
3941 }
3942
Hanno Becker67bc7c32018-08-06 11:33:50 +01003943 ret = ssl_get_remaining_payload_in_datagram( ssl );
3944 if( ret < 0 )
3945 return( ret );
3946 max_frag_len = (size_t) ret;
3947
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003948 /* CCS is copied as is, while HS messages may need fragmentation */
3949 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3950 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003951 if( max_frag_len == 0 )
3952 {
3953 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3954 return( ret );
3955
3956 continue;
3957 }
3958
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003959 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003960 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003961 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003962
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003963 /* Update position inside current message */
3964 ssl->handshake->cur_msg_p += cur->len;
3965 }
3966 else
3967 {
3968 const unsigned char * const p = ssl->handshake->cur_msg_p;
3969 const size_t hs_len = cur->len - 12;
3970 const size_t frag_off = p - ( cur->p + 12 );
3971 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003972 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003973
Hanno Beckere1dcb032018-08-17 16:47:58 +01003974 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003975 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003976 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003977 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003978
Hanno Becker67bc7c32018-08-06 11:33:50 +01003979 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3980 return( ret );
3981
3982 continue;
3983 }
3984 max_hs_frag_len = max_frag_len - 12;
3985
3986 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3987 max_hs_frag_len : rem_len;
3988
3989 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003990 {
3991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003992 (unsigned) cur_hs_frag_len,
3993 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003994 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003995
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003996 /* Messages are stored with handshake headers as if not fragmented,
3997 * copy beginning of headers then fill fragmentation fields.
3998 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3999 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004000
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004001 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4002 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4003 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4004
Hanno Becker67bc7c32018-08-06 11:33:50 +01004005 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4006 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4007 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004008
4009 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4010
Hanno Becker3f7b9732018-08-28 09:53:25 +01004011 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004012 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4013 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004014 ssl->out_msgtype = cur->type;
4015
4016 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004017 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004018 }
4019
4020 /* If done with the current message move to the next one if any */
4021 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4022 {
4023 if( cur->next != NULL )
4024 {
4025 ssl->handshake->cur_msg = cur->next;
4026 ssl->handshake->cur_msg_p = cur->next->p + 12;
4027 }
4028 else
4029 {
4030 ssl->handshake->cur_msg = NULL;
4031 ssl->handshake->cur_msg_p = NULL;
4032 }
4033 }
4034
4035 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004036 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004039 return( ret );
4040 }
4041 }
4042
Hanno Becker67bc7c32018-08-06 11:33:50 +01004043 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4044 return( ret );
4045
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004046 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4048 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004049 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004051 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004052 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4053 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004054
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004056
4057 return( 0 );
4058}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004059
4060/*
4061 * To be called when the last message of an incoming flight is received.
4062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004064{
4065 /* We won't need to resend that one any more */
4066 ssl_flight_free( ssl->handshake->flight );
4067 ssl->handshake->flight = NULL;
4068 ssl->handshake->cur_msg = NULL;
4069
4070 /* The next incoming flight will start with this msg_seq */
4071 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4072
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004073 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004074 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004075
Hanno Becker0271f962018-08-16 13:23:47 +01004076 /* Clear future message buffering structure. */
4077 ssl_buffering_free( ssl );
4078
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004079 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004080 ssl_set_timer( ssl, 0 );
4081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4083 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004085 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004086 }
4087 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004088 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004089}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004090
4091/*
4092 * To be called when the last message of an outgoing flight is send.
4093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004095{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004096 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004097 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4100 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004103 }
4104 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004106}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004108
Paul Bakker5121ce52009-01-03 21:22:43 +00004109/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004110 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004111 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004112
4113/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004114 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004115 *
4116 * - fill in handshake headers
4117 * - update handshake checksum
4118 * - DTLS: save message for resending
4119 * - then pass to the record layer
4120 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004121 * DTLS: except for HelloRequest, messages are only queued, and will only be
4122 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004123 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004124 * Inputs:
4125 * - ssl->out_msglen: 4 + actual handshake message len
4126 * (4 is the size of handshake headers for TLS)
4127 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4128 * - ssl->out_msg + 4: the handshake message body
4129 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004130 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004131 * - ssl->out_msglen: the length of the record contents
4132 * (including handshake headers but excluding record headers)
4133 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004134 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004135int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004136{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004137 int ret;
4138 const size_t hs_len = ssl->out_msglen - 4;
4139 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004140
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4142
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004143 /*
4144 * Sanity checks
4145 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004146 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004147 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4148 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004149 /* In SSLv3, the client might send a NoCertificate alert. */
4150#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4151 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4152 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4153 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4154#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4155 {
4156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4157 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4158 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004159 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004160
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004161 /* Whenever we send anything different from a
4162 * HelloRequest we should be in a handshake - double check. */
4163 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4164 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004165 ssl->handshake == NULL )
4166 {
4167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4168 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4169 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004172 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004173 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004175 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004178 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004179#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004180
Hanno Beckerb50a2532018-08-06 11:52:54 +01004181 /* Double-check that we did not exceed the bounds
4182 * of the outgoing record buffer.
4183 * This should never fail as the various message
4184 * writing functions must obey the bounds of the
4185 * outgoing record buffer, but better be safe.
4186 *
4187 * Note: We deliberately do not check for the MTU or MFL here.
4188 */
4189 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4190 {
4191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4192 "size %u, maximum %u",
4193 (unsigned) ssl->out_msglen,
4194 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4196 }
4197
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004198 /*
4199 * Fill handshake headers
4200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004201 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004202 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004203 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4204 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4205 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004206
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004207 /*
4208 * DTLS has additional fields in the Handshake layer,
4209 * between the length field and the actual payload:
4210 * uint16 message_seq;
4211 * uint24 fragment_offset;
4212 * uint24 fragment_length;
4213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004215 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004216 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004217 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004218 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004219 {
4220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4221 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004222 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004223 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4225 }
4226
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004227 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004228 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004229
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004230 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004231 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004232 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004233 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4234 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4235 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004236 }
4237 else
4238 {
4239 ssl->out_msg[4] = 0;
4240 ssl->out_msg[5] = 0;
4241 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004242
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004243 /* Handshake hashes are computed without fragmentation,
4244 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004245 memset( ssl->out_msg + 6, 0x00, 3 );
4246 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004247 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004249
Hanno Becker0207e532018-08-28 10:28:28 +01004250 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004251 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4252 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004253 }
4254
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004255 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004257 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004258 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4259 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004260 {
4261 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004264 return( ret );
4265 }
4266 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004267 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004268#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004269 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004270 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004271 {
4272 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4273 return( ret );
4274 }
4275 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004276
4277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4278
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004279 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004280}
4281
4282/*
4283 * Record layer functions
4284 */
4285
4286/*
4287 * Write current record.
4288 *
4289 * Uses:
4290 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4291 * - ssl->out_msglen: length of the record content (excl headers)
4292 * - ssl->out_msg: record content
4293 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004294int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004295{
4296 int ret, done = 0;
4297 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004298 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004299
4300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004303 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004305 {
4306 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004309 return( ret );
4310 }
4311
4312 len = ssl->out_msglen;
4313 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4317 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321 ret = mbedtls_ssl_hw_record_write( ssl );
4322 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4325 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004326 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004327
4328 if( ret == 0 )
4329 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004330 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004332 if( !done )
4333 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004334 unsigned i;
4335 size_t protected_record_size;
4336
Hanno Becker6430faf2019-05-08 11:57:13 +01004337 /* Skip writing the record content type to after the encryption,
4338 * as it may change when using the CID extension. */
4339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004341 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004342
Hanno Becker19859472018-08-06 09:40:20 +01004343 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004344 ssl->out_len[0] = (unsigned char)( len >> 8 );
4345 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004346
Paul Bakker48916f92012-09-16 19:57:18 +00004347 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004348 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004349 mbedtls_record rec;
4350
4351 rec.buf = ssl->out_iv;
4352 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4353 ( ssl->out_iv - ssl->out_buf );
4354 rec.data_len = ssl->out_msglen;
4355 rec.data_offset = ssl->out_msg - rec.buf;
4356
4357 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4358 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4359 ssl->conf->transport, rec.ver );
4360 rec.type = ssl->out_msgtype;
4361
Hanno Beckera0e20d02019-05-15 14:03:01 +01004362#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004363 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004364 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004365#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004366
Hanno Beckera18d1322018-01-03 14:27:32 +00004367 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004368 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004371 return( ret );
4372 }
4373
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004374 if( rec.data_offset != 0 )
4375 {
4376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4377 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4378 }
4379
Hanno Becker6430faf2019-05-08 11:57:13 +01004380 /* Update the record content type and CID. */
4381 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004382#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004383 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004384#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004385 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004386 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4387 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004388 }
4389
Hanno Becker5903de42019-05-03 14:46:38 +01004390 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004391
4392#if defined(MBEDTLS_SSL_PROTO_DTLS)
4393 /* In case of DTLS, double-check that we don't exceed
4394 * the remaining space in the datagram. */
4395 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4396 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004397 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004398 if( ret < 0 )
4399 return( ret );
4400
4401 if( protected_record_size > (size_t) ret )
4402 {
4403 /* Should never happen */
4404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4405 }
4406 }
4407#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004408
Hanno Becker6430faf2019-05-08 11:57:13 +01004409 /* Now write the potentially updated record content type. */
4410 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004413 "version = [%d:%d], msglen = %d",
4414 ssl->out_hdr[0], ssl->out_hdr[1],
4415 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004417 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004418 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004419
4420 ssl->out_left += protected_record_size;
4421 ssl->out_hdr += protected_record_size;
4422 ssl_update_out_pointers( ssl, ssl->transform_out );
4423
Hanno Becker04484622018-08-06 09:49:38 +01004424 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4425 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4426 break;
4427
4428 /* The loop goes to its end iff the counter is wrapping */
4429 if( i == ssl_ep_len( ssl ) )
4430 {
4431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4432 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4433 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004434 }
4435
Hanno Becker67bc7c32018-08-06 11:33:50 +01004436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004437 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4438 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004439 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004440 size_t remaining;
4441 ret = ssl_get_remaining_payload_in_datagram( ssl );
4442 if( ret < 0 )
4443 {
4444 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4445 ret );
4446 return( ret );
4447 }
4448
4449 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004450 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004451 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004452 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004453 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004454 else
4455 {
Hanno Becker513815a2018-08-20 11:56:09 +01004456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004457 }
4458 }
4459#endif /* MBEDTLS_SSL_PROTO_DTLS */
4460
4461 if( ( flush == SSL_FORCE_FLUSH ) &&
4462 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004465 return( ret );
4466 }
4467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004469
4470 return( 0 );
4471}
4472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004474
4475static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4476{
4477 if( ssl->in_msglen < ssl->in_hslen ||
4478 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4479 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4480 {
4481 return( 1 );
4482 }
4483 return( 0 );
4484}
Hanno Becker44650b72018-08-16 12:51:11 +01004485
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004486static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004487{
4488 return( ( ssl->in_msg[9] << 16 ) |
4489 ( ssl->in_msg[10] << 8 ) |
4490 ssl->in_msg[11] );
4491}
4492
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004493static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004494{
4495 return( ( ssl->in_msg[6] << 16 ) |
4496 ( ssl->in_msg[7] << 8 ) |
4497 ssl->in_msg[8] );
4498}
4499
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004500static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004501{
4502 uint32_t msg_len, frag_off, frag_len;
4503
4504 msg_len = ssl_get_hs_total_len( ssl );
4505 frag_off = ssl_get_hs_frag_off( ssl );
4506 frag_len = ssl_get_hs_frag_len( ssl );
4507
4508 if( frag_off > msg_len )
4509 return( -1 );
4510
4511 if( frag_len > msg_len - frag_off )
4512 return( -1 );
4513
4514 if( frag_len + 12 > ssl->in_msglen )
4515 return( -1 );
4516
4517 return( 0 );
4518}
4519
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004520/*
4521 * Mark bits in bitmask (used for DTLS HS reassembly)
4522 */
4523static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4524{
4525 unsigned int start_bits, end_bits;
4526
4527 start_bits = 8 - ( offset % 8 );
4528 if( start_bits != 8 )
4529 {
4530 size_t first_byte_idx = offset / 8;
4531
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004532 /* Special case */
4533 if( len <= start_bits )
4534 {
4535 for( ; len != 0; len-- )
4536 mask[first_byte_idx] |= 1 << ( start_bits - len );
4537
4538 /* Avoid potential issues with offset or len becoming invalid */
4539 return;
4540 }
4541
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004542 offset += start_bits; /* Now offset % 8 == 0 */
4543 len -= start_bits;
4544
4545 for( ; start_bits != 0; start_bits-- )
4546 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4547 }
4548
4549 end_bits = len % 8;
4550 if( end_bits != 0 )
4551 {
4552 size_t last_byte_idx = ( offset + len ) / 8;
4553
4554 len -= end_bits; /* Now len % 8 == 0 */
4555
4556 for( ; end_bits != 0; end_bits-- )
4557 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4558 }
4559
4560 memset( mask + offset / 8, 0xFF, len / 8 );
4561}
4562
4563/*
4564 * Check that bitmask is full
4565 */
4566static int ssl_bitmask_check( unsigned char *mask, size_t len )
4567{
4568 size_t i;
4569
4570 for( i = 0; i < len / 8; i++ )
4571 if( mask[i] != 0xFF )
4572 return( -1 );
4573
4574 for( i = 0; i < len % 8; i++ )
4575 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4576 return( -1 );
4577
4578 return( 0 );
4579}
4580
Hanno Becker56e205e2018-08-16 09:06:12 +01004581/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004582static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004583 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004584{
Hanno Becker56e205e2018-08-16 09:06:12 +01004585 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004586
Hanno Becker56e205e2018-08-16 09:06:12 +01004587 alloc_len = 12; /* Handshake header */
4588 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004589
Hanno Beckerd07df862018-08-16 09:14:58 +01004590 if( add_bitmap )
4591 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004592
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004593 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004594}
Hanno Becker56e205e2018-08-16 09:06:12 +01004595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004597
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004598static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004599{
4600 return( ( ssl->in_msg[1] << 16 ) |
4601 ( ssl->in_msg[2] << 8 ) |
4602 ssl->in_msg[3] );
4603}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004604
Simon Butcher99000142016-10-13 17:21:01 +01004605int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004606{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004607 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004610 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004611 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004612 }
4613
Hanno Becker12555c62018-08-16 12:47:53 +01004614 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004617 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004618 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004621 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004622 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004623 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004624 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004625
Hanno Becker44650b72018-08-16 12:51:11 +01004626 if( ssl_check_hs_header( ssl ) != 0 )
4627 {
4628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4629 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4630 }
4631
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004632 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004633 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4634 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4635 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4636 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004637 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004638 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4639 {
4640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4641 recv_msg_seq,
4642 ssl->handshake->in_msg_seq ) );
4643 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4644 }
4645
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004646 /* Retransmit only on last message from previous flight, to avoid
4647 * too many retransmissions.
4648 * Besides, No sane server ever retransmits HelloVerifyRequest */
4649 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004653 "message_seq = %d, start_of_flight = %d",
4654 recv_msg_seq,
4655 ssl->handshake->in_flight_start_seq ) );
4656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004657 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004660 return( ret );
4661 }
4662 }
4663 else
4664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004666 "message_seq = %d, expected = %d",
4667 recv_msg_seq,
4668 ssl->handshake->in_msg_seq ) );
4669 }
4670
Hanno Becker90333da2017-10-10 11:27:13 +01004671 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004672 }
4673 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004674
Hanno Becker6d97ef52018-08-16 13:09:04 +01004675 /* Message reassembly is handled alongside buffering of future
4676 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004677 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004678 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004679 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004682 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004683 }
4684 }
4685 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004686#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004687 /* With TLS we don't handle fragmentation (for now) */
4688 if( ssl->in_msglen < ssl->in_hslen )
4689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4691 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004692 }
4693
Simon Butcher99000142016-10-13 17:21:01 +01004694 return( 0 );
4695}
4696
4697void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4698{
Hanno Becker0271f962018-08-16 13:23:47 +01004699 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004700
Hanno Becker0271f962018-08-16 13:23:47 +01004701 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004702 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004703 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004704 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004705
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004706 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004708 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004709 ssl->handshake != NULL )
4710 {
Hanno Becker0271f962018-08-16 13:23:47 +01004711 unsigned offset;
4712 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004713
Hanno Becker0271f962018-08-16 13:23:47 +01004714 /* Increment handshake sequence number */
4715 hs->in_msg_seq++;
4716
4717 /*
4718 * Clear up handshake buffering and reassembly structure.
4719 */
4720
4721 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004722 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004723
4724 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004725 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4726 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004727 offset++, hs_buf++ )
4728 {
4729 *hs_buf = *(hs_buf + 1);
4730 }
4731
4732 /* Create a fresh last entry */
4733 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004734 }
4735#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004736}
4737
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004738/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004739 * DTLS anti-replay: RFC 6347 4.1.2.6
4740 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004741 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4742 * Bit n is set iff record number in_window_top - n has been seen.
4743 *
4744 * Usually, in_window_top is the last record number seen and the lsb of
4745 * in_window is set. The only exception is the initial state (record number 0
4746 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4749static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004750{
4751 ssl->in_window_top = 0;
4752 ssl->in_window = 0;
4753}
4754
4755static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4756{
4757 return( ( (uint64_t) buf[0] << 40 ) |
4758 ( (uint64_t) buf[1] << 32 ) |
4759 ( (uint64_t) buf[2] << 24 ) |
4760 ( (uint64_t) buf[3] << 16 ) |
4761 ( (uint64_t) buf[4] << 8 ) |
4762 ( (uint64_t) buf[5] ) );
4763}
4764
4765/*
4766 * Return 0 if sequence number is acceptable, -1 otherwise
4767 */
Hanno Becker0183d692019-07-12 08:50:37 +01004768int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004769{
4770 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4771 uint64_t bit;
4772
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004773 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004774 return( 0 );
4775
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004776 if( rec_seqnum > ssl->in_window_top )
4777 return( 0 );
4778
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004779 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004780
4781 if( bit >= 64 )
4782 return( -1 );
4783
4784 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4785 return( -1 );
4786
4787 return( 0 );
4788}
4789
4790/*
4791 * Update replay window on new validated record
4792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004794{
4795 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4796
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004797 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004798 return;
4799
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004800 if( rec_seqnum > ssl->in_window_top )
4801 {
4802 /* Update window_top and the contents of the window */
4803 uint64_t shift = rec_seqnum - ssl->in_window_top;
4804
4805 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004806 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004807 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004808 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004809 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004810 ssl->in_window |= 1;
4811 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004812
4813 ssl->in_window_top = rec_seqnum;
4814 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004815 else
4816 {
4817 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004818 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004819
4820 if( bit < 64 ) /* Always true, but be extra sure */
4821 ssl->in_window |= (uint64_t) 1 << bit;
4822 }
4823}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004825
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004826#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004827/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004828static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4829
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004830/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004831 * Without any SSL context, check if a datagram looks like a ClientHello with
4832 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004833 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004834 *
4835 * - if cookie is valid, return 0
4836 * - if ClientHello looks superficially valid but cookie is not,
4837 * fill obuf and set olen, then
4838 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4839 * - otherwise return a specific error code
4840 */
4841static int ssl_check_dtls_clihlo_cookie(
4842 mbedtls_ssl_cookie_write_t *f_cookie_write,
4843 mbedtls_ssl_cookie_check_t *f_cookie_check,
4844 void *p_cookie,
4845 const unsigned char *cli_id, size_t cli_id_len,
4846 const unsigned char *in, size_t in_len,
4847 unsigned char *obuf, size_t buf_len, size_t *olen )
4848{
4849 size_t sid_len, cookie_len;
4850 unsigned char *p;
4851
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004852 /*
4853 * Structure of ClientHello with record and handshake headers,
4854 * and expected values. We don't need to check a lot, more checks will be
4855 * done when actually parsing the ClientHello - skipping those checks
4856 * avoids code duplication and does not make cookie forging any easier.
4857 *
4858 * 0-0 ContentType type; copied, must be handshake
4859 * 1-2 ProtocolVersion version; copied
4860 * 3-4 uint16 epoch; copied, must be 0
4861 * 5-10 uint48 sequence_number; copied
4862 * 11-12 uint16 length; (ignored)
4863 *
4864 * 13-13 HandshakeType msg_type; (ignored)
4865 * 14-16 uint24 length; (ignored)
4866 * 17-18 uint16 message_seq; copied
4867 * 19-21 uint24 fragment_offset; copied, must be 0
4868 * 22-24 uint24 fragment_length; (ignored)
4869 *
4870 * 25-26 ProtocolVersion client_version; (ignored)
4871 * 27-58 Random random; (ignored)
4872 * 59-xx SessionID session_id; 1 byte len + sid_len content
4873 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4874 * ...
4875 *
4876 * Minimum length is 61 bytes.
4877 */
4878 if( in_len < 61 ||
4879 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4880 in[3] != 0 || in[4] != 0 ||
4881 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4882 {
4883 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4884 }
4885
4886 sid_len = in[59];
4887 if( sid_len > in_len - 61 )
4888 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4889
4890 cookie_len = in[60 + sid_len];
4891 if( cookie_len > in_len - 60 )
4892 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4893
4894 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4895 cli_id, cli_id_len ) == 0 )
4896 {
4897 /* Valid cookie */
4898 return( 0 );
4899 }
4900
4901 /*
4902 * If we get here, we've got an invalid cookie, let's prepare HVR.
4903 *
4904 * 0-0 ContentType type; copied
4905 * 1-2 ProtocolVersion version; copied
4906 * 3-4 uint16 epoch; copied
4907 * 5-10 uint48 sequence_number; copied
4908 * 11-12 uint16 length; olen - 13
4909 *
4910 * 13-13 HandshakeType msg_type; hello_verify_request
4911 * 14-16 uint24 length; olen - 25
4912 * 17-18 uint16 message_seq; copied
4913 * 19-21 uint24 fragment_offset; copied
4914 * 22-24 uint24 fragment_length; olen - 25
4915 *
4916 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4917 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4918 *
4919 * Minimum length is 28.
4920 */
4921 if( buf_len < 28 )
4922 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4923
4924 /* Copy most fields and adapt others */
4925 memcpy( obuf, in, 25 );
4926 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4927 obuf[25] = 0xfe;
4928 obuf[26] = 0xff;
4929
4930 /* Generate and write actual cookie */
4931 p = obuf + 28;
4932 if( f_cookie_write( p_cookie,
4933 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4934 {
4935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4936 }
4937
4938 *olen = p - obuf;
4939
4940 /* Go back and fill length fields */
4941 obuf[27] = (unsigned char)( *olen - 28 );
4942
4943 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4944 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4945 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4946
4947 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4948 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4949
4950 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4951}
4952
4953/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004954 * Handle possible client reconnect with the same UDP quadruplet
4955 * (RFC 6347 Section 4.2.8).
4956 *
4957 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4958 * that looks like a ClientHello.
4959 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004960 * - if the input looks like a ClientHello without cookies,
4961 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004962 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004963 * - if the input looks like a ClientHello with a valid cookie,
4964 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004965 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004966 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004967 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004968 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004969 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4970 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004971 */
4972static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4973{
4974 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004975 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004976
Hanno Becker2fddd372019-07-10 14:37:41 +01004977 if( ssl->conf->f_cookie_write == NULL ||
4978 ssl->conf->f_cookie_check == NULL )
4979 {
4980 /* If we can't use cookies to verify reachability of the peer,
4981 * drop the record. */
4982 return( 0 );
4983 }
4984
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004985 ret = ssl_check_dtls_clihlo_cookie(
4986 ssl->conf->f_cookie_write,
4987 ssl->conf->f_cookie_check,
4988 ssl->conf->p_cookie,
4989 ssl->cli_id, ssl->cli_id_len,
4990 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004991 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004992
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004993 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4994
4995 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004996 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004997 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004998 * If the error is permanent we'll catch it later,
4999 * if it's not, then hopefully it'll work next time. */
5000 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01005001 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005002 }
5003
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005004 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005005 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005006 /* Got a valid cookie, partially reset context */
5007 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5008 {
5009 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5010 return( ret );
5011 }
5012
5013 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005014 }
5015
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005016 return( ret );
5017}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005018#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005019
Hanno Beckerf661c9c2019-05-03 13:25:54 +01005020static int ssl_check_record_type( uint8_t record_type )
5021{
5022 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5023 record_type != MBEDTLS_SSL_MSG_ALERT &&
5024 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5025 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5026 {
5027 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5028 }
5029
5030 return( 0 );
5031}
5032
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005033/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005034 * ContentType type;
5035 * ProtocolVersion version;
5036 * uint16 epoch; // DTLS only
5037 * uint48 sequence_number; // DTLS only
5038 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005039 *
5040 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005041 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005042 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5043 *
5044 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005045 * 1. proceed with the record if this function returns 0
5046 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5047 * 3. return CLIENT_RECONNECT if this function return that value
5048 * 4. drop the whole datagram if this function returns anything else.
5049 * Point 2 is needed when the peer is resending, and we have already received
5050 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005051 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005052static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005053 unsigned char *buf,
5054 size_t len,
5055 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005056{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005057 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005058
Hanno Beckere5e7e782019-07-11 12:29:35 +01005059 size_t const rec_hdr_type_offset = 0;
5060 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005061
Hanno Beckere5e7e782019-07-11 12:29:35 +01005062 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5063 rec_hdr_type_len;
5064 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005065
Hanno Beckere5e7e782019-07-11 12:29:35 +01005066 size_t const rec_hdr_ctr_len = 8;
5067#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005068 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005069 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5070 rec_hdr_version_len;
5071
Hanno Beckera0e20d02019-05-15 14:03:01 +01005072#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005073 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5074 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005075 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005076#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5077#endif /* MBEDTLS_SSL_PROTO_DTLS */
5078
5079 size_t rec_hdr_len_offset; /* To be determined */
5080 size_t const rec_hdr_len_len = 2;
5081
5082 /*
5083 * Check minimum lengths for record header.
5084 */
5085
5086#if defined(MBEDTLS_SSL_PROTO_DTLS)
5087 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5088 {
5089 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5090 }
5091 else
5092#endif /* MBEDTLS_SSL_PROTO_DTLS */
5093 {
5094 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5095 }
5096
5097 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5098 {
5099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5100 (unsigned) len,
5101 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5102 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5103 }
5104
5105 /*
5106 * Parse and validate record content type
5107 */
5108
5109 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005110
5111 /* Check record content type */
5112#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5113 rec->cid_len = 0;
5114
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005115 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005116 ssl->conf->cid_len != 0 &&
5117 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005118 {
5119 /* Shift pointers to account for record header including CID
5120 * struct {
5121 * ContentType special_type = tls12_cid;
5122 * ProtocolVersion version;
5123 * uint16 epoch;
5124 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005125 * opaque cid[cid_length]; // Additional field compared to
5126 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005127 * uint16 length;
5128 * opaque enc_content[DTLSCiphertext.length];
5129 * } DTLSCiphertext;
5130 */
5131
5132 /* So far, we only support static CID lengths
5133 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005134 rec_hdr_cid_len = ssl->conf->cid_len;
5135 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005136
Hanno Beckere5e7e782019-07-11 12:29:35 +01005137 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005138 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5140 (unsigned) len,
5141 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005142 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005143 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005144
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005145 /* configured CID len is guaranteed at most 255, see
5146 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5147 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005148 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005149 }
5150 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005151#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005152 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005153 if( ssl_check_record_type( rec->type ) )
5154 {
Hanno Becker54229812019-07-12 14:40:00 +01005155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5156 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005157 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5158 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005159 }
5160
Hanno Beckere5e7e782019-07-11 12:29:35 +01005161 /*
5162 * Parse and validate record version
5163 */
5164
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005165 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5166 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005167 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5168 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005169 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005170
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005171 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5174 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005175 }
5176
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005177 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5180 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005181 }
5182
Hanno Beckere5e7e782019-07-11 12:29:35 +01005183 /*
5184 * Parse/Copy record sequence number.
5185 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005186
Hanno Beckere5e7e782019-07-11 12:29:35 +01005187#if defined(MBEDTLS_SSL_PROTO_DTLS)
5188 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005189 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005190 /* Copy explicit record sequence number from input buffer. */
5191 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5192 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005193 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005194 else
5195#endif /* MBEDTLS_SSL_PROTO_DTLS */
5196 {
5197 /* Copy implicit record sequence number from SSL context structure. */
5198 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5199 }
Paul Bakker40e46942009-01-03 21:51:57 +00005200
Hanno Beckere5e7e782019-07-11 12:29:35 +01005201 /*
5202 * Parse record length.
5203 */
5204
Hanno Beckere5e7e782019-07-11 12:29:35 +01005205 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005206 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5207 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005208 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005209
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005211 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005212 rec->type,
5213 major_ver, minor_ver, rec->data_len ) );
5214
5215 rec->buf = buf;
5216 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005217
Hanno Beckerd417cc92019-07-26 08:20:27 +01005218 if( rec->data_len == 0 )
5219 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005220
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005221 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005222 * DTLS-related tests.
5223 * Check epoch before checking length constraint because
5224 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5225 * message gets duplicated before the corresponding Finished message,
5226 * the second ChangeCipherSpec should be discarded because it belongs
5227 * to an old epoch, but not because its length is shorter than
5228 * the minimum record length for packets using the new record transform.
5229 * Note that these two kinds of failures are handled differently,
5230 * as an unexpected record is silently skipped but an invalid
5231 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005232 */
5233#if defined(MBEDTLS_SSL_PROTO_DTLS)
5234 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5235 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005236 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005237
Hanno Becker955a5c92019-07-10 17:12:07 +01005238 /* Check that the datagram is large enough to contain a record
5239 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005240 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005241 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5243 (unsigned) len,
5244 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005245 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5246 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005247
Hanno Becker37cfe732019-07-10 17:20:01 +01005248 /* Records from other, non-matching epochs are silently discarded.
5249 * (The case of same-port Client reconnects must be considered in
5250 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005251 if( rec_epoch != ssl->in_epoch )
5252 {
5253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5254 "expected %d, received %d",
5255 ssl->in_epoch, rec_epoch ) );
5256
Hanno Becker552f7472019-07-19 10:59:12 +01005257 /* Records from the next epoch are considered for buffering
5258 * (concretely: early Finished messages). */
5259 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005260 {
Hanno Becker552f7472019-07-19 10:59:12 +01005261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5262 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005263 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005264
Hanno Becker2fddd372019-07-10 14:37:41 +01005265 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005266 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005267#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005268 /* For records from the correct epoch, check whether their
5269 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005270 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005271 {
5272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5273 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5274 }
5275#endif
5276 }
5277#endif /* MBEDTLS_SSL_PROTO_DTLS */
5278
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005279 return( 0 );
5280}
Paul Bakker5121ce52009-01-03 21:22:43 +00005281
Paul Bakker5121ce52009-01-03 21:22:43 +00005282
Hanno Becker2fddd372019-07-10 14:37:41 +01005283#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5284static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5285{
5286 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5287
5288 /*
5289 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5290 * access the first byte of record content (handshake type), as we
5291 * have an active transform (possibly iv_len != 0), so use the
5292 * fact that the record header len is 13 instead.
5293 */
5294 if( rec_epoch == 0 &&
5295 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5296 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5297 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5298 ssl->in_left > 13 &&
5299 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5300 {
5301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5302 "from the same port" ) );
5303 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005304 }
5305
5306 return( 0 );
5307}
Hanno Becker2fddd372019-07-10 14:37:41 +01005308#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005309
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005310/*
5311 * If applicable, decrypt (and decompress) record content
5312 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005313static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5314 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005315{
5316 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005319 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5322 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 ret = mbedtls_ssl_hw_record_read( ssl );
5327 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5330 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005331 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005332
5333 if( ret == 0 )
5334 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005335 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005337 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005338 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005339 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005340
Hanno Beckera18d1322018-01-03 14:27:32 +00005341 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005342 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005345
Hanno Beckera0e20d02019-05-15 14:03:01 +01005346#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005347 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5348 ssl->conf->ignore_unexpected_cid
5349 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5350 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005351 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005352 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005353 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005354#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005355
Paul Bakker5121ce52009-01-03 21:22:43 +00005356 return( ret );
5357 }
5358
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005359 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005360 {
5361 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005362 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005363 }
5364
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005365 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005366 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005367
Hanno Beckera0e20d02019-05-15 14:03:01 +01005368#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005369 /* We have already checked the record content type
5370 * in ssl_parse_record_header(), failing or silently
5371 * dropping the record in the case of an unknown type.
5372 *
5373 * Since with the use of CIDs, the record content type
5374 * might change during decryption, re-check the record
5375 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005376 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005377 {
5378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5379 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5380 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005381#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005382
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005383 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005384 {
5385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5386 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005387 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005388 {
5389 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5391 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5392 }
5393#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5394
5395 ssl->nb_zero++;
5396
5397 /*
5398 * Three or more empty messages may be a DoS attack
5399 * (excessive CPU consumption).
5400 */
5401 if( ssl->nb_zero > 3 )
5402 {
5403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005404 "messages, possible DoS attack" ) );
5405 /* Treat the records as if they were not properly authenticated,
5406 * thereby failing the connection if we see more than allowed
5407 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005408 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5409 }
5410 }
5411 else
5412 ssl->nb_zero = 0;
5413
5414#if defined(MBEDTLS_SSL_PROTO_DTLS)
5415 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5416 {
5417 ; /* in_ctr read from peer, not maintained internally */
5418 }
5419 else
5420#endif
5421 {
5422 unsigned i;
5423 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5424 if( ++ssl->in_ctr[i - 1] != 0 )
5425 break;
5426
5427 /* The loop goes to its end iff the counter is wrapping */
5428 if( i == ssl_ep_len( ssl ) )
5429 {
5430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5431 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5432 }
5433 }
5434
Paul Bakker5121ce52009-01-03 21:22:43 +00005435 }
5436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005438 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005440 {
5441 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005444 return( ret );
5445 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005446 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005450 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005453 }
5454#endif
5455
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005456 /* Check actual (decrypted) record content length against
5457 * configured maximum. */
5458 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5459 {
5460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5461 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5462 }
5463
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005464 return( 0 );
5465}
5466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005468
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005469/*
5470 * Read a record.
5471 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005472 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5473 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5474 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005475 */
Hanno Becker1097b342018-08-15 14:09:41 +01005476
5477/* Helper functions for mbedtls_ssl_read_record(). */
5478static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005479static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5480static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005481
Hanno Becker327c93b2018-08-15 13:56:18 +01005482int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005483 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005484{
5485 int ret;
5486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005488
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005489 if( ssl->keep_current_message == 0 )
5490 {
5491 do {
Simon Butcher99000142016-10-13 17:21:01 +01005492
Hanno Becker26994592018-08-15 14:14:59 +01005493 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005494 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005495 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005496
Hanno Beckere74d5562018-08-15 14:26:08 +01005497 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005498 {
Hanno Becker40f50842018-08-15 14:48:01 +01005499#if defined(MBEDTLS_SSL_PROTO_DTLS)
5500 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005501
Hanno Becker40f50842018-08-15 14:48:01 +01005502 /* We only check for buffered messages if the
5503 * current datagram is fully consumed. */
5504 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005505 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005506 {
Hanno Becker40f50842018-08-15 14:48:01 +01005507 if( ssl_load_buffered_message( ssl ) == 0 )
5508 have_buffered = 1;
5509 }
5510
5511 if( have_buffered == 0 )
5512#endif /* MBEDTLS_SSL_PROTO_DTLS */
5513 {
5514 ret = ssl_get_next_record( ssl );
5515 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5516 continue;
5517
5518 if( ret != 0 )
5519 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005520 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005521 return( ret );
5522 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005523 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005524 }
5525
5526 ret = mbedtls_ssl_handle_message_type( ssl );
5527
Hanno Becker40f50842018-08-15 14:48:01 +01005528#if defined(MBEDTLS_SSL_PROTO_DTLS)
5529 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5530 {
5531 /* Buffer future message */
5532 ret = ssl_buffer_message( ssl );
5533 if( ret != 0 )
5534 return( ret );
5535
5536 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5537 }
5538#endif /* MBEDTLS_SSL_PROTO_DTLS */
5539
Hanno Becker90333da2017-10-10 11:27:13 +01005540 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5541 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005542
5543 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005544 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005545 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005546 return( ret );
5547 }
5548
Hanno Becker327c93b2018-08-15 13:56:18 +01005549 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005550 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005551 {
5552 mbedtls_ssl_update_handshake_status( ssl );
5553 }
Simon Butcher99000142016-10-13 17:21:01 +01005554 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005555 else
Simon Butcher99000142016-10-13 17:21:01 +01005556 {
Hanno Becker02f59072018-08-15 14:00:24 +01005557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005558 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005559 }
5560
5561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5562
5563 return( 0 );
5564}
5565
Hanno Becker40f50842018-08-15 14:48:01 +01005566#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005567static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005568{
Hanno Becker40f50842018-08-15 14:48:01 +01005569 if( ssl->in_left > ssl->next_record_offset )
5570 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005571
Hanno Becker40f50842018-08-15 14:48:01 +01005572 return( 0 );
5573}
5574
5575static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5576{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005577 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005578 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005579 int ret = 0;
5580
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005581 if( hs == NULL )
5582 return( -1 );
5583
Hanno Beckere00ae372018-08-20 09:39:42 +01005584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5585
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005586 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5587 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5588 {
5589 /* Check if we have seen a ChangeCipherSpec before.
5590 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005591 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005592 {
5593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5594 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005595 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005596 }
5597
Hanno Becker39b8bc92018-08-28 17:17:13 +01005598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005599 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5600 ssl->in_msglen = 1;
5601 ssl->in_msg[0] = 1;
5602
5603 /* As long as they are equal, the exact value doesn't matter. */
5604 ssl->in_left = 0;
5605 ssl->next_record_offset = 0;
5606
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005607 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005608 goto exit;
5609 }
Hanno Becker37f95322018-08-16 13:55:32 +01005610
Hanno Beckerb8f50142018-08-28 10:01:34 +01005611#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005612 /* Debug only */
5613 {
5614 unsigned offset;
5615 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5616 {
5617 hs_buf = &hs->buffering.hs[offset];
5618 if( hs_buf->is_valid == 1 )
5619 {
5620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5621 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005622 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005623 }
5624 }
5625 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005626#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005627
5628 /* Check if we have buffered and/or fully reassembled the
5629 * next handshake message. */
5630 hs_buf = &hs->buffering.hs[0];
5631 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5632 {
5633 /* Synthesize a record containing the buffered HS message. */
5634 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5635 ( hs_buf->data[2] << 8 ) |
5636 hs_buf->data[3];
5637
5638 /* Double-check that we haven't accidentally buffered
5639 * a message that doesn't fit into the input buffer. */
5640 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5641 {
5642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5643 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5644 }
5645
5646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5647 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5648 hs_buf->data, msg_len + 12 );
5649
5650 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5651 ssl->in_hslen = msg_len + 12;
5652 ssl->in_msglen = msg_len + 12;
5653 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5654
5655 ret = 0;
5656 goto exit;
5657 }
5658 else
5659 {
5660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5661 hs->in_msg_seq ) );
5662 }
5663
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005664 ret = -1;
5665
5666exit:
5667
5668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5669 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005670}
5671
Hanno Beckera02b0b42018-08-21 17:20:27 +01005672static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5673 size_t desired )
5674{
5675 int offset;
5676 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5678 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005679
Hanno Becker01315ea2018-08-21 17:22:17 +01005680 /* Get rid of future records epoch first, if such exist. */
5681 ssl_free_buffered_record( ssl );
5682
5683 /* Check if we have enough space available now. */
5684 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5685 hs->buffering.total_bytes_buffered ) )
5686 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005688 return( 0 );
5689 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005690
Hanno Becker4f432ad2018-08-28 10:02:32 +01005691 /* We don't have enough space to buffer the next expected handshake
5692 * message. Remove buffers used for future messages to gain space,
5693 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005694 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5695 offset >= 0; offset-- )
5696 {
5697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5698 offset ) );
5699
Hanno Beckerb309b922018-08-23 13:18:05 +01005700 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005701
5702 /* Check if we have enough space available now. */
5703 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5704 hs->buffering.total_bytes_buffered ) )
5705 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005707 return( 0 );
5708 }
5709 }
5710
5711 return( -1 );
5712}
5713
Hanno Becker40f50842018-08-15 14:48:01 +01005714static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5715{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005716 int ret = 0;
5717 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5718
5719 if( hs == NULL )
5720 return( 0 );
5721
5722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5723
5724 switch( ssl->in_msgtype )
5725 {
5726 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005728
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005729 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005730 break;
5731
5732 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005733 {
5734 unsigned recv_msg_seq_offset;
5735 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5736 mbedtls_ssl_hs_buffer *hs_buf;
5737 size_t msg_len = ssl->in_hslen - 12;
5738
5739 /* We should never receive an old handshake
5740 * message - double-check nonetheless. */
5741 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5742 {
5743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5744 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5745 }
5746
5747 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5748 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5749 {
5750 /* Silently ignore -- message too far in the future */
5751 MBEDTLS_SSL_DEBUG_MSG( 2,
5752 ( "Ignore future HS message with sequence number %u, "
5753 "buffering window %u - %u",
5754 recv_msg_seq, ssl->handshake->in_msg_seq,
5755 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5756
5757 goto exit;
5758 }
5759
5760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5761 recv_msg_seq, recv_msg_seq_offset ) );
5762
5763 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5764
5765 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005766 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005767 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005768 size_t reassembly_buf_sz;
5769
Hanno Becker37f95322018-08-16 13:55:32 +01005770 hs_buf->is_fragmented =
5771 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5772
5773 /* We copy the message back into the input buffer
5774 * after reassembly, so check that it's not too large.
5775 * This is an implementation-specific limitation
5776 * and not one from the standard, hence it is not
5777 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005778 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005779 {
5780 /* Ignore message */
5781 goto exit;
5782 }
5783
Hanno Beckere0b150f2018-08-21 15:51:03 +01005784 /* Check if we have enough space to buffer the message. */
5785 if( hs->buffering.total_bytes_buffered >
5786 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5787 {
5788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5789 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5790 }
5791
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005792 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5793 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005794
5795 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5796 hs->buffering.total_bytes_buffered ) )
5797 {
5798 if( recv_msg_seq_offset > 0 )
5799 {
5800 /* If we can't buffer a future message because
5801 * of space limitations -- ignore. */
5802 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",
5803 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5804 (unsigned) hs->buffering.total_bytes_buffered ) );
5805 goto exit;
5806 }
Hanno Beckere1801392018-08-21 16:51:05 +01005807 else
5808 {
5809 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",
5810 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5811 (unsigned) hs->buffering.total_bytes_buffered ) );
5812 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005813
Hanno Beckera02b0b42018-08-21 17:20:27 +01005814 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005815 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005816 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",
5817 (unsigned) msg_len,
5818 (unsigned) reassembly_buf_sz,
5819 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005820 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005821 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5822 goto exit;
5823 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005824 }
5825
5826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5827 msg_len ) );
5828
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005829 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5830 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005831 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005832 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005833 goto exit;
5834 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005835 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005836
5837 /* Prepare final header: copy msg_type, length and message_seq,
5838 * then add standardised fragment_offset and fragment_length */
5839 memcpy( hs_buf->data, ssl->in_msg, 6 );
5840 memset( hs_buf->data + 6, 0, 3 );
5841 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5842
5843 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005844
5845 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005846 }
5847 else
5848 {
5849 /* Make sure msg_type and length are consistent */
5850 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5851 {
5852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5853 /* Ignore */
5854 goto exit;
5855 }
5856 }
5857
Hanno Becker4422bbb2018-08-20 09:40:19 +01005858 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005859 {
5860 size_t frag_len, frag_off;
5861 unsigned char * const msg = hs_buf->data + 12;
5862
5863 /*
5864 * Check and copy current fragment
5865 */
5866
5867 /* Validation of header fields already done in
5868 * mbedtls_ssl_prepare_handshake_record(). */
5869 frag_off = ssl_get_hs_frag_off( ssl );
5870 frag_len = ssl_get_hs_frag_len( ssl );
5871
5872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5873 frag_off, frag_len ) );
5874 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5875
5876 if( hs_buf->is_fragmented )
5877 {
5878 unsigned char * const bitmask = msg + msg_len;
5879 ssl_bitmask_set( bitmask, frag_off, frag_len );
5880 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5881 msg_len ) == 0 );
5882 }
5883 else
5884 {
5885 hs_buf->is_complete = 1;
5886 }
5887
5888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5889 hs_buf->is_complete ? "" : "not yet " ) );
5890 }
5891
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005892 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005893 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005894
5895 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005896 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005897 break;
5898 }
5899
5900exit:
5901
5902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5903 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005904}
5905#endif /* MBEDTLS_SSL_PROTO_DTLS */
5906
Hanno Becker1097b342018-08-15 14:09:41 +01005907static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005908{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005909 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005910 * Consume last content-layer message and potentially
5911 * update in_msglen which keeps track of the contents'
5912 * consumption state.
5913 *
5914 * (1) Handshake messages:
5915 * Remove last handshake message, move content
5916 * and adapt in_msglen.
5917 *
5918 * (2) Alert messages:
5919 * Consume whole record content, in_msglen = 0.
5920 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005921 * (3) Change cipher spec:
5922 * Consume whole record content, in_msglen = 0.
5923 *
5924 * (4) Application data:
5925 * Don't do anything - the record layer provides
5926 * the application data as a stream transport
5927 * and consumes through mbedtls_ssl_read only.
5928 *
5929 */
5930
5931 /* Case (1): Handshake messages */
5932 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005933 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005934 /* Hard assertion to be sure that no application data
5935 * is in flight, as corrupting ssl->in_msglen during
5936 * ssl->in_offt != NULL is fatal. */
5937 if( ssl->in_offt != NULL )
5938 {
5939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5940 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5941 }
5942
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005943 /*
5944 * Get next Handshake message in the current record
5945 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005946
Hanno Becker4a810fb2017-05-24 16:27:30 +01005947 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005948 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005949 * current handshake content: If DTLS handshake
5950 * fragmentation is used, that's the fragment
5951 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005952 * size here is faulty and should be changed at
5953 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005954 * (2) While it doesn't seem to cause problems, one
5955 * has to be very careful not to assume that in_hslen
5956 * is always <= in_msglen in a sensible communication.
5957 * Again, it's wrong for DTLS handshake fragmentation.
5958 * The following check is therefore mandatory, and
5959 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005960 * Additionally, ssl->in_hslen might be arbitrarily out of
5961 * bounds after handling a DTLS message with an unexpected
5962 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005963 */
5964 if( ssl->in_hslen < ssl->in_msglen )
5965 {
5966 ssl->in_msglen -= ssl->in_hslen;
5967 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5968 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005969
Hanno Becker4a810fb2017-05-24 16:27:30 +01005970 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5971 ssl->in_msg, ssl->in_msglen );
5972 }
5973 else
5974 {
5975 ssl->in_msglen = 0;
5976 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005977
Hanno Becker4a810fb2017-05-24 16:27:30 +01005978 ssl->in_hslen = 0;
5979 }
5980 /* Case (4): Application data */
5981 else if( ssl->in_offt != NULL )
5982 {
5983 return( 0 );
5984 }
5985 /* Everything else (CCS & Alerts) */
5986 else
5987 {
5988 ssl->in_msglen = 0;
5989 }
5990
Hanno Becker1097b342018-08-15 14:09:41 +01005991 return( 0 );
5992}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005993
Hanno Beckere74d5562018-08-15 14:26:08 +01005994static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5995{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005996 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005997 return( 1 );
5998
5999 return( 0 );
6000}
6001
Hanno Becker5f066e72018-08-16 14:56:31 +01006002#if defined(MBEDTLS_SSL_PROTO_DTLS)
6003
6004static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6005{
6006 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6007 if( hs == NULL )
6008 return;
6009
Hanno Becker01315ea2018-08-21 17:22:17 +01006010 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006011 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006012 hs->buffering.total_bytes_buffered -=
6013 hs->buffering.future_record.len;
6014
6015 mbedtls_free( hs->buffering.future_record.data );
6016 hs->buffering.future_record.data = NULL;
6017 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006018}
6019
6020static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6021{
6022 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6023 unsigned char * rec;
6024 size_t rec_len;
6025 unsigned rec_epoch;
6026
6027 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6028 return( 0 );
6029
6030 if( hs == NULL )
6031 return( 0 );
6032
Hanno Becker5f066e72018-08-16 14:56:31 +01006033 rec = hs->buffering.future_record.data;
6034 rec_len = hs->buffering.future_record.len;
6035 rec_epoch = hs->buffering.future_record.epoch;
6036
6037 if( rec == NULL )
6038 return( 0 );
6039
Hanno Becker4cb782d2018-08-20 11:19:05 +01006040 /* Only consider loading future records if the
6041 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006042 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006043 return( 0 );
6044
Hanno Becker5f066e72018-08-16 14:56:31 +01006045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6046
6047 if( rec_epoch != ssl->in_epoch )
6048 {
6049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6050 goto exit;
6051 }
6052
6053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6054
6055 /* Double-check that the record is not too large */
6056 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6057 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6058 {
6059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6060 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6061 }
6062
6063 memcpy( ssl->in_hdr, rec, rec_len );
6064 ssl->in_left = rec_len;
6065 ssl->next_record_offset = 0;
6066
6067 ssl_free_buffered_record( ssl );
6068
6069exit:
6070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6071 return( 0 );
6072}
6073
Hanno Becker519f15d2019-07-11 12:43:20 +01006074static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6075 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006076{
6077 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006078
6079 /* Don't buffer future records outside handshakes. */
6080 if( hs == NULL )
6081 return( 0 );
6082
6083 /* Only buffer handshake records (we are only interested
6084 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006085 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006086 return( 0 );
6087
6088 /* Don't buffer more than one future epoch record. */
6089 if( hs->buffering.future_record.data != NULL )
6090 return( 0 );
6091
Hanno Becker01315ea2018-08-21 17:22:17 +01006092 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006093 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006094 hs->buffering.total_bytes_buffered ) )
6095 {
6096 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 +01006097 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006098 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006099 return( 0 );
6100 }
6101
Hanno Becker5f066e72018-08-16 14:56:31 +01006102 /* Buffer record */
6103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6104 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006105 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006106
6107 /* ssl_parse_record_header() only considers records
6108 * of the next epoch as candidates for buffering. */
6109 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006110 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006111
6112 hs->buffering.future_record.data =
6113 mbedtls_calloc( 1, hs->buffering.future_record.len );
6114 if( hs->buffering.future_record.data == NULL )
6115 {
6116 /* If we run out of RAM trying to buffer a
6117 * record from the next epoch, just ignore. */
6118 return( 0 );
6119 }
6120
Hanno Becker519f15d2019-07-11 12:43:20 +01006121 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006122
Hanno Becker519f15d2019-07-11 12:43:20 +01006123 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006124 return( 0 );
6125}
6126
6127#endif /* MBEDTLS_SSL_PROTO_DTLS */
6128
Hanno Beckere74d5562018-08-15 14:26:08 +01006129static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006130{
6131 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006132 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006133
Hanno Becker5f066e72018-08-16 14:56:31 +01006134#if defined(MBEDTLS_SSL_PROTO_DTLS)
6135 /* We might have buffered a future record; if so,
6136 * and if the epoch matches now, load it.
6137 * On success, this call will set ssl->in_left to
6138 * the length of the buffered record, so that
6139 * the calls to ssl_fetch_input() below will
6140 * essentially be no-ops. */
6141 ret = ssl_load_buffered_record( ssl );
6142 if( ret != 0 )
6143 return( ret );
6144#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006145
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006146 /* Ensure that we have enough space available for the default form
6147 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6148 * with no space for CIDs counted in). */
6149 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6150 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006153 return( ret );
6154 }
6155
Hanno Beckere5e7e782019-07-11 12:29:35 +01006156 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6157 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006160 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006161 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006162 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6163 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006164 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006165 if( ret != 0 )
6166 return( ret );
6167
6168 /* Fall through to handling of unexpected records */
6169 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6170 }
6171
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006172 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6173 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006174#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006175 /* Reset in pointers to default state for TLS/DTLS records,
6176 * assuming no CID and no offset between record content and
6177 * record plaintext. */
6178 ssl_update_in_pointers( ssl );
6179
Hanno Becker7ae20e02019-07-12 08:33:49 +01006180 /* Setup internal message pointers from record structure. */
6181 ssl->in_msgtype = rec.type;
6182#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6183 ssl->in_len = ssl->in_cid + rec.cid_len;
6184#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6185 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6186 ssl->in_msglen = rec.data_len;
6187
Hanno Becker2fddd372019-07-10 14:37:41 +01006188 ret = ssl_check_client_reconnect( ssl );
6189 if( ret != 0 )
6190 return( ret );
6191#endif
6192
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006193 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006194 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006195
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6197 "(header)" ) );
6198 }
6199 else
6200 {
6201 /* Skip invalid record and the rest of the datagram */
6202 ssl->next_record_offset = 0;
6203 ssl->in_left = 0;
6204
6205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6206 "(header)" ) );
6207 }
6208
6209 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006210 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006211 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006212 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006213#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006214 {
6215 return( ret );
6216 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006217 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006220 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006221 {
Hanno Beckera8814792019-07-10 15:01:45 +01006222 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006223 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006224 if( ssl->next_record_offset < ssl->in_left )
6225 {
6226 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6227 }
6228 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006229 else
6230#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006231 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006232 /*
6233 * Fetch record contents from underlying transport.
6234 */
Hanno Beckera3175662019-07-11 12:50:29 +01006235 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006236 if( ret != 0 )
6237 {
6238 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6239 return( ret );
6240 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006241
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006242 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006243 }
6244
6245 /*
6246 * Decrypt record contents.
6247 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006248
Hanno Beckerfdf66042019-07-11 13:07:45 +01006249 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006252 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006253 {
6254 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006255 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006256 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006257 /* Except when waiting for Finished as a bad mac here
6258 * probably means something went wrong in the handshake
6259 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6260 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6261 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6262 {
6263#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6264 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6265 {
6266 mbedtls_ssl_send_alert_message( ssl,
6267 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6268 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6269 }
6270#endif
6271 return( ret );
6272 }
6273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006274#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006275 if( ssl->conf->badmac_limit != 0 &&
6276 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6279 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006280 }
6281#endif
6282
Hanno Becker4a810fb2017-05-24 16:27:30 +01006283 /* As above, invalid records cause
6284 * dismissal of the whole datagram. */
6285
6286 ssl->next_record_offset = 0;
6287 ssl->in_left = 0;
6288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006290 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006291 }
6292
6293 return( ret );
6294 }
6295 else
6296#endif
6297 {
6298 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6300 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302 mbedtls_ssl_send_alert_message( ssl,
6303 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6304 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006305 }
6306#endif
6307 return( ret );
6308 }
6309 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006310
Hanno Becker44d89b22019-07-12 09:40:44 +01006311
6312 /* Reset in pointers to default state for TLS/DTLS records,
6313 * assuming no CID and no offset between record content and
6314 * record plaintext. */
6315 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006316#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6317 ssl->in_len = ssl->in_cid + rec.cid_len;
6318#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6319 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006320
Hanno Becker8685c822019-07-12 09:37:30 +01006321 /* The record content type may change during decryption,
6322 * so re-read it. */
6323 ssl->in_msgtype = rec.type;
6324 /* Also update the input buffer, because unfortunately
6325 * the server-side ssl_parse_client_hello() reparses the
6326 * record header when receiving a ClientHello initiating
6327 * a renegotiation. */
6328 ssl->in_hdr[0] = rec.type;
6329 ssl->in_msg = rec.buf + rec.data_offset;
6330 ssl->in_msglen = rec.data_len;
6331 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6332 ssl->in_len[1] = (unsigned char)( rec.data_len );
6333
Simon Butcher99000142016-10-13 17:21:01 +01006334 return( 0 );
6335}
6336
6337int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6338{
6339 int ret;
6340
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006341 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006342 * Handle particular types of records
6343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006345 {
Simon Butcher99000142016-10-13 17:21:01 +01006346 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6347 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006348 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006350 }
6351
Hanno Beckere678eaa2018-08-21 14:57:46 +01006352 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006353 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006354 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006355 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6357 ssl->in_msglen ) );
6358 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006359 }
6360
Hanno Beckere678eaa2018-08-21 14:57:46 +01006361 if( ssl->in_msg[0] != 1 )
6362 {
6363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6364 ssl->in_msg[0] ) );
6365 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6366 }
6367
6368#if defined(MBEDTLS_SSL_PROTO_DTLS)
6369 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6370 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6371 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6372 {
6373 if( ssl->handshake == NULL )
6374 {
6375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6376 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6377 }
6378
6379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6380 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6381 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006382#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006383 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006386 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006387 if( ssl->in_msglen != 2 )
6388 {
6389 /* Note: Standard allows for more than one 2 byte alert
6390 to be packed in a single message, but Mbed TLS doesn't
6391 currently support this. */
6392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6393 ssl->in_msglen ) );
6394 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6395 }
6396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006398 ssl->in_msg[0], ssl->in_msg[1] ) );
6399
6400 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006401 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006403 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006406 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006407 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006408 }
6409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006410 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6411 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6414 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006415 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006416
6417#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6418 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6419 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6420 {
Hanno Becker90333da2017-10-10 11:27:13 +01006421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006422 /* Will be handled when trying to parse ServerHello */
6423 return( 0 );
6424 }
6425#endif
6426
6427#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6428 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6429 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6430 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6431 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6432 {
6433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6434 /* Will be handled in mbedtls_ssl_parse_certificate() */
6435 return( 0 );
6436 }
6437#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6438
6439 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006440 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006441 }
6442
Hanno Beckerc76c6192017-06-06 10:03:17 +01006443#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006444 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006445 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006446 /* Drop unexpected ApplicationData records,
6447 * except at the beginning of renegotiations */
6448 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6449 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6450#if defined(MBEDTLS_SSL_RENEGOTIATION)
6451 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6452 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006453#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006454 )
6455 {
6456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6457 return( MBEDTLS_ERR_SSL_NON_FATAL );
6458 }
6459
6460 if( ssl->handshake != NULL &&
6461 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6462 {
6463 ssl_handshake_wrapup_free_hs_transform( ssl );
6464 }
6465 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006466#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006467
Paul Bakker5121ce52009-01-03 21:22:43 +00006468 return( 0 );
6469}
6470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006472{
6473 int ret;
6474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6476 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6477 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006478 {
6479 return( ret );
6480 }
6481
6482 return( 0 );
6483}
6484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006486 unsigned char level,
6487 unsigned char message )
6488{
6489 int ret;
6490
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006491 if( ssl == NULL || ssl->conf == NULL )
6492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006498 ssl->out_msglen = 2;
6499 ssl->out_msg[0] = level;
6500 ssl->out_msg[1] = message;
6501
Hanno Becker67bc7c32018-08-06 11:33:50 +01006502 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006505 return( ret );
6506 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006508
6509 return( 0 );
6510}
6511
Hanno Beckerb9d44792019-02-08 07:19:04 +00006512#if defined(MBEDTLS_X509_CRT_PARSE_C)
6513static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6514{
6515#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6516 if( session->peer_cert != NULL )
6517 {
6518 mbedtls_x509_crt_free( session->peer_cert );
6519 mbedtls_free( session->peer_cert );
6520 session->peer_cert = NULL;
6521 }
6522#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6523 if( session->peer_cert_digest != NULL )
6524 {
6525 /* Zeroization is not necessary. */
6526 mbedtls_free( session->peer_cert_digest );
6527 session->peer_cert_digest = NULL;
6528 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6529 session->peer_cert_digest_len = 0;
6530 }
6531#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6532}
6533#endif /* MBEDTLS_X509_CRT_PARSE_C */
6534
Paul Bakker5121ce52009-01-03 21:22:43 +00006535/*
6536 * Handshake functions
6537 */
Hanno Becker21489932019-02-05 13:20:55 +00006538#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006539/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006541{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006542 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6543 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006546
Hanno Becker7177a882019-02-05 13:36:46 +00006547 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006550 ssl->state++;
6551 return( 0 );
6552 }
6553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006556}
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006559{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006560 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6561 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006564
Hanno Becker7177a882019-02-05 13:36:46 +00006565 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006568 ssl->state++;
6569 return( 0 );
6570 }
6571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6573 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006574}
Gilles Peskinef9828522017-05-03 12:28:43 +02006575
Hanno Becker21489932019-02-05 13:20:55 +00006576#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006577/* Some certificate support -> implement write and parse */
6578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006580{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006581 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006582 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006584 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6585 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006588
Hanno Becker7177a882019-02-05 13:36:46 +00006589 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006592 ssl->state++;
6593 return( 0 );
6594 }
6595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006597 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006598 {
6599 if( ssl->client_auth == 0 )
6600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006602 ssl->state++;
6603 return( 0 );
6604 }
6605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006607 /*
6608 * If using SSLv3 and got no cert, send an Alert message
6609 * (otherwise an empty Certificate message will be sent).
6610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6612 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006613 {
6614 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6616 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6617 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006620 goto write_msg;
6621 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006623 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624#endif /* MBEDTLS_SSL_CLI_C */
6625#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006626 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6631 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006632 }
6633 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006634#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006637
6638 /*
6639 * 0 . 0 handshake type
6640 * 1 . 3 handshake length
6641 * 4 . 6 length of all certs
6642 * 7 . 9 length of cert. 1
6643 * 10 . n-1 peer certificate
6644 * n . n+2 length of cert. 2
6645 * n+3 . ... upper level cert, etc.
6646 */
6647 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006649
Paul Bakker29087132010-03-21 21:03:34 +00006650 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006651 {
6652 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006653 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006656 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006658 }
6659
6660 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6661 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6662 ssl->out_msg[i + 2] = (unsigned char)( n );
6663
6664 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6665 i += n; crt = crt->next;
6666 }
6667
6668 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6669 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6670 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6671
6672 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6674 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006675
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006676#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006677write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006678#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006679
6680 ssl->state++;
6681
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006682 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006683 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006685 return( ret );
6686 }
6687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006689
Paul Bakkered27a042013-04-18 22:46:23 +02006690 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006691}
6692
Hanno Becker84879e32019-01-31 07:44:03 +00006693#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006694
6695#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006696static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6697 unsigned char *crt_buf,
6698 size_t crt_buf_len )
6699{
6700 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6701
6702 if( peer_crt == NULL )
6703 return( -1 );
6704
6705 if( peer_crt->raw.len != crt_buf_len )
6706 return( -1 );
6707
Hanno Becker46f34d02019-02-08 14:00:04 +00006708 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006709}
Hanno Becker177475a2019-02-05 17:02:46 +00006710#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6711static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6712 unsigned char *crt_buf,
6713 size_t crt_buf_len )
6714{
6715 int ret;
6716 unsigned char const * const peer_cert_digest =
6717 ssl->session->peer_cert_digest;
6718 mbedtls_md_type_t const peer_cert_digest_type =
6719 ssl->session->peer_cert_digest_type;
6720 mbedtls_md_info_t const * const digest_info =
6721 mbedtls_md_info_from_type( peer_cert_digest_type );
6722 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6723 size_t digest_len;
6724
6725 if( peer_cert_digest == NULL || digest_info == NULL )
6726 return( -1 );
6727
6728 digest_len = mbedtls_md_get_size( digest_info );
6729 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6730 return( -1 );
6731
6732 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6733 if( ret != 0 )
6734 return( -1 );
6735
6736 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6737}
6738#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006739#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006740
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006741/*
6742 * Once the certificate message is read, parse it into a cert chain and
6743 * perform basic checks, but leave actual verification to the caller
6744 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006745static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6746 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006747{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006748 int ret;
6749#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6750 int crt_cnt=0;
6751#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006752 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006753 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006758 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6759 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006761 }
6762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6764 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006767 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6768 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006770 }
6771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006773
Paul Bakker5121ce52009-01-03 21:22:43 +00006774 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006776 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006777 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006778
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006779 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006783 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6784 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006786 }
6787
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006788 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6789 i += 3;
6790
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006791 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006792 while( i < ssl->in_hslen )
6793 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006794 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006795 if ( i + 3 > ssl->in_hslen ) {
6796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006797 mbedtls_ssl_send_alert_message( ssl,
6798 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6799 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006800 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6801 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006802 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6803 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006804 if( ssl->in_msg[i] != 0 )
6805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006807 mbedtls_ssl_send_alert_message( ssl,
6808 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6809 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006811 }
6812
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006813 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006814 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6815 | (unsigned int) ssl->in_msg[i + 2];
6816 i += 3;
6817
6818 if( n < 128 || i + n > ssl->in_hslen )
6819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006821 mbedtls_ssl_send_alert_message( ssl,
6822 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6823 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006825 }
6826
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006827 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006828#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6829 if( crt_cnt++ == 0 &&
6830 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6831 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006832 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006833 /* During client-side renegotiation, check that the server's
6834 * end-CRTs hasn't changed compared to the initial handshake,
6835 * mitigating the triple handshake attack. On success, reuse
6836 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006837 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6838 if( ssl_check_peer_crt_unchanged( ssl,
6839 &ssl->in_msg[i],
6840 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006841 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6843 mbedtls_ssl_send_alert_message( ssl,
6844 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6845 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6846 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006847 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006848
6849 /* Now we can safely free the original chain. */
6850 ssl_clear_peer_cert( ssl->session );
6851 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006852#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6853
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006854 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006855#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006856 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006857#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006858 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006859 * it in-place from the input buffer instead of making a copy. */
6860 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6861#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006862 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006863 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006864 case 0: /*ok*/
6865 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6866 /* Ignore certificate with an unknown algorithm: maybe a
6867 prior certificate was already trusted. */
6868 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006869
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006870 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6871 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6872 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006873
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006874 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6875 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6876 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006877
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006878 default:
6879 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6880 crt_parse_der_failed:
6881 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6882 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6883 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006884 }
6885
6886 i += n;
6887 }
6888
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006889 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006890 return( 0 );
6891}
6892
Hanno Becker4a55f632019-02-05 12:49:06 +00006893#if defined(MBEDTLS_SSL_SRV_C)
6894static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6895{
6896 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6897 return( -1 );
6898
6899#if defined(MBEDTLS_SSL_PROTO_SSL3)
6900 /*
6901 * Check if the client sent an empty certificate
6902 */
6903 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6904 {
6905 if( ssl->in_msglen == 2 &&
6906 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6907 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6908 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6909 {
6910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6911 return( 0 );
6912 }
6913
6914 return( -1 );
6915 }
6916#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6917
6918#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6919 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6920 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6921 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6922 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6923 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6924 {
6925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6926 return( 0 );
6927 }
6928
6929 return( -1 );
6930#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6931 MBEDTLS_SSL_PROTO_TLS1_2 */
6932}
6933#endif /* MBEDTLS_SSL_SRV_C */
6934
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006935/* Check if a certificate message is expected.
6936 * Return either
6937 * - SSL_CERTIFICATE_EXPECTED, or
6938 * - SSL_CERTIFICATE_SKIP
6939 * indicating whether a Certificate message is expected or not.
6940 */
6941#define SSL_CERTIFICATE_EXPECTED 0
6942#define SSL_CERTIFICATE_SKIP 1
6943static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6944 int authmode )
6945{
6946 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006947 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006948
6949 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6950 return( SSL_CERTIFICATE_SKIP );
6951
6952#if defined(MBEDTLS_SSL_SRV_C)
6953 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6954 {
6955 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6956 return( SSL_CERTIFICATE_SKIP );
6957
6958 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6959 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006960 ssl->session_negotiate->verify_result =
6961 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6962 return( SSL_CERTIFICATE_SKIP );
6963 }
6964 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006965#else
6966 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006967#endif /* MBEDTLS_SSL_SRV_C */
6968
6969 return( SSL_CERTIFICATE_EXPECTED );
6970}
6971
Hanno Becker68636192019-02-05 14:36:34 +00006972static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6973 int authmode,
6974 mbedtls_x509_crt *chain,
6975 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006976{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006977 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006978 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006979 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006980 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006981
Hanno Becker8927c832019-04-03 12:52:50 +01006982 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6983 void *p_vrfy;
6984
Hanno Becker68636192019-02-05 14:36:34 +00006985 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6986 return( 0 );
6987
Hanno Becker8927c832019-04-03 12:52:50 +01006988 if( ssl->f_vrfy != NULL )
6989 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006991 f_vrfy = ssl->f_vrfy;
6992 p_vrfy = ssl->p_vrfy;
6993 }
6994 else
6995 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006997 f_vrfy = ssl->conf->f_vrfy;
6998 p_vrfy = ssl->conf->p_vrfy;
6999 }
7000
Hanno Becker68636192019-02-05 14:36:34 +00007001 /*
7002 * Main check: verify certificate
7003 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007004#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7005 if( ssl->conf->f_ca_cb != NULL )
7006 {
7007 ((void) rs_ctx);
7008 have_ca_chain = 1;
7009
7010 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03007011 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007012 chain,
7013 ssl->conf->f_ca_cb,
7014 ssl->conf->p_ca_cb,
7015 ssl->conf->cert_profile,
7016 ssl->hostname,
7017 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007018 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007019 }
7020 else
7021#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7022 {
7023 mbedtls_x509_crt *ca_chain;
7024 mbedtls_x509_crl *ca_crl;
7025
7026#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7027 if( ssl->handshake->sni_ca_chain != NULL )
7028 {
7029 ca_chain = ssl->handshake->sni_ca_chain;
7030 ca_crl = ssl->handshake->sni_ca_crl;
7031 }
7032 else
7033#endif
7034 {
7035 ca_chain = ssl->conf->ca_chain;
7036 ca_crl = ssl->conf->ca_crl;
7037 }
7038
7039 if( ca_chain != NULL )
7040 have_ca_chain = 1;
7041
7042 ret = mbedtls_x509_crt_verify_restartable(
7043 chain,
7044 ca_chain, ca_crl,
7045 ssl->conf->cert_profile,
7046 ssl->hostname,
7047 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007048 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007049 }
Hanno Becker68636192019-02-05 14:36:34 +00007050
7051 if( ret != 0 )
7052 {
7053 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7054 }
7055
7056#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7057 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7058 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7059#endif
7060
7061 /*
7062 * Secondary checks: always done, but change 'ret' only if it was 0
7063 */
7064
7065#if defined(MBEDTLS_ECP_C)
7066 {
7067 const mbedtls_pk_context *pk = &chain->pk;
7068
7069 /* If certificate uses an EC key, make sure the curve is OK */
7070 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7071 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7072 {
7073 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7074
7075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7076 if( ret == 0 )
7077 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7078 }
7079 }
7080#endif /* MBEDTLS_ECP_C */
7081
7082 if( mbedtls_ssl_check_cert_usage( chain,
7083 ciphersuite_info,
7084 ! ssl->conf->endpoint,
7085 &ssl->session_negotiate->verify_result ) != 0 )
7086 {
7087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7088 if( ret == 0 )
7089 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7090 }
7091
7092 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7093 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7094 * with details encoded in the verification flags. All other kinds
7095 * of error codes, including those from the user provided f_vrfy
7096 * functions, are treated as fatal and lead to a failure of
7097 * ssl_parse_certificate even if verification was optional. */
7098 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7099 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7100 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7101 {
7102 ret = 0;
7103 }
7104
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007105 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007106 {
7107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7108 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7109 }
7110
7111 if( ret != 0 )
7112 {
7113 uint8_t alert;
7114
7115 /* The certificate may have been rejected for several reasons.
7116 Pick one and send the corresponding alert. Which alert to send
7117 may be a subject of debate in some cases. */
7118 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7119 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7120 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7121 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7122 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7123 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7124 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7125 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7126 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7127 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7128 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7129 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7130 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7131 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7132 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7133 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7134 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7135 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7136 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7137 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7138 else
7139 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7140 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7141 alert );
7142 }
7143
7144#if defined(MBEDTLS_DEBUG_C)
7145 if( ssl->session_negotiate->verify_result != 0 )
7146 {
7147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7148 ssl->session_negotiate->verify_result ) );
7149 }
7150 else
7151 {
7152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7153 }
7154#endif /* MBEDTLS_DEBUG_C */
7155
7156 return( ret );
7157}
7158
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007159#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7160static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7161 unsigned char *start, size_t len )
7162{
7163 int ret;
7164 /* Remember digest of the peer's end-CRT. */
7165 ssl->session_negotiate->peer_cert_digest =
7166 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7167 if( ssl->session_negotiate->peer_cert_digest == NULL )
7168 {
7169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7170 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7171 mbedtls_ssl_send_alert_message( ssl,
7172 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7173 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7174
7175 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7176 }
7177
7178 ret = mbedtls_md( mbedtls_md_info_from_type(
7179 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7180 start, len,
7181 ssl->session_negotiate->peer_cert_digest );
7182
7183 ssl->session_negotiate->peer_cert_digest_type =
7184 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7185 ssl->session_negotiate->peer_cert_digest_len =
7186 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7187
7188 return( ret );
7189}
7190
7191static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7192 unsigned char *start, size_t len )
7193{
7194 unsigned char *end = start + len;
7195 int ret;
7196
7197 /* Make a copy of the peer's raw public key. */
7198 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7199 ret = mbedtls_pk_parse_subpubkey( &start, end,
7200 &ssl->handshake->peer_pubkey );
7201 if( ret != 0 )
7202 {
7203 /* We should have parsed the public key before. */
7204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7205 }
7206
7207 return( 0 );
7208}
7209#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7210
Hanno Becker68636192019-02-05 14:36:34 +00007211int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7212{
7213 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007214 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007215#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7216 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7217 ? ssl->handshake->sni_authmode
7218 : ssl->conf->authmode;
7219#else
7220 const int authmode = ssl->conf->authmode;
7221#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007222 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007223 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007224
7225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7226
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007227 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7228 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007229 {
7230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007231 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007232 }
7233
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007234#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7235 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007236 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007237 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007238 chain = ssl->handshake->ecrs_peer_cert;
7239 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007240 goto crt_verify;
7241 }
7242#endif
7243
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007244 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007245 {
7246 /* mbedtls_ssl_read_record may have sent an alert already. We
7247 let it decide whether to alert. */
7248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007249 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007250 }
7251
Hanno Becker4a55f632019-02-05 12:49:06 +00007252#if defined(MBEDTLS_SSL_SRV_C)
7253 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7254 {
7255 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007256
7257 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007258 ret = 0;
7259 else
7260 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007261
Hanno Becker6bdfab22019-02-05 13:11:17 +00007262 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007263 }
7264#endif /* MBEDTLS_SSL_SRV_C */
7265
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007266 /* Clear existing peer CRT structure in case we tried to
7267 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007268 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007269
7270 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7271 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007272 {
7273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7274 sizeof( mbedtls_x509_crt ) ) );
7275 mbedtls_ssl_send_alert_message( ssl,
7276 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7277 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007278
Hanno Becker3dad3112019-02-05 17:19:52 +00007279 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7280 goto exit;
7281 }
7282 mbedtls_x509_crt_init( chain );
7283
7284 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007285 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007286 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007287
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007288#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7289 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007290 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007291
7292crt_verify:
7293 if( ssl->handshake->ecrs_enabled)
7294 rs_ctx = &ssl->handshake->ecrs_ctx;
7295#endif
7296
Hanno Becker68636192019-02-05 14:36:34 +00007297 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007298 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007299 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007300 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007301
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007302#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007303 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007304 unsigned char *crt_start, *pk_start;
7305 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007306
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007307 /* We parse the CRT chain without copying, so
7308 * these pointers point into the input buffer,
7309 * and are hence still valid after freeing the
7310 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007311
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007312 crt_start = chain->raw.p;
7313 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007314
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007315 pk_start = chain->pk_raw.p;
7316 pk_len = chain->pk_raw.len;
7317
7318 /* Free the CRT structures before computing
7319 * digest and copying the peer's public key. */
7320 mbedtls_x509_crt_free( chain );
7321 mbedtls_free( chain );
7322 chain = NULL;
7323
7324 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007325 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007326 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007327
7328 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7329 if( ret != 0 )
7330 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007331 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007332#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7333 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007334 ssl->session_negotiate->peer_cert = chain;
7335 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007336#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007339
Hanno Becker6bdfab22019-02-05 13:11:17 +00007340exit:
7341
Hanno Becker3dad3112019-02-05 17:19:52 +00007342 if( ret == 0 )
7343 ssl->state++;
7344
7345#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7346 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7347 {
7348 ssl->handshake->ecrs_peer_cert = chain;
7349 chain = NULL;
7350 }
7351#endif
7352
7353 if( chain != NULL )
7354 {
7355 mbedtls_x509_crt_free( chain );
7356 mbedtls_free( chain );
7357 }
7358
Paul Bakker5121ce52009-01-03 21:22:43 +00007359 return( ret );
7360}
Hanno Becker21489932019-02-05 13:20:55 +00007361#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007364{
7365 int ret;
7366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007370 ssl->out_msglen = 1;
7371 ssl->out_msg[0] = 1;
7372
Paul Bakker5121ce52009-01-03 21:22:43 +00007373 ssl->state++;
7374
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007375 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007376 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007378 return( ret );
7379 }
7380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007382
7383 return( 0 );
7384}
7385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007387{
7388 int ret;
7389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007391
Hanno Becker327c93b2018-08-15 13:56:18 +01007392 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007395 return( ret );
7396 }
7397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007401 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7402 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007404 }
7405
Hanno Beckere678eaa2018-08-21 14:57:46 +01007406 /* CCS records are only accepted if they have length 1 and content '1',
7407 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007408
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007409 /*
7410 * Switch to our negotiated transform and session parameters for inbound
7411 * data.
7412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007414 ssl->transform_in = ssl->transform_negotiate;
7415 ssl->session_in = ssl->session_negotiate;
7416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007418 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007421 ssl_dtls_replay_reset( ssl );
7422#endif
7423
7424 /* Increment epoch */
7425 if( ++ssl->in_epoch == 0 )
7426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007428 /* This is highly unlikely to happen for legitimate reasons, so
7429 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007431 }
7432 }
7433 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007435 memset( ssl->in_ctr, 0, 8 );
7436
Hanno Becker79594fd2019-05-08 09:38:41 +01007437 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7440 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007445 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7446 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007448 }
7449 }
7450#endif
7451
Paul Bakker5121ce52009-01-03 21:22:43 +00007452 ssl->state++;
7453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007455
7456 return( 0 );
7457}
7458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7460 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007461{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007462 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7465 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7466 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007467 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007468 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007469#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7471#if defined(MBEDTLS_SHA512_C)
7472 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007473 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7474 else
7475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476#if defined(MBEDTLS_SHA256_C)
7477 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007478 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007479 else
7480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007484 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007485 }
Paul Bakker380da532012-04-18 16:10:25 +00007486}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007489{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7491 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007492 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7493 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007494#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7496#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007497#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007498 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007499 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7500#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007501 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007502#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007503#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007505#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007506 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007507 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007508#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007509 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007510#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007511#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007513}
7514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007516 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7519 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007520 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7521 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007522#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7524#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007525#if defined(MBEDTLS_USE_PSA_CRYPTO)
7526 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7527#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007528 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007529#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007532#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007533 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007534#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007535 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007536#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007537#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007539}
7540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7542 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7543static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007544 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007545{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007546 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7547 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007548}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007549#endif
Paul Bakker380da532012-04-18 16:10:25 +00007550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7552#if defined(MBEDTLS_SHA256_C)
7553static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007554 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007555{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007556#if defined(MBEDTLS_USE_PSA_CRYPTO)
7557 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7558#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007559 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007560#endif
Paul Bakker380da532012-04-18 16:10:25 +00007561}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007562#endif
Paul Bakker380da532012-04-18 16:10:25 +00007563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564#if defined(MBEDTLS_SHA512_C)
7565static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007566 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007567{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007568#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007569 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007570#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007571 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007572#endif
Paul Bakker380da532012-04-18 16:10:25 +00007573}
Paul Bakker769075d2012-11-24 11:26:46 +01007574#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007578static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007580{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007581 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007582 mbedtls_md5_context md5;
7583 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007584
Paul Bakker5121ce52009-01-03 21:22:43 +00007585 unsigned char padbuf[48];
7586 unsigned char md5sum[16];
7587 unsigned char sha1sum[20];
7588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007590 if( !session )
7591 session = ssl->session;
7592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007594
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007595 mbedtls_md5_init( &md5 );
7596 mbedtls_sha1_init( &sha1 );
7597
7598 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7599 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007600
7601 /*
7602 * SSLv3:
7603 * hash =
7604 * MD5( master + pad2 +
7605 * MD5( handshake + sender + master + pad1 ) )
7606 * + SHA1( master + pad2 +
7607 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007608 */
7609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007611 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7612 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007613#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007616 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7617 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007618#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007620 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007621 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007622
Paul Bakker1ef83d62012-04-11 12:09:53 +00007623 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007624
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007625 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7626 mbedtls_md5_update_ret( &md5, session->master, 48 );
7627 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7628 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007629
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007630 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7631 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7632 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7633 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007634
Paul Bakker1ef83d62012-04-11 12:09:53 +00007635 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007636
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007637 mbedtls_md5_starts_ret( &md5 );
7638 mbedtls_md5_update_ret( &md5, session->master, 48 );
7639 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7640 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7641 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007642
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007643 mbedtls_sha1_starts_ret( &sha1 );
7644 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7645 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7646 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7647 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007650
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007651 mbedtls_md5_free( &md5 );
7652 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007653
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007654 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7655 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7656 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007663static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007665{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007666 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007667 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007668 mbedtls_md5_context md5;
7669 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007670 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007673 if( !session )
7674 session = ssl->session;
7675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007677
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007678 mbedtls_md5_init( &md5 );
7679 mbedtls_sha1_init( &sha1 );
7680
7681 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7682 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007683
Paul Bakker1ef83d62012-04-11 12:09:53 +00007684 /*
7685 * TLSv1:
7686 * hash = PRF( master, finished_label,
7687 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7688 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007691 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7692 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007693#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007696 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7697 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007698#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007701 ? "client finished"
7702 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007703
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007704 mbedtls_md5_finish_ret( &md5, padbuf );
7705 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007706
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007707 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007708 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007711
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007712 mbedtls_md5_free( &md5 );
7713 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007714
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007715 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007718}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7722#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007723static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007725{
7726 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007727 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007728 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007729#if defined(MBEDTLS_USE_PSA_CRYPTO)
7730 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007731 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007732 psa_status_t status;
7733#else
7734 mbedtls_sha256_context sha256;
7735#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007738 if( !session )
7739 session = ssl->session;
7740
Andrzej Kurekeb342242019-01-29 09:14:33 -05007741 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7742 ? "client finished"
7743 : "server finished";
7744
7745#if defined(MBEDTLS_USE_PSA_CRYPTO)
7746 sha256_psa = psa_hash_operation_init();
7747
7748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7749
7750 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7751 if( status != PSA_SUCCESS )
7752 {
7753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7754 return;
7755 }
7756
7757 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7758 if( status != PSA_SUCCESS )
7759 {
7760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7761 return;
7762 }
7763 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7764#else
7765
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007766 mbedtls_sha256_init( &sha256 );
7767
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007769
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007770 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007771
7772 /*
7773 * TLSv1.2:
7774 * hash = PRF( master, finished_label,
7775 * Hash( handshake ) )[0.11]
7776 */
7777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778#if !defined(MBEDTLS_SHA256_ALT)
7779 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007780 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007781#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007782
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007783 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007784 mbedtls_sha256_free( &sha256 );
7785#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007786
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007787 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007788 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007791
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007792 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007795}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007799static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007801{
7802 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007803 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007804 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007805#if defined(MBEDTLS_USE_PSA_CRYPTO)
7806 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007807 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007808 psa_status_t status;
7809#else
7810 mbedtls_sha512_context sha512;
7811#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007814 if( !session )
7815 session = ssl->session;
7816
Andrzej Kurekeb342242019-01-29 09:14:33 -05007817 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7818 ? "client finished"
7819 : "server finished";
7820
7821#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007822 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007823
7824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7825
Andrzej Kurek972fba52019-01-30 03:29:12 -05007826 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007827 if( status != PSA_SUCCESS )
7828 {
7829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7830 return;
7831 }
7832
Andrzej Kurek972fba52019-01-30 03:29:12 -05007833 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007834 if( status != PSA_SUCCESS )
7835 {
7836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7837 return;
7838 }
7839 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7840#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007841 mbedtls_sha512_init( &sha512 );
7842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007844
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007845 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007846
7847 /*
7848 * TLSv1.2:
7849 * hash = PRF( master, finished_label,
7850 * Hash( handshake ) )[0.11]
7851 */
7852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007854 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7855 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007856#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007857
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007858 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007859 mbedtls_sha512_free( &sha512 );
7860#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007861
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007862 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007863 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007866
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007867 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007870}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871#endif /* MBEDTLS_SHA512_C */
7872#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007875{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007877
7878 /*
7879 * Free our handshake params
7880 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007881 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007883 ssl->handshake = NULL;
7884
7885 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007886 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007887 */
7888 if( ssl->transform )
7889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 mbedtls_ssl_transform_free( ssl->transform );
7891 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007892 }
7893 ssl->transform = ssl->transform_negotiate;
7894 ssl->transform_negotiate = NULL;
7895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007897}
7898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007900{
7901 int resume = ssl->handshake->resume;
7902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905#if defined(MBEDTLS_SSL_RENEGOTIATION)
7906 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007909 ssl->renego_records_seen = 0;
7910 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007911#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007912
7913 /*
7914 * Free the previous session and switch in the current one
7915 */
Paul Bakker0a597072012-09-25 21:55:46 +00007916 if( ssl->session )
7917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007919 /* RFC 7366 3.1: keep the EtM state */
7920 ssl->session_negotiate->encrypt_then_mac =
7921 ssl->session->encrypt_then_mac;
7922#endif
7923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924 mbedtls_ssl_session_free( ssl->session );
7925 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007926 }
7927 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007928 ssl->session_negotiate = NULL;
7929
Paul Bakker0a597072012-09-25 21:55:46 +00007930 /*
7931 * Add cache entry
7932 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007933 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007934 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007935 resume == 0 )
7936 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007937 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007939 }
Paul Bakker0a597072012-09-25 21:55:46 +00007940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007942 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007943 ssl->handshake->flight != NULL )
7944 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007945 /* Cancel handshake timer */
7946 ssl_set_timer( ssl, 0 );
7947
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007948 /* Keep last flight around in case we need to resend it:
7949 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007951 }
7952 else
7953#endif
7954 ssl_handshake_wrapup_free_hs_transform( ssl );
7955
Paul Bakker48916f92012-09-16 19:57:18 +00007956 ssl->state++;
7957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007959}
7960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007962{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007963 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007966
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007967 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007968
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007969 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007970
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007971 /*
7972 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7973 * may define some other value. Currently (early 2016), no defined
7974 * ciphersuite does this (and this is unlikely to change as activity has
7975 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007980 ssl->verify_data_len = hash_len;
7981 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007982#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007983
Paul Bakker5121ce52009-01-03 21:22:43 +00007984 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7986 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007987
7988 /*
7989 * In case of session resuming, invert the client and server
7990 * ChangeCipherSpec messages order.
7991 */
Paul Bakker0a597072012-09-25 21:55:46 +00007992 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007995 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007997#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007999 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008001#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008002 }
8003 else
8004 ssl->state++;
8005
Paul Bakker48916f92012-09-16 19:57:18 +00008006 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008007 * Switch to our negotiated transform and session parameters for outbound
8008 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008013 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008014 {
8015 unsigned char i;
8016
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008017 /* Remember current epoch settings for resending */
8018 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01008019 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008020
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008021 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008022 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008023
8024 /* Increment epoch */
8025 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008026 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008027 break;
8028
8029 /* The loop goes to its end iff the counter is wrapping */
8030 if( i == 0 )
8031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8033 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008034 }
8035 }
8036 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008038 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008039
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008040 ssl->transform_out = ssl->transform_negotiate;
8041 ssl->session_out = ssl->session_negotiate;
8042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8044 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008046 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8049 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008050 }
8051 }
8052#endif
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_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008057#endif
8058
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008059 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008060 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008061 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008062 return( ret );
8063 }
8064
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008065#if defined(MBEDTLS_SSL_PROTO_DTLS)
8066 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8067 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8068 {
8069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8070 return( ret );
8071 }
8072#endif
8073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008075
8076 return( 0 );
8077}
8078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008080#define SSL_MAX_HASH_LEN 36
8081#else
8082#define SSL_MAX_HASH_LEN 12
8083#endif
8084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008086{
Paul Bakker23986e52011-04-24 08:57:21 +00008087 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008088 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008089 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008092
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008093 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008094
Hanno Becker327c93b2018-08-15 13:56:18 +01008095 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008098 return( ret );
8099 }
8100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008101 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008104 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8105 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008107 }
8108
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008109 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110#if defined(MBEDTLS_SSL_PROTO_SSL3)
8111 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008112 hash_len = 36;
8113 else
8114#endif
8115 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8118 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008121 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8122 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008124 }
8125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008127 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008130 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8131 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008133 }
8134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008136 ssl->verify_data_len = hash_len;
8137 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008138#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008139
Paul Bakker0a597072012-09-25 21:55:46 +00008140 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008143 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008147 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008149#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008150 }
8151 else
8152 ssl->state++;
8153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008155 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008157#endif
8158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008160
8161 return( 0 );
8162}
8163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008168#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8169 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8170 mbedtls_md5_init( &handshake->fin_md5 );
8171 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008172 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8173 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008174#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8176#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008177#if defined(MBEDTLS_USE_PSA_CRYPTO)
8178 handshake->fin_sha256_psa = psa_hash_operation_init();
8179 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8180#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008182 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008183#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008184#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008186#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008187 handshake->fin_sha384_psa = psa_hash_operation_init();
8188 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008189#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008191 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008192#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008193#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008195
8196 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008197
8198#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8199 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8200 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8201#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203#if defined(MBEDTLS_DHM_C)
8204 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008205#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206#if defined(MBEDTLS_ECDH_C)
8207 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008208#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008209#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008210 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008211#if defined(MBEDTLS_SSL_CLI_C)
8212 handshake->ecjpake_cache = NULL;
8213 handshake->ecjpake_cache_len = 0;
8214#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008215#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008216
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008217#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008218 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008219#endif
8220
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008221#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8222 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8223#endif
Hanno Becker75173122019-02-06 16:18:31 +00008224
8225#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8226 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8227 mbedtls_pk_init( &handshake->peer_pubkey );
8228#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008229}
8230
Hanno Beckera18d1322018-01-03 14:27:32 +00008231void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008232{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8236 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008237
Hanno Beckerd56ed242018-01-03 15:32:51 +00008238#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239 mbedtls_md_init( &transform->md_ctx_enc );
8240 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008241#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008242}
8243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008245{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008247}
8248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008250{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008251 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008252 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008254 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008256 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008257 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008258
8259 /*
8260 * Either the pointers are now NULL or cleared properly and can be freed.
8261 * Now allocate missing structures.
8262 */
8263 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008264 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008265 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008266 }
Paul Bakker48916f92012-09-16 19:57:18 +00008267
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008268 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008269 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008270 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008271 }
Paul Bakker48916f92012-09-16 19:57:18 +00008272
Paul Bakker82788fb2014-10-20 13:59:19 +02008273 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008274 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008275 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008276 }
Paul Bakker48916f92012-09-16 19:57:18 +00008277
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008278 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008279 if( ssl->handshake == NULL ||
8280 ssl->transform_negotiate == NULL ||
8281 ssl->session_negotiate == NULL )
8282 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285 mbedtls_free( ssl->handshake );
8286 mbedtls_free( ssl->transform_negotiate );
8287 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008288
8289 ssl->handshake = NULL;
8290 ssl->transform_negotiate = NULL;
8291 ssl->session_negotiate = NULL;
8292
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008293 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008294 }
8295
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008296 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008297 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008298 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008299 ssl_handshake_params_init( ssl->handshake );
8300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008302 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8303 {
8304 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008305
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008306 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8307 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8308 else
8309 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008310
8311 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008312 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008313#endif
8314
Paul Bakker48916f92012-09-16 19:57:18 +00008315 return( 0 );
8316}
8317
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008318#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008319/* Dummy cookie callbacks for defaults */
8320static int ssl_cookie_write_dummy( void *ctx,
8321 unsigned char **p, unsigned char *end,
8322 const unsigned char *cli_id, size_t cli_id_len )
8323{
8324 ((void) ctx);
8325 ((void) p);
8326 ((void) end);
8327 ((void) cli_id);
8328 ((void) cli_id_len);
8329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008331}
8332
8333static int ssl_cookie_check_dummy( void *ctx,
8334 const unsigned char *cookie, size_t cookie_len,
8335 const unsigned char *cli_id, size_t cli_id_len )
8336{
8337 ((void) ctx);
8338 ((void) cookie);
8339 ((void) cookie_len);
8340 ((void) cli_id);
8341 ((void) cli_id_len);
8342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008344}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008345#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008346
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008347/* Once ssl->out_hdr as the address of the beginning of the
8348 * next outgoing record is set, deduce the other pointers.
8349 *
8350 * Note: For TLS, we save the implicit record sequence number
8351 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8352 * and the caller has to make sure there's space for this.
8353 */
8354
8355static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8356 mbedtls_ssl_transform *transform )
8357{
8358#if defined(MBEDTLS_SSL_PROTO_DTLS)
8359 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8360 {
8361 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008362#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008363 ssl->out_cid = ssl->out_ctr + 8;
8364 ssl->out_len = ssl->out_cid;
8365 if( transform != NULL )
8366 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008367#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008368 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008369#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008370 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008371 }
8372 else
8373#endif
8374 {
8375 ssl->out_ctr = ssl->out_hdr - 8;
8376 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008377#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008378 ssl->out_cid = ssl->out_len;
8379#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008380 ssl->out_iv = ssl->out_hdr + 5;
8381 }
8382
8383 /* Adjust out_msg to make space for explicit IV, if used. */
8384 if( transform != NULL &&
8385 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8386 {
8387 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8388 }
8389 else
8390 ssl->out_msg = ssl->out_iv;
8391}
8392
8393/* Once ssl->in_hdr as the address of the beginning of the
8394 * next incoming record is set, deduce the other pointers.
8395 *
8396 * Note: For TLS, we save the implicit record sequence number
8397 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8398 * and the caller has to make sure there's space for this.
8399 */
8400
Hanno Becker79594fd2019-05-08 09:38:41 +01008401static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008402{
Hanno Becker79594fd2019-05-08 09:38:41 +01008403 /* This function sets the pointers to match the case
8404 * of unprotected TLS/DTLS records, with both ssl->in_iv
8405 * and ssl->in_msg pointing to the beginning of the record
8406 * content.
8407 *
8408 * When decrypting a protected record, ssl->in_msg
8409 * will be shifted to point to the beginning of the
8410 * record plaintext.
8411 */
8412
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008413#if defined(MBEDTLS_SSL_PROTO_DTLS)
8414 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8415 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008416 /* This sets the header pointers to match records
8417 * without CID. When we receive a record containing
8418 * a CID, the fields are shifted accordingly in
8419 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008420 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008421#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008422 ssl->in_cid = ssl->in_ctr + 8;
8423 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008424#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008425 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008426#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008427 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008428 }
8429 else
8430#endif
8431 {
8432 ssl->in_ctr = ssl->in_hdr - 8;
8433 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008434#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008435 ssl->in_cid = ssl->in_len;
8436#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008437 ssl->in_iv = ssl->in_hdr + 5;
8438 }
8439
Hanno Becker79594fd2019-05-08 09:38:41 +01008440 /* This will be adjusted at record decryption time. */
8441 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008442}
8443
Paul Bakker5121ce52009-01-03 21:22:43 +00008444/*
8445 * Initialize an SSL context
8446 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008447void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8448{
8449 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8450}
8451
8452/*
8453 * Setup an SSL context
8454 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008455
8456static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8457{
8458 /* Set the incoming and outgoing record pointers. */
8459#if defined(MBEDTLS_SSL_PROTO_DTLS)
8460 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8461 {
8462 ssl->out_hdr = ssl->out_buf;
8463 ssl->in_hdr = ssl->in_buf;
8464 }
8465 else
8466#endif /* MBEDTLS_SSL_PROTO_DTLS */
8467 {
8468 ssl->out_hdr = ssl->out_buf + 8;
8469 ssl->in_hdr = ssl->in_buf + 8;
8470 }
8471
8472 /* Derive other internal pointers. */
8473 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008474 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008475}
8476
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008477int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008478 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008479{
Paul Bakker48916f92012-09-16 19:57:18 +00008480 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008481
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008482 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008483
8484 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008485 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008486 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008487
8488 /* Set to NULL in case of an error condition */
8489 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008490
Angus Grattond8213d02016-05-25 20:56:48 +10008491 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8492 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008493 {
Angus Grattond8213d02016-05-25 20:56:48 +10008494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008495 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008496 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008497 }
8498
8499 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8500 if( ssl->out_buf == NULL )
8501 {
8502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008503 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008504 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008505 }
8506
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008507 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008508
Paul Bakker48916f92012-09-16 19:57:18 +00008509 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008510 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008511
8512 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008513
8514error:
8515 mbedtls_free( ssl->in_buf );
8516 mbedtls_free( ssl->out_buf );
8517
8518 ssl->conf = NULL;
8519
8520 ssl->in_buf = NULL;
8521 ssl->out_buf = NULL;
8522
8523 ssl->in_hdr = NULL;
8524 ssl->in_ctr = NULL;
8525 ssl->in_len = NULL;
8526 ssl->in_iv = NULL;
8527 ssl->in_msg = NULL;
8528
8529 ssl->out_hdr = NULL;
8530 ssl->out_ctr = NULL;
8531 ssl->out_len = NULL;
8532 ssl->out_iv = NULL;
8533 ssl->out_msg = NULL;
8534
k-stachowiak9f7798e2018-07-31 16:52:32 +02008535 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008536}
8537
8538/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008539 * Reset an initialized and used SSL context for re-use while retaining
8540 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008541 *
8542 * If partial is non-zero, keep data in the input buffer and client ID.
8543 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008544 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008545static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008546{
Paul Bakker48916f92012-09-16 19:57:18 +00008547 int ret;
8548
Hanno Becker7e772132018-08-10 12:38:21 +01008549#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8550 !defined(MBEDTLS_SSL_SRV_C)
8551 ((void) partial);
8552#endif
8553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008555
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008556 /* Cancel any possibly running timer */
8557 ssl_set_timer( ssl, 0 );
8558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559#if defined(MBEDTLS_SSL_RENEGOTIATION)
8560 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008561 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008562
8563 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8565 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008568
Paul Bakker7eb013f2011-10-06 12:37:39 +00008569 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008570 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008571
8572 ssl->in_msgtype = 0;
8573 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008574#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008575 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008576 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008577#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008579 ssl_dtls_replay_reset( ssl );
8580#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008581
8582 ssl->in_hslen = 0;
8583 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008584
8585 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008586
8587 ssl->out_msgtype = 0;
8588 ssl->out_msglen = 0;
8589 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8591 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008592 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008593#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008594
Hanno Becker19859472018-08-06 09:40:20 +01008595 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8596
Paul Bakker48916f92012-09-16 19:57:18 +00008597 ssl->transform_in = NULL;
8598 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008599
Hanno Becker78640902018-08-13 16:35:15 +01008600 ssl->session_in = NULL;
8601 ssl->session_out = NULL;
8602
Angus Grattond8213d02016-05-25 20:56:48 +10008603 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008604
8605#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008606 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008607#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8608 {
8609 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008610 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008611 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8614 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8617 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8620 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008621 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008622 }
8623#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008624
Paul Bakker48916f92012-09-16 19:57:18 +00008625 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627 mbedtls_ssl_transform_free( ssl->transform );
8628 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008629 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008630 }
Paul Bakker48916f92012-09-16 19:57:18 +00008631
Paul Bakkerc0463502013-02-14 11:19:38 +01008632 if( ssl->session )
8633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634 mbedtls_ssl_session_free( ssl->session );
8635 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008636 ssl->session = NULL;
8637 }
8638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008640 ssl->alpn_chosen = NULL;
8641#endif
8642
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008643#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008644#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008645 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008646#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008647 {
8648 mbedtls_free( ssl->cli_id );
8649 ssl->cli_id = NULL;
8650 ssl->cli_id_len = 0;
8651 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008652#endif
8653
Paul Bakker48916f92012-09-16 19:57:18 +00008654 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8655 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008656
8657 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008658}
8659
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008660/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008661 * Reset an initialized and used SSL context for re-use while retaining
8662 * all application-set variables, function pointers and data.
8663 */
8664int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8665{
8666 return( ssl_session_reset_int( ssl, 0 ) );
8667}
8668
8669/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008670 * SSL set accessors
8671 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008672void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008673{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008674 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008675}
8676
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008677void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008678{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008679 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008680}
8681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008683void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008684{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008685 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008686}
8687#endif
8688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008690void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008691{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008692 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008693}
8694#endif
8695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008697
Hanno Becker1841b0a2018-08-24 11:13:57 +01008698void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8699 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008700{
8701 ssl->disable_datagram_packing = !allow_packing;
8702}
8703
8704void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8705 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008706{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008707 conf->hs_timeout_min = min;
8708 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008709}
8710#endif
8711
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008712void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008713{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008714 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008715}
8716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008717#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008718void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008719 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008720 void *p_vrfy )
8721{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008722 conf->f_vrfy = f_vrfy;
8723 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008724}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008725#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008726
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008727void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008728 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008729 void *p_rng )
8730{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008731 conf->f_rng = f_rng;
8732 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008733}
8734
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008735void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008736 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008737 void *p_dbg )
8738{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008739 conf->f_dbg = f_dbg;
8740 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008741}
8742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008744 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008745 mbedtls_ssl_send_t *f_send,
8746 mbedtls_ssl_recv_t *f_recv,
8747 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008748{
8749 ssl->p_bio = p_bio;
8750 ssl->f_send = f_send;
8751 ssl->f_recv = f_recv;
8752 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008753}
8754
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008755#if defined(MBEDTLS_SSL_PROTO_DTLS)
8756void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8757{
8758 ssl->mtu = mtu;
8759}
8760#endif
8761
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008762void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008763{
8764 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008765}
8766
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008767void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8768 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008769 mbedtls_ssl_set_timer_t *f_set_timer,
8770 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008771{
8772 ssl->p_timer = p_timer;
8773 ssl->f_set_timer = f_set_timer;
8774 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008775
8776 /* Make sure we start with no timer running */
8777 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008778}
8779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008781void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008782 void *p_cache,
8783 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8784 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008785{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008786 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008787 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008788 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792#if defined(MBEDTLS_SSL_CLI_C)
8793int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008794{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008795 int ret;
8796
8797 if( ssl == NULL ||
8798 session == NULL ||
8799 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008800 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008803 }
8804
Hanno Becker52055ae2019-02-06 14:30:46 +00008805 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8806 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008807 return( ret );
8808
Paul Bakker0a597072012-09-25 21:55:46 +00008809 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008810
8811 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008814
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008815void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008816 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008817{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008818 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8819 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8820 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8821 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008822}
8823
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008824void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008825 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008826 int major, int minor )
8827{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008829 return;
8830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008832 return;
8833
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008834 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008835}
8836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008838void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008839 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008840{
8841 conf->cert_profile = profile;
8842}
8843
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008844/* Append a new keycert entry to a (possibly empty) list */
8845static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8846 mbedtls_x509_crt *cert,
8847 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008848{
niisato8ee24222018-06-25 19:05:48 +09008849 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008850
niisato8ee24222018-06-25 19:05:48 +09008851 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8852 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008853 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008854
niisato8ee24222018-06-25 19:05:48 +09008855 new_cert->cert = cert;
8856 new_cert->key = key;
8857 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008858
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008859 /* Update head is the list was null, else add to the end */
8860 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008861 {
niisato8ee24222018-06-25 19:05:48 +09008862 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008863 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008864 else
8865 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008866 mbedtls_ssl_key_cert *cur = *head;
8867 while( cur->next != NULL )
8868 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008869 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008870 }
8871
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008872 return( 0 );
8873}
8874
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008875int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008876 mbedtls_x509_crt *own_cert,
8877 mbedtls_pk_context *pk_key )
8878{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008879 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008880}
8881
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008882void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008883 mbedtls_x509_crt *ca_chain,
8884 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008885{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008886 conf->ca_chain = ca_chain;
8887 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008888
8889#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8890 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8891 * cannot be used together. */
8892 conf->f_ca_cb = NULL;
8893 conf->p_ca_cb = NULL;
8894#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008895}
Hanno Becker5adaad92019-03-27 16:54:37 +00008896
8897#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8898void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008899 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008900 void *p_ca_cb )
8901{
8902 conf->f_ca_cb = f_ca_cb;
8903 conf->p_ca_cb = p_ca_cb;
8904
8905 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8906 * cannot be used together. */
8907 conf->ca_chain = NULL;
8908 conf->ca_crl = NULL;
8909}
8910#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008912
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008913#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8914int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8915 mbedtls_x509_crt *own_cert,
8916 mbedtls_pk_context *pk_key )
8917{
8918 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8919 own_cert, pk_key ) );
8920}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008921
8922void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8923 mbedtls_x509_crt *ca_chain,
8924 mbedtls_x509_crl *ca_crl )
8925{
8926 ssl->handshake->sni_ca_chain = ca_chain;
8927 ssl->handshake->sni_ca_crl = ca_crl;
8928}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008929
8930void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8931 int authmode )
8932{
8933 ssl->handshake->sni_authmode = authmode;
8934}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008935#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8936
Hanno Becker8927c832019-04-03 12:52:50 +01008937#if defined(MBEDTLS_X509_CRT_PARSE_C)
8938void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8939 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8940 void *p_vrfy )
8941{
8942 ssl->f_vrfy = f_vrfy;
8943 ssl->p_vrfy = p_vrfy;
8944}
8945#endif
8946
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008947#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008948/*
8949 * Set EC J-PAKE password for current handshake
8950 */
8951int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8952 const unsigned char *pw,
8953 size_t pw_len )
8954{
8955 mbedtls_ecjpake_role role;
8956
Janos Follath8eb64132016-06-03 15:40:57 +01008957 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008958 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8959
8960 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8961 role = MBEDTLS_ECJPAKE_SERVER;
8962 else
8963 role = MBEDTLS_ECJPAKE_CLIENT;
8964
8965 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8966 role,
8967 MBEDTLS_MD_SHA256,
8968 MBEDTLS_ECP_DP_SECP256R1,
8969 pw, pw_len ) );
8970}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008971#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008974
8975static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8976{
8977 /* Remove reference to existing PSK, if any. */
8978#if defined(MBEDTLS_USE_PSA_CRYPTO)
8979 if( conf->psk_opaque != 0 )
8980 {
8981 /* The maintenance of the PSK key slot is the
8982 * user's responsibility. */
8983 conf->psk_opaque = 0;
8984 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008985 /* This and the following branch should never
8986 * be taken simultaenously as we maintain the
8987 * invariant that raw and opaque PSKs are never
8988 * configured simultaneously. As a safeguard,
8989 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008990#endif /* MBEDTLS_USE_PSA_CRYPTO */
8991 if( conf->psk != NULL )
8992 {
8993 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8994
8995 mbedtls_free( conf->psk );
8996 conf->psk = NULL;
8997 conf->psk_len = 0;
8998 }
8999
9000 /* Remove reference to PSK identity, if any. */
9001 if( conf->psk_identity != NULL )
9002 {
9003 mbedtls_free( conf->psk_identity );
9004 conf->psk_identity = NULL;
9005 conf->psk_identity_len = 0;
9006 }
9007}
9008
Hanno Becker7390c712018-11-15 13:33:04 +00009009/* This function assumes that PSK identity in the SSL config is unset.
9010 * It checks that the provided identity is well-formed and attempts
9011 * to make a copy of it in the SSL config.
9012 * On failure, the PSK identity in the config remains unset. */
9013static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
9014 unsigned char const *psk_identity,
9015 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009016{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009017 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00009018 if( psk_identity == NULL ||
9019 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009020 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009021 {
9022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9023 }
9024
Hanno Becker7390c712018-11-15 13:33:04 +00009025 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
9026 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009027 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02009028
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009029 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009030 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02009031
9032 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02009033}
9034
Hanno Becker7390c712018-11-15 13:33:04 +00009035int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9036 const unsigned char *psk, size_t psk_len,
9037 const unsigned char *psk_identity, size_t psk_identity_len )
9038{
9039 int ret;
9040 /* Remove opaque/raw PSK + PSK Identity */
9041 ssl_conf_remove_psk( conf );
9042
9043 /* Check and set raw PSK */
9044 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9045 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9046 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9047 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9048 conf->psk_len = psk_len;
9049 memcpy( conf->psk, psk, conf->psk_len );
9050
9051 /* Check and set PSK Identity */
9052 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9053 if( ret != 0 )
9054 ssl_conf_remove_psk( conf );
9055
9056 return( ret );
9057}
9058
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009059static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9060{
9061#if defined(MBEDTLS_USE_PSA_CRYPTO)
9062 if( ssl->handshake->psk_opaque != 0 )
9063 {
9064 ssl->handshake->psk_opaque = 0;
9065 }
9066 else
9067#endif /* MBEDTLS_USE_PSA_CRYPTO */
9068 if( ssl->handshake->psk != NULL )
9069 {
9070 mbedtls_platform_zeroize( ssl->handshake->psk,
9071 ssl->handshake->psk_len );
9072 mbedtls_free( ssl->handshake->psk );
9073 ssl->handshake->psk_len = 0;
9074 }
9075}
9076
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009077int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9078 const unsigned char *psk, size_t psk_len )
9079{
9080 if( psk == NULL || ssl->handshake == NULL )
9081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9082
9083 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9085
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009086 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009087
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009088 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009089 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009090
9091 ssl->handshake->psk_len = psk_len;
9092 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9093
9094 return( 0 );
9095}
9096
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009097#if defined(MBEDTLS_USE_PSA_CRYPTO)
9098int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009099 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009100 const unsigned char *psk_identity,
9101 size_t psk_identity_len )
9102{
Hanno Becker7390c712018-11-15 13:33:04 +00009103 int ret;
9104 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009105 ssl_conf_remove_psk( conf );
9106
Hanno Becker7390c712018-11-15 13:33:04 +00009107 /* Check and set opaque PSK */
9108 if( psk_slot == 0 )
9109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009110 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009111
9112 /* Check and set PSK Identity */
9113 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9114 psk_identity_len );
9115 if( ret != 0 )
9116 ssl_conf_remove_psk( conf );
9117
9118 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009119}
9120
9121int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009122 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009123{
9124 if( psk_slot == 0 || ssl->handshake == NULL )
9125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9126
9127 ssl_remove_psk( ssl );
9128 ssl->handshake->psk_opaque = psk_slot;
9129 return( 0 );
9130}
9131#endif /* MBEDTLS_USE_PSA_CRYPTO */
9132
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009133void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009134 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009135 size_t),
9136 void *p_psk )
9137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009138 conf->f_psk = f_psk;
9139 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009141#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009142
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009143#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009144
9145#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009146int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009147{
9148 int ret;
9149
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009150 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9151 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9152 {
9153 mbedtls_mpi_free( &conf->dhm_P );
9154 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009155 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009157
9158 return( 0 );
9159}
Hanno Becker470a8c42017-10-04 15:28:46 +01009160#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009161
Hanno Beckera90658f2017-10-04 15:29:08 +01009162int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9163 const unsigned char *dhm_P, size_t P_len,
9164 const unsigned char *dhm_G, size_t G_len )
9165{
9166 int ret;
9167
9168 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9169 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9170 {
9171 mbedtls_mpi_free( &conf->dhm_P );
9172 mbedtls_mpi_free( &conf->dhm_G );
9173 return( ret );
9174 }
9175
9176 return( 0 );
9177}
Paul Bakker5121ce52009-01-03 21:22:43 +00009178
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009179int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009180{
9181 int ret;
9182
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009183 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9184 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9185 {
9186 mbedtls_mpi_free( &conf->dhm_P );
9187 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009188 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009189 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009190
9191 return( 0 );
9192}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009193#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009194
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009195#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9196/*
9197 * Set the minimum length for Diffie-Hellman parameters
9198 */
9199void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9200 unsigned int bitlen )
9201{
9202 conf->dhm_min_bitlen = bitlen;
9203}
9204#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9205
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009206#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009207/*
9208 * Set allowed/preferred hashes for handshake signatures
9209 */
9210void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9211 const int *hashes )
9212{
9213 conf->sig_hashes = hashes;
9214}
Hanno Becker947194e2017-04-07 13:25:49 +01009215#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009216
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009217#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009218/*
9219 * Set the allowed elliptic curves
9220 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009221void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009222 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009223{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009224 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009225}
Hanno Becker947194e2017-04-07 13:25:49 +01009226#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009227
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009228#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009229int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009230{
Hanno Becker947194e2017-04-07 13:25:49 +01009231 /* Initialize to suppress unnecessary compiler warning */
9232 size_t hostname_len = 0;
9233
9234 /* Check if new hostname is valid before
9235 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009236 if( hostname != NULL )
9237 {
9238 hostname_len = strlen( hostname );
9239
9240 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9242 }
9243
9244 /* Now it's clear that we will overwrite the old hostname,
9245 * so we can free it safely */
9246
9247 if( ssl->hostname != NULL )
9248 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009249 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009250 mbedtls_free( ssl->hostname );
9251 }
9252
9253 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009254
Paul Bakker5121ce52009-01-03 21:22:43 +00009255 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009256 {
9257 ssl->hostname = NULL;
9258 }
9259 else
9260 {
9261 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009262 if( ssl->hostname == NULL )
9263 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009264
Hanno Becker947194e2017-04-07 13:25:49 +01009265 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009266
Hanno Becker947194e2017-04-07 13:25:49 +01009267 ssl->hostname[hostname_len] = '\0';
9268 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009269
9270 return( 0 );
9271}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009272#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009273
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009274#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009275void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009276 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009277 const unsigned char *, size_t),
9278 void *p_sni )
9279{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009280 conf->f_sni = f_sni;
9281 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009282}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009283#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009286int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009287{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009288 size_t cur_len, tot_len;
9289 const char **p;
9290
9291 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009292 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9293 * MUST NOT be truncated."
9294 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009295 */
9296 tot_len = 0;
9297 for( p = protos; *p != NULL; p++ )
9298 {
9299 cur_len = strlen( *p );
9300 tot_len += cur_len;
9301
9302 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009304 }
9305
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009306 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009307
9308 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009309}
9310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009312{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009313 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009316
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009317void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009318{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009319 conf->max_major_ver = major;
9320 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009321}
9322
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009323void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009324{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009325 conf->min_major_ver = major;
9326 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009327}
9328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009330void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009331{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009332 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009333}
9334#endif
9335
Janos Follath088ce432017-04-10 12:42:31 +01009336#if defined(MBEDTLS_SSL_SRV_C)
9337void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9338 char cert_req_ca_list )
9339{
9340 conf->cert_req_ca_list = cert_req_ca_list;
9341}
9342#endif
9343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009345void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009346{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009347 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009348}
9349#endif
9350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009352void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009353{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009354 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009355}
9356#endif
9357
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009358#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009359void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009360{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009361 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009362}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009363#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009366int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009369 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009372 }
9373
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009374 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009375
9376 return( 0 );
9377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009378#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009381void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009382{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009383 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009388void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009389{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009390 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009391}
9392#endif
9393
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009394void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009395{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009396 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009397}
9398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009400void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009401{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009402 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009403}
9404
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009405void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009406{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009407 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009408}
9409
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009410void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009411 const unsigned char period[8] )
9412{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009413 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009418#if defined(MBEDTLS_SSL_CLI_C)
9419void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009420{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009421 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009422}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009423#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009424
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009425#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009426void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9427 mbedtls_ssl_ticket_write_t *f_ticket_write,
9428 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9429 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009430{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009431 conf->f_ticket_write = f_ticket_write;
9432 conf->f_ticket_parse = f_ticket_parse;
9433 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009434}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009435#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009436#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009437
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009438#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9439void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9440 mbedtls_ssl_export_keys_t *f_export_keys,
9441 void *p_export_keys )
9442{
9443 conf->f_export_keys = f_export_keys;
9444 conf->p_export_keys = p_export_keys;
9445}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009446
9447void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9448 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9449 void *p_export_keys )
9450{
9451 conf->f_export_keys_ext = f_export_keys_ext;
9452 conf->p_export_keys = p_export_keys;
9453}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009454#endif
9455
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009456#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009457void mbedtls_ssl_conf_async_private_cb(
9458 mbedtls_ssl_config *conf,
9459 mbedtls_ssl_async_sign_t *f_async_sign,
9460 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9461 mbedtls_ssl_async_resume_t *f_async_resume,
9462 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009463 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009464{
9465 conf->f_async_sign_start = f_async_sign;
9466 conf->f_async_decrypt_start = f_async_decrypt;
9467 conf->f_async_resume = f_async_resume;
9468 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009469 conf->p_async_config_data = async_config_data;
9470}
9471
Gilles Peskine8f97af72018-04-26 11:46:10 +02009472void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9473{
9474 return( conf->p_async_config_data );
9475}
9476
Gilles Peskine1febfef2018-04-30 11:54:39 +02009477void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009478{
9479 if( ssl->handshake == NULL )
9480 return( NULL );
9481 else
9482 return( ssl->handshake->user_async_ctx );
9483}
9484
Gilles Peskine1febfef2018-04-30 11:54:39 +02009485void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009486 void *ctx )
9487{
9488 if( ssl->handshake != NULL )
9489 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009490}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009491#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009492
Paul Bakker5121ce52009-01-03 21:22:43 +00009493/*
9494 * SSL get accessors
9495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009497{
9498 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9499}
9500
Hanno Becker8b170a02017-10-10 11:51:19 +01009501int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9502{
9503 /*
9504 * Case A: We're currently holding back
9505 * a message for further processing.
9506 */
9507
9508 if( ssl->keep_current_message == 1 )
9509 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009511 return( 1 );
9512 }
9513
9514 /*
9515 * Case B: Further records are pending in the current datagram.
9516 */
9517
9518#if defined(MBEDTLS_SSL_PROTO_DTLS)
9519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9520 ssl->in_left > ssl->next_record_offset )
9521 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009523 return( 1 );
9524 }
9525#endif /* MBEDTLS_SSL_PROTO_DTLS */
9526
9527 /*
9528 * Case C: A handshake message is being processed.
9529 */
9530
Hanno Becker8b170a02017-10-10 11:51:19 +01009531 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9532 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009533 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009534 return( 1 );
9535 }
9536
9537 /*
9538 * Case D: An application data message is being processed
9539 */
9540 if( ssl->in_offt != NULL )
9541 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009543 return( 1 );
9544 }
9545
9546 /*
9547 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009548 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009549 * we implement support for multiple alerts in single records.
9550 */
9551
9552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9553 return( 0 );
9554}
9555
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009556uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009557{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009558 if( ssl->session != NULL )
9559 return( ssl->session->verify_result );
9560
9561 if( ssl->session_negotiate != NULL )
9562 return( ssl->session_negotiate->verify_result );
9563
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009564 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009565}
9566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009568{
Paul Bakker926c8e42013-03-06 10:23:34 +01009569 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009570 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009573}
9574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009576{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009578 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009579 {
9580 switch( ssl->minor_ver )
9581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009583 return( "DTLSv1.0" );
9584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009586 return( "DTLSv1.2" );
9587
9588 default:
9589 return( "unknown (DTLS)" );
9590 }
9591 }
9592#endif
9593
Paul Bakker43ca69c2011-01-15 17:35:19 +00009594 switch( ssl->minor_ver )
9595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009597 return( "SSLv3.0" );
9598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009599 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009600 return( "TLSv1.0" );
9601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009603 return( "TLSv1.1" );
9604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009606 return( "TLSv1.2" );
9607
Paul Bakker43ca69c2011-01-15 17:35:19 +00009608 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009609 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009610 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009611}
9612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009614{
Hanno Becker3136ede2018-08-17 15:28:19 +01009615 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009617 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009618
Hanno Becker5903de42019-05-03 14:46:38 +01009619 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9620
Hanno Becker78640902018-08-13 16:35:15 +01009621 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009622 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624#if defined(MBEDTLS_ZLIB_SUPPORT)
9625 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9626 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009627#endif
9628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 case MBEDTLS_MODE_GCM:
9632 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009633 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009635 transform_expansion = transform->minlen;
9636 break;
9637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009639
9640 block_size = mbedtls_cipher_get_block_size(
9641 &transform->cipher_ctx_enc );
9642
Hanno Becker3136ede2018-08-17 15:28:19 +01009643 /* Expansion due to the addition of the MAC. */
9644 transform_expansion += transform->maclen;
9645
9646 /* Expansion due to the addition of CBC padding;
9647 * Theoretically up to 256 bytes, but we never use
9648 * more than the block size of the underlying cipher. */
9649 transform_expansion += block_size;
9650
9651 /* For TLS 1.1 or higher, an explicit IV is added
9652 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009653#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9654 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009655 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009656#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009657
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009658 break;
9659
9660 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009663 }
9664
Hanno Beckera0e20d02019-05-15 14:03:01 +01009665#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009666 if( transform->out_cid_len != 0 )
9667 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009668#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009669
Hanno Becker5903de42019-05-03 14:46:38 +01009670 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009671}
9672
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009673#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9674size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9675{
9676 size_t max_len;
9677
9678 /*
9679 * Assume mfl_code is correct since it was checked when set
9680 */
Angus Grattond8213d02016-05-25 20:56:48 +10009681 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009682
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009683 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009684 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009685 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009686 {
Angus Grattond8213d02016-05-25 20:56:48 +10009687 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009688 }
9689
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009690 /* During a handshake, use the value being negotiated */
9691 if( ssl->session_negotiate != NULL &&
9692 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9693 {
9694 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9695 }
9696
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009697 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009698}
9699#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9700
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009701#if defined(MBEDTLS_SSL_PROTO_DTLS)
9702static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9703{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009704 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9705 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9706 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9707 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9708 return ( 0 );
9709
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009710 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9711 return( ssl->mtu );
9712
9713 if( ssl->mtu == 0 )
9714 return( ssl->handshake->mtu );
9715
9716 return( ssl->mtu < ssl->handshake->mtu ?
9717 ssl->mtu : ssl->handshake->mtu );
9718}
9719#endif /* MBEDTLS_SSL_PROTO_DTLS */
9720
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009721int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9722{
9723 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9724
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009725#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9726 !defined(MBEDTLS_SSL_PROTO_DTLS)
9727 (void) ssl;
9728#endif
9729
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009730#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9731 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9732
9733 if( max_len > mfl )
9734 max_len = mfl;
9735#endif
9736
9737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009738 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009739 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009740 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009741 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9742 const size_t overhead = (size_t) ret;
9743
9744 if( ret < 0 )
9745 return( ret );
9746
9747 if( mtu <= overhead )
9748 {
9749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9750 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9751 }
9752
9753 if( max_len > mtu - overhead )
9754 max_len = mtu - overhead;
9755 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009756#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009757
Hanno Becker0defedb2018-08-10 12:35:02 +01009758#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9759 !defined(MBEDTLS_SSL_PROTO_DTLS)
9760 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009761#endif
9762
9763 return( (int) max_len );
9764}
9765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766#if defined(MBEDTLS_X509_CRT_PARSE_C)
9767const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009768{
9769 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009770 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009771
Hanno Beckere6824572019-02-07 13:18:46 +00009772#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009773 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009774#else
9775 return( NULL );
9776#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009777}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009781int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9782 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009783{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009784 if( ssl == NULL ||
9785 dst == NULL ||
9786 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009787 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009790 }
9791
Hanno Becker52055ae2019-02-06 14:30:46 +00009792 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009793}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009795
Paul Bakker5121ce52009-01-03 21:22:43 +00009796/*
Paul Bakker1961b702013-01-25 14:49:24 +01009797 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009800{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009802
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009803 if( ssl == NULL || ssl->conf == NULL )
9804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009807 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009809#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009811 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009813#endif
9814
Paul Bakker1961b702013-01-25 14:49:24 +01009815 return( ret );
9816}
9817
9818/*
9819 * Perform the SSL handshake
9820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009822{
9823 int ret = 0;
9824
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009825 if( ssl == NULL || ssl->conf == NULL )
9826 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009833
9834 if( ret != 0 )
9835 break;
9836 }
9837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009839
9840 return( ret );
9841}
9842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843#if defined(MBEDTLS_SSL_RENEGOTIATION)
9844#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009845/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009846 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009849{
9850 int ret;
9851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009853
9854 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9856 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009857
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009858 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009859 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009861 return( ret );
9862 }
9863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009865
9866 return( 0 );
9867}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009869
9870/*
9871 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872 * - any side: calling mbedtls_ssl_renegotiate(),
9873 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9874 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009875 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009876 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009877 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009880{
9881 int ret;
9882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009884
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009885 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9886 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009887
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009888 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9889 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009891 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009893 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009894 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009895 ssl->handshake->out_msg_seq = 1;
9896 else
9897 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009898 }
9899#endif
9900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009901 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9902 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009904 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009907 return( ret );
9908 }
9909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009911
9912 return( 0 );
9913}
9914
9915/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009916 * Renegotiate current connection on client,
9917 * or request renegotiation on server
9918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009921 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009922
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009923 if( ssl == NULL || ssl->conf == NULL )
9924 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009926#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009927 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009928 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009933 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009934
9935 /* Did we already try/start sending HelloRequest? */
9936 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009938
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009939 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009940 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009941#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009944 /*
9945 * On client, either start the renegotiation process or,
9946 * if already in progress, continue the handshake
9947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009952
9953 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009956 return( ret );
9957 }
9958 }
9959 else
9960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009964 return( ret );
9965 }
9966 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009968
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009969 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009970}
9971
9972/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009973 * Check record counters and renegotiate if they're above the limit.
9974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009976{
Andres AG2196c7f2016-12-15 17:01:16 +00009977 size_t ep_len = ssl_ep_len( ssl );
9978 int in_ctr_cmp;
9979 int out_ctr_cmp;
9980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9982 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009983 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009984 {
9985 return( 0 );
9986 }
9987
Andres AG2196c7f2016-12-15 17:01:16 +00009988 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9989 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009990 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009991 ssl->conf->renego_period + ep_len, 8 - ep_len );
9992
9993 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009994 {
9995 return( 0 );
9996 }
9997
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009999 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010000}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010002
10003/*
10004 * Receive application data decrypted from the SSL layer
10005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010007{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010008 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010009 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010010
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010011 if( ssl == NULL || ssl->conf == NULL )
10012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010016#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010017 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010019 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010020 return( ret );
10021
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010022 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010024 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010025 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010026 return( ret );
10027 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010028 }
10029#endif
10030
Hanno Becker4a810fb2017-05-24 16:27:30 +010010031 /*
10032 * Check if renegotiation is necessary and/or handshake is
10033 * in process. If yes, perform/continue, and fall through
10034 * if an unexpected packet is received while the client
10035 * is waiting for the ServerHello.
10036 *
10037 * (There is no equivalent to the last condition on
10038 * the server-side as it is not treated as within
10039 * a handshake while waiting for the ClientHello
10040 * after a renegotiation request.)
10041 */
10042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010044 ret = ssl_check_ctr_renegotiate( ssl );
10045 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10046 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010049 return( ret );
10050 }
10051#endif
10052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010053 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010055 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010056 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10057 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010059 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010060 return( ret );
10061 }
10062 }
10063
Hanno Beckere41158b2017-10-23 13:30:32 +010010064 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010065 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010066 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010067 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010068 if( ssl->f_get_timer != NULL &&
10069 ssl->f_get_timer( ssl->p_timer ) == -1 )
10070 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010071 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010072 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010073
Hanno Becker327c93b2018-08-15 13:56:18 +010010074 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010075 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010076 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10077 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010078
Hanno Becker4a810fb2017-05-24 16:27:30 +010010079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10080 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010081 }
10082
10083 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010085 {
10086 /*
10087 * OpenSSL sends empty messages to randomize the IV
10088 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010089 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010091 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010092 return( 0 );
10093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010095 return( ret );
10096 }
10097 }
10098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010102
Hanno Becker4a810fb2017-05-24 16:27:30 +010010103 /*
10104 * - For client-side, expect SERVER_HELLO_REQUEST.
10105 * - For server-side, expect CLIENT_HELLO.
10106 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10107 */
10108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010110 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010112 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010115
10116 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010117#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010118 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010119 {
10120 continue;
10121 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010123 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010124 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010125#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010126
Hanno Becker4a810fb2017-05-24 16:27:30 +010010127#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010128 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010132
10133 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010134#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010135 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010136 {
10137 continue;
10138 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010140 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010141 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010142#endif /* MBEDTLS_SSL_SRV_C */
10143
Hanno Becker21df7f92017-10-17 11:03:26 +010010144#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010145 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010146 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10147 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10148 ssl->conf->allow_legacy_renegotiation ==
10149 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10150 {
10151 /*
10152 * Accept renegotiation request
10153 */
Paul Bakker48916f92012-09-16 19:57:18 +000010154
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010155 /* DTLS clients need to know renego is server-initiated */
10156#if defined(MBEDTLS_SSL_PROTO_DTLS)
10157 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10158 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10159 {
10160 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10161 }
10162#endif
10163 ret = ssl_start_renegotiation( ssl );
10164 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10165 ret != 0 )
10166 {
10167 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10168 return( ret );
10169 }
10170 }
10171 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010172#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010173 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010174 /*
10175 * Refuse renegotiation
10176 */
10177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180#if defined(MBEDTLS_SSL_PROTO_SSL3)
10181 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010182 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010183 /* SSLv3 does not have a "no_renegotiation" warning, so
10184 we send a fatal alert and abort the connection. */
10185 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10186 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10187 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010188 }
10189 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010190#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10191#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10192 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10193 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10196 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10197 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010198 {
10199 return( ret );
10200 }
Paul Bakker48916f92012-09-16 19:57:18 +000010201 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010202 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10204 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10207 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010208 }
Paul Bakker48916f92012-09-16 19:57:18 +000010209 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010210
Hanno Becker90333da2017-10-10 11:27:13 +010010211 /* At this point, we don't know whether the renegotiation has been
10212 * completed or not. The cases to consider are the following:
10213 * 1) The renegotiation is complete. In this case, no new record
10214 * has been read yet.
10215 * 2) The renegotiation is incomplete because the client received
10216 * an application data record while awaiting the ServerHello.
10217 * 3) The renegotiation is incomplete because the client received
10218 * a non-handshake, non-application data message while awaiting
10219 * the ServerHello.
10220 * In each of these case, looping will be the proper action:
10221 * - For 1), the next iteration will read a new record and check
10222 * if it's application data.
10223 * - For 2), the loop condition isn't satisfied as application data
10224 * is present, hence continue is the same as break
10225 * - For 3), the loop condition is satisfied and read_record
10226 * will re-deliver the message that was held back by the client
10227 * when expecting the ServerHello.
10228 */
10229 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010230 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010231#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010233 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010234 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010235 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010236 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010239 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010241 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010242 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010243 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010246 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10247 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010250 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010251 }
10252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010253 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10256 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010257 }
10258
10259 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010260
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010261 /* We're going to return something now, cancel timer,
10262 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010263 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010264 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010265
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010267 /* If we requested renego but received AppData, resend HelloRequest.
10268 * Do it now, after setting in_offt, to avoid taking this branch
10269 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010270#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010271 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010272 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010273 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010274 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010276 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010277 return( ret );
10278 }
10279 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010281#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010282 }
10283
10284 n = ( len < ssl->in_msglen )
10285 ? len : ssl->in_msglen;
10286
10287 memcpy( buf, ssl->in_offt, n );
10288 ssl->in_msglen -= n;
10289
10290 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010291 {
10292 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010293 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010294 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010295 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010296 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010297 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010298 /* more data available */
10299 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010300 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010303
Paul Bakker23986e52011-04-24 08:57:21 +000010304 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010305}
10306
10307/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010308 * Send application data to be encrypted by the SSL layer, taking care of max
10309 * fragment length and buffer size.
10310 *
10311 * According to RFC 5246 Section 6.2.1:
10312 *
10313 * Zero-length fragments of Application data MAY be sent as they are
10314 * potentially useful as a traffic analysis countermeasure.
10315 *
10316 * Therefore, it is possible that the input message length is 0 and the
10317 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010318 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010319static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010320 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010321{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010322 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10323 const size_t max_len = (size_t) ret;
10324
10325 if( ret < 0 )
10326 {
10327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10328 return( ret );
10329 }
10330
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010331 if( len > max_len )
10332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010333#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010334 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010337 "maximum fragment length: %d > %d",
10338 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010340 }
10341 else
10342#endif
10343 len = max_len;
10344 }
Paul Bakker887bd502011-06-08 13:10:54 +000010345
Paul Bakker5121ce52009-01-03 21:22:43 +000010346 if( ssl->out_left != 0 )
10347 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010348 /*
10349 * The user has previously tried to send the data and
10350 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10351 * written. In this case, we expect the high-level write function
10352 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010357 return( ret );
10358 }
10359 }
Paul Bakker887bd502011-06-08 13:10:54 +000010360 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010361 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010362 /*
10363 * The user is trying to send a message the first time, so we need to
10364 * copy the data into the internal buffers and setup the data structure
10365 * to keep track of partial writes
10366 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010367 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010369 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010370
Hanno Becker67bc7c32018-08-06 11:33:50 +010010371 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010374 return( ret );
10375 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010376 }
10377
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010378 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010379}
10380
10381/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010382 * Write application data, doing 1/n-1 splitting if necessary.
10383 *
10384 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010385 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010386 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010388#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010389static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010390 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010391{
10392 int ret;
10393
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010394 if( ssl->conf->cbc_record_splitting ==
10395 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010396 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010397 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10398 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10399 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010400 {
10401 return( ssl_write_real( ssl, buf, len ) );
10402 }
10403
10404 if( ssl->split_done == 0 )
10405 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010406 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010407 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010408 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010409 }
10410
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010411 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10412 return( ret );
10413 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010414
10415 return( ret + 1 );
10416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010417#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010418
10419/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010420 * Write application data (public-facing wrapper)
10421 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010422int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010423{
10424 int ret;
10425
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010427
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010428 if( ssl == NULL || ssl->conf == NULL )
10429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10430
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010431#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010432 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10433 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010434 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010435 return( ret );
10436 }
10437#endif
10438
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010439 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010440 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010441 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010442 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010444 return( ret );
10445 }
10446 }
10447
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010448#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010449 ret = ssl_write_split( ssl, buf, len );
10450#else
10451 ret = ssl_write_real( ssl, buf, len );
10452#endif
10453
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010455
10456 return( ret );
10457}
10458
10459/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010460 * Notify the peer that the connection is being closed
10461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010462int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010463{
10464 int ret;
10465
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010466 if( ssl == NULL || ssl->conf == NULL )
10467 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010470
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010471 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010472 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010474 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10477 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10478 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010481 return( ret );
10482 }
10483 }
10484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010486
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010487 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010488}
10489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010490void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010491{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010492 if( transform == NULL )
10493 return;
10494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010496 deflateEnd( &transform->ctx_deflate );
10497 inflateEnd( &transform->ctx_inflate );
10498#endif
10499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10501 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010502
Hanno Beckerd56ed242018-01-03 15:32:51 +000010503#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010504 mbedtls_md_free( &transform->md_ctx_enc );
10505 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010506#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010507
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010508 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010509}
10510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010511#if defined(MBEDTLS_X509_CRT_PARSE_C)
10512static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010513{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010515
10516 while( cur != NULL )
10517 {
10518 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010519 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010520 cur = next;
10521 }
10522}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010523#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010524
Hanno Becker0271f962018-08-16 13:23:47 +010010525#if defined(MBEDTLS_SSL_PROTO_DTLS)
10526
10527static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10528{
10529 unsigned offset;
10530 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10531
10532 if( hs == NULL )
10533 return;
10534
Hanno Becker283f5ef2018-08-24 09:34:47 +010010535 ssl_free_buffered_record( ssl );
10536
Hanno Becker0271f962018-08-16 13:23:47 +010010537 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010538 ssl_buffering_free_slot( ssl, offset );
10539}
10540
10541static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10542 uint8_t slot )
10543{
10544 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10545 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010546
10547 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10548 return;
10549
Hanno Beckere605b192018-08-21 15:59:07 +010010550 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010551 {
Hanno Beckere605b192018-08-21 15:59:07 +010010552 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010553 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010554 mbedtls_free( hs_buf->data );
10555 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010556 }
10557}
10558
10559#endif /* MBEDTLS_SSL_PROTO_DTLS */
10560
Gilles Peskine9b562d52018-04-25 20:32:43 +020010561void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010562{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010563 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10564
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010565 if( handshake == NULL )
10566 return;
10567
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010568#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10569 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10570 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010571 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010572 handshake->async_in_progress = 0;
10573 }
10574#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10575
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010576#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10577 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10578 mbedtls_md5_free( &handshake->fin_md5 );
10579 mbedtls_sha1_free( &handshake->fin_sha1 );
10580#endif
10581#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10582#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010583#if defined(MBEDTLS_USE_PSA_CRYPTO)
10584 psa_hash_abort( &handshake->fin_sha256_psa );
10585#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010586 mbedtls_sha256_free( &handshake->fin_sha256 );
10587#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010588#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010589#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010590#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010591 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010592#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010593 mbedtls_sha512_free( &handshake->fin_sha512 );
10594#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010595#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010596#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598#if defined(MBEDTLS_DHM_C)
10599 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010600#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010601#if defined(MBEDTLS_ECDH_C)
10602 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010603#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010604#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010605 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010606#if defined(MBEDTLS_SSL_CLI_C)
10607 mbedtls_free( handshake->ecjpake_cache );
10608 handshake->ecjpake_cache = NULL;
10609 handshake->ecjpake_cache_len = 0;
10610#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010611#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010612
Janos Follath4ae5c292016-02-10 11:27:43 +000010613#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10614 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010615 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010616 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010617#endif
10618
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010619#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10620 if( handshake->psk != NULL )
10621 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010622 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010623 mbedtls_free( handshake->psk );
10624 }
10625#endif
10626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10628 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010629 /*
10630 * Free only the linked list wrapper, not the keys themselves
10631 * since the belong to the SNI callback
10632 */
10633 if( handshake->sni_key_cert != NULL )
10634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010636
10637 while( cur != NULL )
10638 {
10639 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010640 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010641 cur = next;
10642 }
10643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010645
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010646#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010647 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010648 if( handshake->ecrs_peer_cert != NULL )
10649 {
10650 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10651 mbedtls_free( handshake->ecrs_peer_cert );
10652 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010653#endif
10654
Hanno Becker75173122019-02-06 16:18:31 +000010655#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10656 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10657 mbedtls_pk_free( &handshake->peer_pubkey );
10658#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010660#if defined(MBEDTLS_SSL_PROTO_DTLS)
10661 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010662 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010663 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010664#endif
10665
Hanno Becker4a63ed42019-01-08 11:39:35 +000010666#if defined(MBEDTLS_ECDH_C) && \
10667 defined(MBEDTLS_USE_PSA_CRYPTO)
10668 psa_destroy_key( handshake->ecdh_psa_privkey );
10669#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10670
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010671 mbedtls_platform_zeroize( handshake,
10672 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010673}
10674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010676{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010677 if( session == NULL )
10678 return;
10679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010680#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010681 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010682#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010683
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010684#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010685 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010686#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010687
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010688 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010689}
10690
Paul Bakker5121ce52009-01-03 21:22:43 +000010691/*
10692 * Free an SSL context
10693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010695{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010696 if( ssl == NULL )
10697 return;
10698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010700
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010701 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010702 {
Angus Grattond8213d02016-05-25 20:56:48 +100010703 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010705 }
10706
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010707 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010708 {
Angus Grattond8213d02016-05-25 20:56:48 +100010709 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010710 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010711 }
10712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010714 if( ssl->compress_buf != NULL )
10715 {
Angus Grattond8213d02016-05-25 20:56:48 +100010716 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010717 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010718 }
10719#endif
10720
Paul Bakker48916f92012-09-16 19:57:18 +000010721 if( ssl->transform )
10722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010723 mbedtls_ssl_transform_free( ssl->transform );
10724 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010725 }
10726
10727 if( ssl->handshake )
10728 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010729 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010730 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10731 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010733 mbedtls_free( ssl->handshake );
10734 mbedtls_free( ssl->transform_negotiate );
10735 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010736 }
10737
Paul Bakkerc0463502013-02-14 11:19:38 +010010738 if( ssl->session )
10739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740 mbedtls_ssl_session_free( ssl->session );
10741 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010742 }
10743
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010744#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010745 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010746 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010747 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010748 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010749 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010750#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10753 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10756 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010757 }
10758#endif
10759
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010760#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010761 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010762#endif
10763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010765
Paul Bakker86f04f42013-02-14 11:20:09 +010010766 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010767 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010768}
10769
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010770/*
10771 * Initialze mbedtls_ssl_config
10772 */
10773void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10774{
10775 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10776}
10777
Simon Butcherc97b6972015-12-27 23:48:17 +000010778#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010779static int ssl_preset_default_hashes[] = {
10780#if defined(MBEDTLS_SHA512_C)
10781 MBEDTLS_MD_SHA512,
10782 MBEDTLS_MD_SHA384,
10783#endif
10784#if defined(MBEDTLS_SHA256_C)
10785 MBEDTLS_MD_SHA256,
10786 MBEDTLS_MD_SHA224,
10787#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010788#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010789 MBEDTLS_MD_SHA1,
10790#endif
10791 MBEDTLS_MD_NONE
10792};
Simon Butcherc97b6972015-12-27 23:48:17 +000010793#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010794
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010795static int ssl_preset_suiteb_ciphersuites[] = {
10796 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10797 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10798 0
10799};
10800
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010801#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010802static int ssl_preset_suiteb_hashes[] = {
10803 MBEDTLS_MD_SHA256,
10804 MBEDTLS_MD_SHA384,
10805 MBEDTLS_MD_NONE
10806};
10807#endif
10808
10809#if defined(MBEDTLS_ECP_C)
10810static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010811#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010812 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010813#endif
10814#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010815 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010816#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010817 MBEDTLS_ECP_DP_NONE
10818};
10819#endif
10820
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010821/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010822 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010823 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010824int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010825 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010826{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010827#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010828 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010829#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010830
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010831 /* Use the functions here so that they are covered in tests,
10832 * but otherwise access member directly for efficiency */
10833 mbedtls_ssl_conf_endpoint( conf, endpoint );
10834 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010835
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010836 /*
10837 * Things that are common to all presets
10838 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010839#if defined(MBEDTLS_SSL_CLI_C)
10840 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10841 {
10842 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10843#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10844 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10845#endif
10846 }
10847#endif
10848
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010849#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010850 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010851#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010852
10853#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10854 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10855#endif
10856
10857#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10858 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10859#endif
10860
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010861#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10862 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10863#endif
10864
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010865#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010866 conf->f_cookie_write = ssl_cookie_write_dummy;
10867 conf->f_cookie_check = ssl_cookie_check_dummy;
10868#endif
10869
10870#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10871 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10872#endif
10873
Janos Follath088ce432017-04-10 12:42:31 +010010874#if defined(MBEDTLS_SSL_SRV_C)
10875 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10876#endif
10877
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010878#if defined(MBEDTLS_SSL_PROTO_DTLS)
10879 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10880 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10881#endif
10882
10883#if defined(MBEDTLS_SSL_RENEGOTIATION)
10884 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010885 memset( conf->renego_period, 0x00, 2 );
10886 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010887#endif
10888
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010889#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10890 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10891 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010892 const unsigned char dhm_p[] =
10893 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10894 const unsigned char dhm_g[] =
10895 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10896
Hanno Beckera90658f2017-10-04 15:29:08 +010010897 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10898 dhm_p, sizeof( dhm_p ),
10899 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010900 {
10901 return( ret );
10902 }
10903 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010904#endif
10905
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010906 /*
10907 * Preset-specific defaults
10908 */
10909 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010910 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010911 /*
10912 * NSA Suite B
10913 */
10914 case MBEDTLS_SSL_PRESET_SUITEB:
10915 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10916 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10917 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10918 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10919
10920 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10921 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10922 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10923 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10924 ssl_preset_suiteb_ciphersuites;
10925
10926#if defined(MBEDTLS_X509_CRT_PARSE_C)
10927 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010928#endif
10929
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010930#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010931 conf->sig_hashes = ssl_preset_suiteb_hashes;
10932#endif
10933
10934#if defined(MBEDTLS_ECP_C)
10935 conf->curve_list = ssl_preset_suiteb_curves;
10936#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010937 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010938
10939 /*
10940 * Default
10941 */
10942 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010943 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10944 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10945 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10946 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10947 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10948 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10949 MBEDTLS_SSL_MIN_MINOR_VERSION :
10950 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010951 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10952 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10953
10954#if defined(MBEDTLS_SSL_PROTO_DTLS)
10955 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10956 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10957#endif
10958
10959 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10960 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10961 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10962 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10963 mbedtls_ssl_list_ciphersuites();
10964
10965#if defined(MBEDTLS_X509_CRT_PARSE_C)
10966 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10967#endif
10968
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010969#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010970 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010971#endif
10972
10973#if defined(MBEDTLS_ECP_C)
10974 conf->curve_list = mbedtls_ecp_grp_id_list();
10975#endif
10976
10977#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10978 conf->dhm_min_bitlen = 1024;
10979#endif
10980 }
10981
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010982 return( 0 );
10983}
10984
10985/*
10986 * Free mbedtls_ssl_config
10987 */
10988void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10989{
10990#if defined(MBEDTLS_DHM_C)
10991 mbedtls_mpi_free( &conf->dhm_P );
10992 mbedtls_mpi_free( &conf->dhm_G );
10993#endif
10994
10995#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10996 if( conf->psk != NULL )
10997 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010998 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010999 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000011000 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011001 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090011002 }
11003
11004 if( conf->psk_identity != NULL )
11005 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011006 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090011007 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000011008 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011009 conf->psk_identity_len = 0;
11010 }
11011#endif
11012
11013#if defined(MBEDTLS_X509_CRT_PARSE_C)
11014 ssl_key_cert_free( conf->key_cert );
11015#endif
11016
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011017 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011018}
11019
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011020#if defined(MBEDTLS_PK_C) && \
11021 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011022/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011023 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011026{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011027#if defined(MBEDTLS_RSA_C)
11028 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
11029 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011031#if defined(MBEDTLS_ECDSA_C)
11032 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
11033 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011035 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011036}
11037
Hanno Becker7e5437a2017-04-28 17:15:26 +010011038unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11039{
11040 switch( type ) {
11041 case MBEDTLS_PK_RSA:
11042 return( MBEDTLS_SSL_SIG_RSA );
11043 case MBEDTLS_PK_ECDSA:
11044 case MBEDTLS_PK_ECKEY:
11045 return( MBEDTLS_SSL_SIG_ECDSA );
11046 default:
11047 return( MBEDTLS_SSL_SIG_ANON );
11048 }
11049}
11050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011051mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011052{
11053 switch( sig )
11054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011055#if defined(MBEDTLS_RSA_C)
11056 case MBEDTLS_SSL_SIG_RSA:
11057 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011058#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011059#if defined(MBEDTLS_ECDSA_C)
11060 case MBEDTLS_SSL_SIG_ECDSA:
11061 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011062#endif
11063 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011064 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011065 }
11066}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011067#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011068
Hanno Becker7e5437a2017-04-28 17:15:26 +010011069#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11070 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11071
11072/* Find an entry in a signature-hash set matching a given hash algorithm. */
11073mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11074 mbedtls_pk_type_t sig_alg )
11075{
11076 switch( sig_alg )
11077 {
11078 case MBEDTLS_PK_RSA:
11079 return( set->rsa );
11080 case MBEDTLS_PK_ECDSA:
11081 return( set->ecdsa );
11082 default:
11083 return( MBEDTLS_MD_NONE );
11084 }
11085}
11086
11087/* Add a signature-hash-pair to a signature-hash set */
11088void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11089 mbedtls_pk_type_t sig_alg,
11090 mbedtls_md_type_t md_alg )
11091{
11092 switch( sig_alg )
11093 {
11094 case MBEDTLS_PK_RSA:
11095 if( set->rsa == MBEDTLS_MD_NONE )
11096 set->rsa = md_alg;
11097 break;
11098
11099 case MBEDTLS_PK_ECDSA:
11100 if( set->ecdsa == MBEDTLS_MD_NONE )
11101 set->ecdsa = md_alg;
11102 break;
11103
11104 default:
11105 break;
11106 }
11107}
11108
11109/* Allow exactly one hash algorithm for each signature. */
11110void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11111 mbedtls_md_type_t md_alg )
11112{
11113 set->rsa = md_alg;
11114 set->ecdsa = md_alg;
11115}
11116
11117#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11118 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11119
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011120/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011121 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011123mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011124{
11125 switch( hash )
11126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011127#if defined(MBEDTLS_MD5_C)
11128 case MBEDTLS_SSL_HASH_MD5:
11129 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011130#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011131#if defined(MBEDTLS_SHA1_C)
11132 case MBEDTLS_SSL_HASH_SHA1:
11133 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011134#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011135#if defined(MBEDTLS_SHA256_C)
11136 case MBEDTLS_SSL_HASH_SHA224:
11137 return( MBEDTLS_MD_SHA224 );
11138 case MBEDTLS_SSL_HASH_SHA256:
11139 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011140#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011141#if defined(MBEDTLS_SHA512_C)
11142 case MBEDTLS_SSL_HASH_SHA384:
11143 return( MBEDTLS_MD_SHA384 );
11144 case MBEDTLS_SSL_HASH_SHA512:
11145 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011146#endif
11147 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011148 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011149 }
11150}
11151
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011152/*
11153 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11154 */
11155unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11156{
11157 switch( md )
11158 {
11159#if defined(MBEDTLS_MD5_C)
11160 case MBEDTLS_MD_MD5:
11161 return( MBEDTLS_SSL_HASH_MD5 );
11162#endif
11163#if defined(MBEDTLS_SHA1_C)
11164 case MBEDTLS_MD_SHA1:
11165 return( MBEDTLS_SSL_HASH_SHA1 );
11166#endif
11167#if defined(MBEDTLS_SHA256_C)
11168 case MBEDTLS_MD_SHA224:
11169 return( MBEDTLS_SSL_HASH_SHA224 );
11170 case MBEDTLS_MD_SHA256:
11171 return( MBEDTLS_SSL_HASH_SHA256 );
11172#endif
11173#if defined(MBEDTLS_SHA512_C)
11174 case MBEDTLS_MD_SHA384:
11175 return( MBEDTLS_SSL_HASH_SHA384 );
11176 case MBEDTLS_MD_SHA512:
11177 return( MBEDTLS_SSL_HASH_SHA512 );
11178#endif
11179 default:
11180 return( MBEDTLS_SSL_HASH_NONE );
11181 }
11182}
11183
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011184#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011185/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011186 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011187 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011188 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011189int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011190{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011191 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011192
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011193 if( ssl->conf->curve_list == NULL )
11194 return( -1 );
11195
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011196 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011197 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011198 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011199
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011200 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011201}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011202#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011203
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011204#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011205/*
11206 * Check if a hash proposed by the peer is in our list.
11207 * Return 0 if we're willing to use it, -1 otherwise.
11208 */
11209int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11210 mbedtls_md_type_t md )
11211{
11212 const int *cur;
11213
11214 if( ssl->conf->sig_hashes == NULL )
11215 return( -1 );
11216
11217 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11218 if( *cur == (int) md )
11219 return( 0 );
11220
11221 return( -1 );
11222}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011223#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011225#if defined(MBEDTLS_X509_CRT_PARSE_C)
11226int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11227 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011228 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011229 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011230{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011231 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011232#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011233 int usage = 0;
11234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011235#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011236 const char *ext_oid;
11237 size_t ext_len;
11238#endif
11239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011240#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11241 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011242 ((void) cert);
11243 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011244 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011245#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011247#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11248 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011249 {
11250 /* Server part of the key exchange */
11251 switch( ciphersuite->key_exchange )
11252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011253 case MBEDTLS_KEY_EXCHANGE_RSA:
11254 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011255 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011256 break;
11257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011258 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11259 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11260 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11261 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011262 break;
11263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011264 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11265 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011266 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011267 break;
11268
11269 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011270 case MBEDTLS_KEY_EXCHANGE_NONE:
11271 case MBEDTLS_KEY_EXCHANGE_PSK:
11272 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11273 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011274 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011275 usage = 0;
11276 }
11277 }
11278 else
11279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011280 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11281 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011282 }
11283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011284 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011285 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011286 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011287 ret = -1;
11288 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011289#else
11290 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011291#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011293#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11294 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011296 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11297 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011298 }
11299 else
11300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011301 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11302 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011303 }
11304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011305 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011306 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011307 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011308 ret = -1;
11309 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011310#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011311
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011312 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011313}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011314#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011315
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011316/*
11317 * Convert version numbers to/from wire format
11318 * and, for DTLS, to/from TLS equivalent.
11319 *
11320 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011321 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011322 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11323 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011325void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011326 unsigned char ver[2] )
11327{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011328#if defined(MBEDTLS_SSL_PROTO_DTLS)
11329 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011331 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011332 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11333
11334 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11335 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11336 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011337 else
11338#else
11339 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011340#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011341 {
11342 ver[0] = (unsigned char) major;
11343 ver[1] = (unsigned char) minor;
11344 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011345}
11346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011347void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011348 const unsigned char ver[2] )
11349{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011350#if defined(MBEDTLS_SSL_PROTO_DTLS)
11351 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011352 {
11353 *major = 255 - ver[0] + 2;
11354 *minor = 255 - ver[1] + 1;
11355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011356 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011357 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11358 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011359 else
11360#else
11361 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011362#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011363 {
11364 *major = ver[0];
11365 *minor = ver[1];
11366 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011367}
11368
Simon Butcher99000142016-10-13 17:21:01 +010011369int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11370{
11371#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11372 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11373 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11374
11375 switch( md )
11376 {
11377#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11378#if defined(MBEDTLS_MD5_C)
11379 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011380 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011381#endif
11382#if defined(MBEDTLS_SHA1_C)
11383 case MBEDTLS_SSL_HASH_SHA1:
11384 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11385 break;
11386#endif
11387#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11388#if defined(MBEDTLS_SHA512_C)
11389 case MBEDTLS_SSL_HASH_SHA384:
11390 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11391 break;
11392#endif
11393#if defined(MBEDTLS_SHA256_C)
11394 case MBEDTLS_SSL_HASH_SHA256:
11395 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11396 break;
11397#endif
11398 default:
11399 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11400 }
11401
11402 return 0;
11403#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11404 (void) ssl;
11405 (void) md;
11406
11407 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11408#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11409}
11410
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011411#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11412 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11413int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11414 unsigned char *output,
11415 unsigned char *data, size_t data_len )
11416{
11417 int ret = 0;
11418 mbedtls_md5_context mbedtls_md5;
11419 mbedtls_sha1_context mbedtls_sha1;
11420
11421 mbedtls_md5_init( &mbedtls_md5 );
11422 mbedtls_sha1_init( &mbedtls_sha1 );
11423
11424 /*
11425 * digitally-signed struct {
11426 * opaque md5_hash[16];
11427 * opaque sha_hash[20];
11428 * };
11429 *
11430 * md5_hash
11431 * MD5(ClientHello.random + ServerHello.random
11432 * + ServerParams);
11433 * sha_hash
11434 * SHA(ClientHello.random + ServerHello.random
11435 * + ServerParams);
11436 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011437 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011438 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011440 goto exit;
11441 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011442 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011443 ssl->handshake->randbytes, 64 ) ) != 0 )
11444 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011446 goto exit;
11447 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011448 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011449 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011451 goto exit;
11452 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011453 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011454 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011456 goto exit;
11457 }
11458
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011459 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011460 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011462 goto exit;
11463 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011464 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011465 ssl->handshake->randbytes, 64 ) ) != 0 )
11466 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011468 goto exit;
11469 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011470 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011471 data_len ) ) != 0 )
11472 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011474 goto exit;
11475 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011476 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011477 output + 16 ) ) != 0 )
11478 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011480 goto exit;
11481 }
11482
11483exit:
11484 mbedtls_md5_free( &mbedtls_md5 );
11485 mbedtls_sha1_free( &mbedtls_sha1 );
11486
11487 if( ret != 0 )
11488 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11489 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11490
11491 return( ret );
11492
11493}
11494#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11495 MBEDTLS_SSL_PROTO_TLS1_1 */
11496
11497#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11498 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011499
11500#if defined(MBEDTLS_USE_PSA_CRYPTO)
11501int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11502 unsigned char *hash, size_t *hashlen,
11503 unsigned char *data, size_t data_len,
11504 mbedtls_md_type_t md_alg )
11505{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011506 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011507 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011508 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11509
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011511
11512 if( ( status = psa_hash_setup( &hash_operation,
11513 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011514 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011515 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011516 goto exit;
11517 }
11518
Andrzej Kurek814feff2019-01-14 04:35:19 -050011519 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11520 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011521 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011522 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011523 goto exit;
11524 }
11525
Andrzej Kurek814feff2019-01-14 04:35:19 -050011526 if( ( status = psa_hash_update( &hash_operation,
11527 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011528 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011529 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011530 goto exit;
11531 }
11532
Andrzej Kurek814feff2019-01-14 04:35:19 -050011533 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11534 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011535 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011536 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011537 goto exit;
11538 }
11539
11540exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011541 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011542 {
11543 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11544 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011545 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011546 {
11547 case PSA_ERROR_NOT_SUPPORTED:
11548 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011549 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011550 case PSA_ERROR_BUFFER_TOO_SMALL:
11551 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11552 case PSA_ERROR_INSUFFICIENT_MEMORY:
11553 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11554 default:
11555 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11556 }
11557 }
11558 return( 0 );
11559}
11560
11561#else
11562
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011563int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011564 unsigned char *hash, size_t *hashlen,
11565 unsigned char *data, size_t data_len,
11566 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011567{
11568 int ret = 0;
11569 mbedtls_md_context_t ctx;
11570 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011571 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011572
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011574
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011575 mbedtls_md_init( &ctx );
11576
11577 /*
11578 * digitally-signed struct {
11579 * opaque client_random[32];
11580 * opaque server_random[32];
11581 * ServerDHParams params;
11582 * };
11583 */
11584 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11585 {
11586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11587 goto exit;
11588 }
11589 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11590 {
11591 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11592 goto exit;
11593 }
11594 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11595 {
11596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11597 goto exit;
11598 }
11599 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11600 {
11601 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11602 goto exit;
11603 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011604 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011605 {
11606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11607 goto exit;
11608 }
11609
11610exit:
11611 mbedtls_md_free( &ctx );
11612
11613 if( ret != 0 )
11614 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11615 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11616
11617 return( ret );
11618}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011619#endif /* MBEDTLS_USE_PSA_CRYPTO */
11620
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011621#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11622 MBEDTLS_SSL_PROTO_TLS1_2 */
11623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011624#endif /* MBEDTLS_SSL_TLS_C */