blob: 40f72996f8bd36fe7887ede82935aad259ffc333 [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()
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +0200992 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +0200993 * - [in] ciphersuite
994 * - [in] master
995 * - [in] encrypt_then_mac
996 * - [in] trunc_hmac
997 * - [in] compression
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +0200998 * - [in] tls_prf: pointer to PRF to use for key derivation
999 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001000 * - [in] minor_ver: SSL/TLS minor version
1001 * - [in] endpoint: client or server
1002 * - [in] ssl: optionally used for:
1003 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1004 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1005 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001006 */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001007static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001008 int ciphersuite,
1009 const unsigned char master[48],
1010#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1011 int encrypt_then_mac,
1012#endif
1013#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1014 int trunc_hmac,
1015#endif
1016#if defined(MBEDTLS_ZLIB_SUPPORT)
1017 int compression,
1018#endif
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001019 ssl_tls_prf_t tls_prf,
1020 const unsigned char randbytes[64],
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001021 int minor_ver,
1022 unsigned endpoint,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001023 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001024{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001025 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001026#if defined(MBEDTLS_USE_PSA_CRYPTO)
1027 int psa_fallthrough;
1028#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 unsigned char keyblk[256];
1030 unsigned char *key1;
1031 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001032 unsigned char *mac_enc;
1033 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001034 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001035 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001036 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001037 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 const mbedtls_cipher_info_t *cipher_info;
1039 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001040
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001041#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1042 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1043 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001044 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001045 (void) ssl;
1046#endif
1047
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001048 /* Copy info about negotiated version and extensions */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001049#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1050 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001051 transform->encrypt_then_mac = encrypt_then_mac;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001052#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001053 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001054
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001055 /*
1056 * Get various info structures
1057 */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001058 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001059 if( ciphersuite_info == NULL )
1060 {
1061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001062 ciphersuite ) );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1064 }
1065
Hanno Beckere694c3e2017-12-27 21:34:08 +00001066 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001067 if( cipher_info == NULL )
1068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001070 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001072 }
1073
Hanno Beckere694c3e2017-12-27 21:34:08 +00001074 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001075 if( md_info == NULL )
1076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001078 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001080 }
1081
Hanno Beckera0e20d02019-05-15 14:03:01 +01001082#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001083 /* Copy own and peer's CID if the use of the CID
1084 * extension has been negotiated. */
1085 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1086 {
1087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001088
Hanno Becker05154c32019-05-03 15:23:51 +01001089 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001090 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001091 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001092 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001093
1094 transform->out_cid_len = ssl->handshake->peer_cid_len;
1095 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1096 ssl->handshake->peer_cid_len );
1097 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1098 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001099 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001100#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001101
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001103 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001105 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001106 if( ret != 0 )
1107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001109 return( ret );
1110 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001113 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
1114 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001115 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 /*
1119 * Determine the appropriate key, IV and MAC length.
1120 */
Paul Bakker68884e32013-01-07 18:20:04 +01001121
Hanno Becker88aaf652017-12-27 08:17:40 +00001122 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001123
Hanno Becker8031d062018-01-03 15:32:31 +00001124#if defined(MBEDTLS_GCM_C) || \
1125 defined(MBEDTLS_CCM_C) || \
1126 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001128 cipher_info->mode == MBEDTLS_MODE_CCM ||
1129 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001131 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001132
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001133 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001134 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001135 transform->taglen =
1136 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001137
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001138 /* All modes haves 96-bit IVs;
1139 * GCM and CCM has 4 implicit and 8 explicit bytes
1140 * ChachaPoly has all 12 bytes implicit
1141 */
Paul Bakker68884e32013-01-07 18:20:04 +01001142 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001143 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1144 transform->fixed_ivlen = 12;
1145 else
1146 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001147
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001148 /* Minimum length of encrypted record */
1149 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001150 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001151 }
1152 else
Hanno Becker8031d062018-01-03 15:32:31 +00001153#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1154#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1155 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1156 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001157 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001158 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1160 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001163 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001166 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001167 mac_key_len = mbedtls_md_get_size( md_info );
1168 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001171 /*
1172 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1173 * (rfc 6066 page 13 or rfc 2104 section 4),
1174 * so we only need to adjust the length here.
1175 */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001176 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001179
1180#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1181 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001182 * HMAC implementation which also truncates the key
1183 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001184 mac_key_len = transform->maclen;
1185#endif
1186 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001188
1189 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001190 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001191
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001192 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001194 transform->minlen = transform->maclen;
1195 else
Paul Bakker68884e32013-01-07 18:20:04 +01001196 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001197 /*
1198 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001199 * 1. if EtM is in use: one block plus MAC
1200 * otherwise: * first multiple of blocklen greater than maclen
1201 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001204 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001205 {
1206 transform->minlen = transform->maclen
1207 + cipher_info->block_size;
1208 }
1209 else
1210#endif
1211 {
1212 transform->minlen = transform->maclen
1213 + cipher_info->block_size
1214 - transform->maclen % cipher_info->block_size;
1215 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001218 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1219 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001220 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001221 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001224 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1225 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001226 {
1227 transform->minlen += transform->ivlen;
1228 }
1229 else
1230#endif
1231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001233 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1234 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001235 }
Paul Bakker68884e32013-01-07 18:20:04 +01001236 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 }
Hanno Becker8031d062018-01-03 15:32:31 +00001238 else
1239#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1240 {
1241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1243 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
Hanno Becker88aaf652017-12-27 08:17:40 +00001245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1246 (unsigned) keylen,
1247 (unsigned) transform->minlen,
1248 (unsigned) transform->ivlen,
1249 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
1251 /*
1252 * Finally setup the cipher contexts, IVs and MAC secrets.
1253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001255 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001257 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001258 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
Paul Bakker68884e32013-01-07 18:20:04 +01001260 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001261 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001263 /*
1264 * This is not used in TLS v1.1.
1265 */
Paul Bakker48916f92012-09-16 19:57:18 +00001266 iv_copy_len = ( transform->fixed_ivlen ) ?
1267 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001268 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1269 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001270 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 }
1272 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#endif /* MBEDTLS_SSL_CLI_C */
1274#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001275 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001277 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001278 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Hanno Becker81c7b182017-11-09 18:39:33 +00001280 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001281 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001282
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001283 /*
1284 * This is not used in TLS v1.1.
1285 */
Paul Bakker48916f92012-09-16 19:57:18 +00001286 iv_copy_len = ( transform->fixed_ivlen ) ?
1287 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001288 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1289 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001290 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001292 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001296 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1297 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001298 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Hanno Beckerd56ed242018-01-03 15:32:51 +00001300#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001302 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001303 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001304 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001307 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1308 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001309 }
1310
Hanno Becker81c7b182017-11-09 18:39:33 +00001311 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1312 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001313 }
1314 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1316#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1317 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001318 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001319 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001320 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1321 For AEAD-based ciphersuites, there is nothing to do here. */
1322 if( mac_key_len != 0 )
1323 {
1324 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1325 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1326 }
Paul Bakker68884e32013-01-07 18:20:04 +01001327 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001328 else
1329#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001332 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1333 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001334 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001335#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1338 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001339 {
1340 int ret = 0;
1341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001343
Hanno Becker88aaf652017-12-27 08:17:40 +00001344 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001345 transform->iv_enc, transform->iv_dec,
1346 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001347 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001348 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001351 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1352 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001353 }
1354 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001355#else
1356 ((void) mac_dec);
1357 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001359
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001360#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1361 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001362 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001363 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001364 master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001365 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001366 iv_copy_len );
1367 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001368
1369 if( ssl->conf->f_export_keys_ext != NULL )
1370 {
1371 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001372 master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001373 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001374 iv_copy_len,
Manuel Pégourié-Gonnard344460c2019-07-25 13:17:38 +02001375 /* work around bug in exporter type */
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001376 (unsigned char *) randbytes + 32,
1377 (unsigned char *) randbytes,
1378 tls_prf_get_type( tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001379 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001380#endif
1381
Hanno Beckerf704bef2018-11-16 15:21:18 +00001382#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001383
1384 /* Only use PSA-based ciphers for TLS-1.2.
1385 * That's relevant at least for TLS-1.0, where
1386 * we assume that mbedtls_cipher_crypt() updates
1387 * the structure field for the IV, which the PSA-based
1388 * implementation currently doesn't. */
1389#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1390 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001391 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001392 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001393 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001394 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1395 {
1396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001397 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001398 }
1399
1400 if( ret == 0 )
1401 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001403 psa_fallthrough = 0;
1404 }
1405 else
1406 {
1407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1408 psa_fallthrough = 1;
1409 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001410 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001411 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001412 psa_fallthrough = 1;
1413#else
1414 psa_fallthrough = 1;
1415#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001416
Hanno Beckercb1cc802018-11-17 22:27:38 +00001417 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001418#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001419 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001420 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001423 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001424 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001425
Hanno Beckerf704bef2018-11-16 15:21:18 +00001426#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001427 /* Only use PSA-based ciphers for TLS-1.2.
1428 * That's relevant at least for TLS-1.0, where
1429 * we assume that mbedtls_cipher_crypt() updates
1430 * the structure field for the IV, which the PSA-based
1431 * implementation currently doesn't. */
1432#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1433 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001434 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001435 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001436 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001437 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1438 {
1439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001440 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001441 }
1442
1443 if( ret == 0 )
1444 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001446 psa_fallthrough = 0;
1447 }
1448 else
1449 {
1450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1451 psa_fallthrough = 1;
1452 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001453 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001454 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001455 psa_fallthrough = 1;
1456#else
1457 psa_fallthrough = 1;
1458#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001459
Hanno Beckercb1cc802018-11-17 22:27:38 +00001460 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001461#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001462 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001463 cipher_info ) ) != 0 )
1464 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001465 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001466 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001467 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001470 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001474 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001475 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001478 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001482 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001483 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_CIPHER_MODE_CBC)
1486 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1489 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001492 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001493 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1496 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001499 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001500 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001505 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001507 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001510
Paul Bakker48916f92012-09-16 19:57:18 +00001511 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1512 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001513
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001514 if( deflateInit( &transform->ctx_deflate,
1515 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001516 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001519 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1520 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001521 }
1522 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001524
Ron Eldore6992702019-05-07 18:27:13 +03001525end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001526 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Ron Eldore6992702019-05-07 18:27:13 +03001527 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001528}
1529
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001530/*
Manuel Pégourié-Gonnard47e33e12019-05-20 10:10:17 +02001531 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001532 *
1533 * Inputs:
1534 * - SSL/TLS minor version
1535 * - hash associated with the ciphersuite (only used by TLS 1.2)
1536 *
Manuel Pégourié-Gonnard31d3ef12019-05-10 10:25:00 +02001537 * Outputs:
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001538 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1539 */
1540static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1541 int minor_ver,
1542 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001543{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001544#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1545 (void) hash;
1546#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001547
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001548#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001549 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001550 {
1551 handshake->tls_prf = ssl3_prf;
1552 handshake->calc_verify = ssl_calc_verify_ssl;
1553 handshake->calc_finished = ssl_calc_finished_ssl;
1554 }
1555 else
1556#endif
1557#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001558 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001559 {
1560 handshake->tls_prf = tls1_prf;
1561 handshake->calc_verify = ssl_calc_verify_tls;
1562 handshake->calc_finished = ssl_calc_finished_tls;
1563 }
1564 else
1565#endif
1566#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1567#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001568 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1569 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001570 {
1571 handshake->tls_prf = tls_prf_sha384;
1572 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1573 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1574 }
1575 else
1576#endif
1577#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001578 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001579 {
1580 handshake->tls_prf = tls_prf_sha256;
1581 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1582 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1583 }
1584 else
1585#endif
1586#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1587 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1589 }
1590
1591 return( 0 );
1592}
1593
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001594/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001595 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001596 *
1597 * Parameters:
1598 * [in/out] handshake
1599 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001600 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001601 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001602 * [out] master
1603 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1604 * debug: conf->f_dbg, conf->p_dbg
1605 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001606 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001607 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001608static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001609 unsigned char *master,
Manuel Pégourié-Gonnard0d56aaa2019-05-03 09:58:33 +02001610 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001611{
1612 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001613
1614 /* cf. RFC 5246, Section 8.1:
1615 * "The master secret is always exactly 48 bytes in length." */
1616 size_t const master_secret_len = 48;
1617
1618#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1619 unsigned char session_hash[48];
1620#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1621
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001622 /* The label for the KDF used for key expansion.
1623 * This is either "master secret" or "extended master secret"
1624 * depending on whether the Extended Master Secret extension
1625 * is used. */
1626 char const *lbl = "master secret";
1627
1628 /* The salt for the KDF used for key expansion.
1629 * - If the Extended Master Secret extension is not used,
1630 * this is ClientHello.Random + ServerHello.Random
1631 * (see Sect. 8.1 in RFC 5246).
1632 * - If the Extended Master Secret extension is used,
1633 * this is the transcript of the handshake so far.
1634 * (see Sect. 4 in RFC 7627). */
1635 unsigned char const *salt = handshake->randbytes;
1636 size_t salt_len = 64;
1637
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001638#if !defined(MBEDTLS_DEBUG_C) && \
1639 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1640 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1641 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001642 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001643 (void) ssl;
1644#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001645
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001646 if( handshake->resume != 0 )
1647 {
1648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001649 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001650 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001651
1652#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001653 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001654 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001655 lbl = "extended master secret";
1656 salt = session_hash;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001657 handshake->calc_verify( ssl, session_hash, &salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001658
1659 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
1660 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001661#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1662
1663#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001664 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1665 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001666 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001667 ssl_use_opaque_psk( ssl ) == 1 )
1668 {
1669 /* Perform PSK-to-MS expansion in a single step. */
1670 psa_status_t status;
1671 psa_algorithm_t alg;
1672 psa_key_handle_t psk;
1673 psa_key_derivation_operation_t derivation =
1674 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001675 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001676
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001678
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001679 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001680 if( handshake->psk_opaque != 0 )
1681 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001682
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001683 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001684 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001685 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001686 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1687
1688 status = psa_key_derivation( &derivation, psk, alg,
1689 salt, salt_len,
1690 (unsigned char const *) lbl,
1691 (size_t) strlen( lbl ),
1692 master_secret_len );
1693 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001694 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001695 psa_key_derivation_abort( &derivation );
1696 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001697 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001698
1699 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001700 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001701 master_secret_len );
1702 if( status != PSA_SUCCESS )
1703 {
1704 psa_key_derivation_abort( &derivation );
1705 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1706 }
1707
1708 status = psa_key_derivation_abort( &derivation );
1709 if( status != PSA_SUCCESS )
1710 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1711 }
1712 else
1713#endif
1714 {
1715 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1716 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001717 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001718 master_secret_len );
1719 if( ret != 0 )
1720 {
1721 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1722 return( ret );
1723 }
1724
1725 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1726 handshake->premaster,
1727 handshake->pmslen );
1728
1729 mbedtls_platform_zeroize( handshake->premaster,
1730 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001731 }
1732
1733 return( 0 );
1734}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001735
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001736int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1737{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001738 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001739 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1740 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001741
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1743
1744 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001745 ret = ssl_set_handshake_prfs( ssl->handshake,
1746 ssl->minor_ver,
1747 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001748 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001749 {
1750 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001751 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001752 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001753
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001754 /* Compute master secret if needed */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001755 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001756 ssl->session_negotiate->master,
1757 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001758 if( ret != 0 )
1759 {
1760 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1761 return( ret );
1762 }
1763
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001764 /* Swap the client and server random values:
1765 * - MS derivation wanted client+server (RFC 5246 8.1)
1766 * - key derivation wants server+client (RFC 5246 6.3) */
1767 {
1768 unsigned char tmp[64];
1769 memcpy( tmp, ssl->handshake->randbytes, 64 );
1770 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1771 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1772 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1773 }
1774
1775 /* Populate transform structure */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001776 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001777 ssl->session_negotiate->ciphersuite,
1778 ssl->session_negotiate->master,
1779#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1780 ssl->session_negotiate->encrypt_then_mac,
1781#endif
1782#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1783 ssl->session_negotiate->trunc_hmac,
1784#endif
1785#if defined(MBEDTLS_ZLIB_SUPPORT)
1786 ssl->session_negotiate->compression,
1787#endif
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001788 ssl->handshake->tls_prf,
1789 ssl->handshake->randbytes,
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001790 ssl->minor_ver,
1791 ssl->conf->endpoint,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001792 ssl );
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001793 if( ret != 0 )
1794 {
1795 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1796 return( ret );
1797 }
1798
1799 /* We no longer need Server/ClientHello.random values */
1800 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1801 sizeof( ssl->handshake->randbytes ) );
1802
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001803 /* Allocate compression buffer */
1804#if defined(MBEDTLS_ZLIB_SUPPORT)
1805 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1806 ssl->compress_buf == NULL )
1807 {
1808 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1809 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1810 if( ssl->compress_buf == NULL )
1811 {
1812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
1813 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
1814 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1815 }
1816 }
1817#endif
1818
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1820
1821 return( 0 );
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001822}
1823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001825void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1826 unsigned char hash[36],
1827 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001828{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001829 mbedtls_md5_context md5;
1830 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001831 unsigned char pad_1[48];
1832 unsigned char pad_2[48];
1833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001835
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001836 mbedtls_md5_init( &md5 );
1837 mbedtls_sha1_init( &sha1 );
1838
1839 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1840 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001841
Paul Bakker380da532012-04-18 16:10:25 +00001842 memset( pad_1, 0x36, 48 );
1843 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001844
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001845 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1846 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1847 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001849 mbedtls_md5_starts_ret( &md5 );
1850 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1851 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1852 mbedtls_md5_update_ret( &md5, hash, 16 );
1853 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001855 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1856 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1857 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001858
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001859 mbedtls_sha1_starts_ret( &sha1 );
1860 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1861 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1862 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1863 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001864
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001865 *hlen = 36;
1866
1867 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001869
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001870 mbedtls_md5_free( &md5 );
1871 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001872
Paul Bakker380da532012-04-18 16:10:25 +00001873 return;
1874}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001878void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1879 unsigned char hash[36],
1880 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001881{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001882 mbedtls_md5_context md5;
1883 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001886
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001887 mbedtls_md5_init( &md5 );
1888 mbedtls_sha1_init( &sha1 );
1889
1890 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1891 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001892
Andrzej Kurekeb342242019-01-29 09:14:33 -05001893 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001894 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001895
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001896 *hlen = 36;
1897
1898 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001900
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001901 mbedtls_md5_free( &md5 );
1902 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001903
Paul Bakker380da532012-04-18 16:10:25 +00001904 return;
1905}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1909#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001910void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1911 unsigned char hash[32],
1912 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001913{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001914#if defined(MBEDTLS_USE_PSA_CRYPTO)
1915 size_t hash_size;
1916 psa_status_t status;
1917 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1918
1919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1920 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1921 if( status != PSA_SUCCESS )
1922 {
1923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1924 return;
1925 }
1926
1927 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1928 if( status != PSA_SUCCESS )
1929 {
1930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1931 return;
1932 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001933
1934 *hlen = 32;
1935 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1937#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001938 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001939
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001940 mbedtls_sha256_init( &sha256 );
1941
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001943
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001944 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001945 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001946
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001947 *hlen = 32;
1948
1949 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001951
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001952 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001953#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001954 return;
1955}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001959void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1960 unsigned char hash[48],
1961 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001962{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001963#if defined(MBEDTLS_USE_PSA_CRYPTO)
1964 size_t hash_size;
1965 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001966 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001967
1968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001969 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001970 if( status != PSA_SUCCESS )
1971 {
1972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1973 return;
1974 }
1975
Andrzej Kurek972fba52019-01-30 03:29:12 -05001976 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001977 if( status != PSA_SUCCESS )
1978 {
1979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1980 return;
1981 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001982
1983 *hlen = 48;
1984 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1986#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001987 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001988
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001989 mbedtls_sha512_init( &sha512 );
1990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001992
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001993 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001994 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001995
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001996 *hlen = 48;
1997
1998 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002000
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02002001 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05002002#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 return;
2004}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#endif /* MBEDTLS_SHA512_C */
2006#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2009int 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 +02002010{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002011 unsigned char *p = ssl->handshake->premaster;
2012 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002013 const unsigned char *psk = ssl->conf->psk;
2014 size_t psk_len = ssl->conf->psk_len;
2015
2016 /* If the psk callback was called, use its result */
2017 if( ssl->handshake->psk != NULL )
2018 {
2019 psk = ssl->handshake->psk;
2020 psk_len = ssl->handshake->psk_len;
2021 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002022
2023 /*
2024 * PMS = struct {
2025 * opaque other_secret<0..2^16-1>;
2026 * opaque psk<0..2^16-1>;
2027 * };
2028 * with "other_secret" depending on the particular key exchange
2029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2031 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002032 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002033 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002035
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002036 *(p++) = (unsigned char)( psk_len >> 8 );
2037 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002038
2039 if( end < p || (size_t)( end - p ) < psk_len )
2040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2041
2042 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002043 p += psk_len;
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_PSK_ENABLED */
2047#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2048 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002049 {
2050 /*
2051 * other_secret already set by the ClientKeyExchange message,
2052 * and is 48 bytes long
2053 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002054 if( end - p < 2 )
2055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2056
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002057 *p++ = 0;
2058 *p++ = 48;
2059 p += 48;
2060 }
2061 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2063#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2064 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002065 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002066 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002067 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002068
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002069 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002071 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002072 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002075 return( ret );
2076 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002077 *(p++) = (unsigned char)( len >> 8 );
2078 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002079 p += len;
2080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002082 }
2083 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2085#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2086 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002087 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002088 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002089 size_t zlen;
2090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002092 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002093 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002096 return( ret );
2097 }
2098
2099 *(p++) = (unsigned char)( zlen >> 8 );
2100 *(p++) = (unsigned char)( zlen );
2101 p += zlen;
2102
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002103 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2104 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002105 }
2106 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002111 }
2112
2113 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002114 if( end - p < 2 )
2115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002116
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002117 *(p++) = (unsigned char)( psk_len >> 8 );
2118 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002119
2120 if( end < p || (size_t)( end - p ) < psk_len )
2121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2122
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002123 memcpy( p, psk, psk_len );
2124 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002125
2126 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2127
2128 return( 0 );
2129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002133/*
2134 * SSLv3.0 MAC functions
2135 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002136#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002137static void ssl_mac( mbedtls_md_context_t *md_ctx,
2138 const unsigned char *secret,
2139 const unsigned char *buf, size_t len,
2140 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002141 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002142{
2143 unsigned char header[11];
2144 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002145 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2147 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002148
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002149 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002151 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002152 else
Paul Bakker68884e32013-01-07 18:20:04 +01002153 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002154
2155 memcpy( header, ctr, 8 );
2156 header[ 8] = (unsigned char) type;
2157 header[ 9] = (unsigned char)( len >> 8 );
2158 header[10] = (unsigned char)( len );
2159
Paul Bakker68884e32013-01-07 18:20:04 +01002160 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 mbedtls_md_starts( md_ctx );
2162 mbedtls_md_update( md_ctx, secret, md_size );
2163 mbedtls_md_update( md_ctx, padding, padlen );
2164 mbedtls_md_update( md_ctx, header, 11 );
2165 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002166 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002167
Paul Bakker68884e32013-01-07 18:20:04 +01002168 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 mbedtls_md_starts( md_ctx );
2170 mbedtls_md_update( md_ctx, secret, md_size );
2171 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002172 mbedtls_md_update( md_ctx, out, md_size );
2173 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002176
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002177/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002178 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002179#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002180 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2181 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2182 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2183/* This function makes sure every byte in the memory region is accessed
2184 * (in ascending addresses order) */
2185static void ssl_read_memory( unsigned char *p, size_t len )
2186{
2187 unsigned char acc = 0;
2188 volatile unsigned char force;
2189
2190 for( ; len != 0; p++, len-- )
2191 acc ^= *p;
2192
2193 force = acc;
2194 (void) force;
2195}
2196#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2197
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002198/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002200 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002201
Hanno Beckera0e20d02019-05-15 14:03:01 +01002202#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002203/* This functions transforms a DTLS plaintext fragment and a record content
2204 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002205 *
2206 * struct {
2207 * opaque content[DTLSPlaintext.length];
2208 * ContentType real_type;
2209 * uint8 zeros[length_of_padding];
2210 * } DTLSInnerPlaintext;
2211 *
2212 * Input:
2213 * - `content`: The beginning of the buffer holding the
2214 * plaintext to be wrapped.
2215 * - `*content_size`: The length of the plaintext in Bytes.
2216 * - `max_len`: The number of Bytes available starting from
2217 * `content`. This must be `>= *content_size`.
2218 * - `rec_type`: The desired record content type.
2219 *
2220 * Output:
2221 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2222 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2223 *
2224 * Returns:
2225 * - `0` on success.
2226 * - A negative error code if `max_len` didn't offer enough space
2227 * for the expansion.
2228 */
2229static int ssl_cid_build_inner_plaintext( unsigned char *content,
2230 size_t *content_size,
2231 size_t remaining,
2232 uint8_t rec_type )
2233{
2234 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002235 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2236 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2237 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002238
2239 /* Write real content type */
2240 if( remaining == 0 )
2241 return( -1 );
2242 content[ len ] = rec_type;
2243 len++;
2244 remaining--;
2245
2246 if( remaining < pad )
2247 return( -1 );
2248 memset( content + len, 0, pad );
2249 len += pad;
2250 remaining -= pad;
2251
2252 *content_size = len;
2253 return( 0 );
2254}
2255
Hanno Becker07dc97d2019-05-20 15:08:01 +01002256/* This function parses a DTLSInnerPlaintext structure.
2257 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002258static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2259 size_t *content_size,
2260 uint8_t *rec_type )
2261{
2262 size_t remaining = *content_size;
2263
2264 /* Determine length of padding by skipping zeroes from the back. */
2265 do
2266 {
2267 if( remaining == 0 )
2268 return( -1 );
2269 remaining--;
2270 } while( content[ remaining ] == 0 );
2271
2272 *content_size = remaining;
2273 *rec_type = content[ remaining ];
2274
2275 return( 0 );
2276}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002277#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002278
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002279/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002280 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002281static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002282 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002283 mbedtls_record *rec )
2284{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002285 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002286 *
2287 * additional_data = seq_num + TLSCompressed.type +
2288 * TLSCompressed.version + TLSCompressed.length;
2289 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002290 * For the CID extension, this is extended as follows
2291 * (quoting draft-ietf-tls-dtls-connection-id-05,
2292 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002293 *
2294 * additional_data = seq_num + DTLSPlaintext.type +
2295 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002296 * cid +
2297 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002298 * length_of_DTLSInnerPlaintext;
2299 */
2300
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002301 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2302 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002303 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002304
Hanno Beckera0e20d02019-05-15 14:03:01 +01002305#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002306 if( rec->cid_len != 0 )
2307 {
2308 memcpy( add_data + 11, rec->cid, rec->cid_len );
2309 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2310 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2311 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2312 *add_data_len = 13 + 1 + rec->cid_len;
2313 }
2314 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002315#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002316 {
2317 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2318 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2319 *add_data_len = 13;
2320 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002321}
2322
Hanno Beckera18d1322018-01-03 14:27:32 +00002323int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2324 mbedtls_ssl_transform *transform,
2325 mbedtls_record *rec,
2326 int (*f_rng)(void *, unsigned char *, size_t),
2327 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002328{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002330 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002331 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002332 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002333 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002334 size_t post_avail;
2335
2336 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002337#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02002338 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002339 ((void) ssl);
2340#endif
2341
2342 /* The PRNG is used for dynamic IV generation that's used
2343 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2344#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2345 ( defined(MBEDTLS_AES_C) || \
2346 defined(MBEDTLS_ARIA_C) || \
2347 defined(MBEDTLS_CAMELLIA_C) ) && \
2348 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2349 ((void) f_rng);
2350 ((void) p_rng);
2351#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002354
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002355 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002356 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2358 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2359 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002360 if( rec == NULL
2361 || rec->buf == NULL
2362 || rec->buf_len < rec->data_offset
2363 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002364#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002365 || rec->cid_len != 0
2366#endif
2367 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002368 {
2369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002371 }
2372
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002373 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002374 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002376 data, rec->data_len );
2377
2378 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2379
2380 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2381 {
2382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2383 (unsigned) rec->data_len,
2384 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2385 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2386 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002387
Hanno Beckera0e20d02019-05-15 14:03:01 +01002388#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002389 /*
2390 * Add CID information
2391 */
2392 rec->cid_len = transform->out_cid_len;
2393 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2394 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002395
2396 if( rec->cid_len != 0 )
2397 {
2398 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002399 * Wrap plaintext into DTLSInnerPlaintext structure.
2400 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002401 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002402 * Note that this changes `rec->data_len`, and hence
2403 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002404 */
2405 if( ssl_cid_build_inner_plaintext( data,
2406 &rec->data_len,
2407 post_avail,
2408 rec->type ) != 0 )
2409 {
2410 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2411 }
2412
2413 rec->type = MBEDTLS_SSL_MSG_CID;
2414 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002415#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002416
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002417 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2418
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002420 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 */
Hanno Becker52344c22018-01-03 15:24:20 +00002422#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 if( mode == MBEDTLS_MODE_STREAM ||
2424 ( mode == MBEDTLS_MODE_CBC
2425#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002426 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002427#endif
2428 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002430 if( post_avail < transform->maclen )
2431 {
2432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2433 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2434 }
2435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002437 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002438 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002439 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002440 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2441 data, rec->data_len, rec->ctr, rec->type, mac );
2442 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002443 }
2444 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002445#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2447 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002448 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002449 {
Hanno Becker992b6872017-11-09 18:57:39 +00002450 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2451
Hanno Beckercab87e62019-04-29 13:52:53 +01002452 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002453
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002454 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002455 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002456 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2457 data, rec->data_len );
2458 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2459 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2460
2461 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002462 }
2463 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002464#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002468 }
2469
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002470 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2471 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002472
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002473 rec->data_len += transform->maclen;
2474 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002475 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002476 }
Hanno Becker52344c22018-01-03 15:24:20 +00002477#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002479 /*
2480 * Encrypt
2481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2483 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002485 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002486 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002488 "including %d bytes of padding",
2489 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002490
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002491 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2492 transform->iv_enc, transform->ivlen,
2493 data, rec->data_len,
2494 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002497 return( ret );
2498 }
2499
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002500 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2503 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002504 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 }
Paul Bakker68884e32013-01-07 18:20:04 +01002506 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002508
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002509#if defined(MBEDTLS_GCM_C) || \
2510 defined(MBEDTLS_CCM_C) || \
2511 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002513 mode == MBEDTLS_MODE_CCM ||
2514 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002515 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002516 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002517 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002518 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002519
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002520 /* Check that there's space for both the authentication tag
2521 * and the explicit IV before and after the record content. */
2522 if( post_avail < transform->taglen ||
2523 rec->data_offset < explicit_iv_len )
2524 {
2525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2526 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2527 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002528
Paul Bakker68884e32013-01-07 18:20:04 +01002529 /*
2530 * Generate IV
2531 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002532 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2533 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002534 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002535 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002536 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2537 explicit_iv_len );
2538 /* Prefix record content with explicit IV. */
2539 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002540 }
2541 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2542 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002543 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002544 unsigned char i;
2545
2546 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2547
2548 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002549 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002550 }
2551 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002552 {
2553 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002556 }
2557
Hanno Beckercab87e62019-04-29 13:52:53 +01002558 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002559
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002560 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2561 iv, transform->ivlen );
2562 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002563 data - explicit_iv_len, explicit_iv_len );
2564 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002565 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002567 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002568 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002569
Paul Bakker68884e32013-01-07 18:20:04 +01002570 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002571 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002572 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002573
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002574 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002575 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002576 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002577 data, rec->data_len, /* source */
2578 data, &rec->data_len, /* destination */
2579 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002582 return( ret );
2583 }
2584
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002585 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2586 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002587
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002588 rec->data_len += transform->taglen + explicit_iv_len;
2589 rec->data_offset -= explicit_iv_len;
2590 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002591 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002592 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2595#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002596 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002599 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002600 size_t padlen, i;
2601 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002602
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002603 /* Currently we're always using minimal padding
2604 * (up to 255 bytes would be allowed). */
2605 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2606 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002607 padlen = 0;
2608
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002609 /* Check there's enough space in the buffer for the padding. */
2610 if( post_avail < padlen + 1 )
2611 {
2612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2613 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2614 }
2615
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002617 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002619 rec->data_len += padlen + 1;
2620 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002623 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002624 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2625 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002626 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002627 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002628 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002629 if( f_rng == NULL )
2630 {
2631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2632 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2633 }
2634
2635 if( rec->data_offset < transform->ivlen )
2636 {
2637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2638 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2639 }
2640
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002641 /*
2642 * Generate IV
2643 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002644 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002645 if( ret != 0 )
2646 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002647
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002648 memcpy( data - transform->ivlen, transform->iv_enc,
2649 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002650
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002651 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002655 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002656 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002657 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002659 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2660 transform->iv_enc,
2661 transform->ivlen,
2662 data, rec->data_len,
2663 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002666 return( ret );
2667 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002668
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002669 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2672 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002673 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002676 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002677 {
2678 /*
2679 * Save IV in SSL3 and TLS1
2680 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002681 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2682 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002684 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002685#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002686 {
2687 data -= transform->ivlen;
2688 rec->data_offset -= transform->ivlen;
2689 rec->data_len += transform->ivlen;
2690 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002693 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002694 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002695 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2696
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002697 /*
2698 * MAC(MAC_write_key, seq_num +
2699 * TLSCipherText.type +
2700 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002701 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002702 * IV + // except for TLS 1.0
2703 * ENC(content + padding + padding_length));
2704 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002705
2706 if( post_avail < transform->maclen)
2707 {
2708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2709 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2710 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002711
Hanno Beckercab87e62019-04-29 13:52:53 +01002712 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002715 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002716 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002717
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002718 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002719 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002720 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2721 data, rec->data_len );
2722 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2723 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002724
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002725 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002726
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002727 rec->data_len += transform->maclen;
2728 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002729 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002730 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002733 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002735 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2738 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002739 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002741 /* Make extra sure authentication was performed, exactly once */
2742 if( auth_done != 1 )
2743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2745 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002746 }
2747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002749
2750 return( 0 );
2751}
2752
Hanno Becker605949f2019-07-12 08:23:59 +01002753int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002754 mbedtls_ssl_transform *transform,
2755 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002756{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002757 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002759 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002760#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002761 size_t padlen = 0, correct = 1;
2762#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002763 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002764 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002765 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002766
Hanno Beckera18d1322018-01-03 14:27:32 +00002767#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02002768 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002769 ((void) ssl);
2770#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002773 if( rec == NULL ||
2774 rec->buf == NULL ||
2775 rec->buf_len < rec->data_offset ||
2776 rec->buf_len - rec->data_offset < rec->data_len )
2777 {
2778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002780 }
2781
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002782 data = rec->buf + rec->data_offset;
2783 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Hanno Beckera0e20d02019-05-15 14:03:01 +01002785#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002786 /*
2787 * Match record's CID with incoming CID.
2788 */
Hanno Becker938489a2019-05-08 13:02:22 +01002789 if( rec->cid_len != transform->in_cid_len ||
2790 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2791 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002792 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002793 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002794#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2797 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002798 {
2799 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002800 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2801 transform->iv_dec,
2802 transform->ivlen,
2803 data, rec->data_len,
2804 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002807 return( ret );
2808 }
2809
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002810 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2813 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002814 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 }
Paul Bakker68884e32013-01-07 18:20:04 +01002816 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002818#if defined(MBEDTLS_GCM_C) || \
2819 defined(MBEDTLS_CCM_C) || \
2820 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002822 mode == MBEDTLS_MODE_CCM ||
2823 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002824 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002825 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002826 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002827
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002828 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002829 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002830 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002831
2832 /* Check that there's enough space for the explicit IV
2833 * (at the beginning of the record) and the MAC (at the
2834 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002835 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002838 "+ taglen (%d)", rec->data_len,
2839 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002841 }
Paul Bakker68884e32013-01-07 18:20:04 +01002842
Hanno Beckerd96a6522019-07-10 13:55:25 +01002843#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002844 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2845 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002846 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002847
Hanno Beckerd96a6522019-07-10 13:55:25 +01002848 /* Fixed */
2849 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2850 /* Explicit */
2851 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002852 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002853 else
2854#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2855#if defined(MBEDTLS_CHACHAPOLY_C)
2856 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002857 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002858 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002859 unsigned char i;
2860
2861 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2862
2863 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002864 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002865 }
2866 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002867#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002868 {
2869 /* Reminder if we ever add an AEAD mode with a different size */
2870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2871 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2872 }
2873
Hanno Beckerd96a6522019-07-10 13:55:25 +01002874 /* Group changes to data, data_len, and add_data, because
2875 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002876 data += explicit_iv_len;
2877 rec->data_offset += explicit_iv_len;
2878 rec->data_len -= explicit_iv_len + transform->taglen;
2879
Hanno Beckercab87e62019-04-29 13:52:53 +01002880 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002881 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002882 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002883
Hanno Beckerd96a6522019-07-10 13:55:25 +01002884 /* Because of the check above, we know that there are
2885 * explicit_iv_len Bytes preceeding data, and taglen
2886 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002887 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002888 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002889
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002890 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002891 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002892 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002893
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002894 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002895 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002896 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002897 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2898 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002899 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002900 data, rec->data_len,
2901 data, &olen,
2902 data + rec->data_len,
2903 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2908 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002909
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002910 return( ret );
2911 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002912 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002913
Hanno Beckerd96a6522019-07-10 13:55:25 +01002914 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002915 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002919 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002920 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002921 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2923#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002924 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002926 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002927 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002928
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 /*
Paul Bakker45829992013-01-03 14:52:21 +01002930 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002933 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2934 {
2935 /* The ciphertext is prefixed with the CBC IV. */
2936 minlen += transform->ivlen;
2937 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002938#endif
Paul Bakker45829992013-01-03 14:52:21 +01002939
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002940 /* Size considerations:
2941 *
2942 * - The CBC cipher text must not be empty and hence
2943 * at least of size transform->ivlen.
2944 *
2945 * Together with the potential IV-prefix, this explains
2946 * the first of the two checks below.
2947 *
2948 * - The record must contain a MAC, either in plain or
2949 * encrypted, depending on whether Encrypt-then-MAC
2950 * is used or not.
2951 * - If it is, the message contains the IV-prefix,
2952 * the CBC ciphertext, and the MAC.
2953 * - If it is not, the padded plaintext, and hence
2954 * the CBC ciphertext, has at least length maclen + 1
2955 * because there is at least the padding length byte.
2956 *
2957 * As the CBC ciphertext is not empty, both cases give the
2958 * lower bound minlen + maclen + 1 on the record size, which
2959 * we test for in the second check below.
2960 */
2961 if( rec->data_len < minlen + transform->ivlen ||
2962 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002965 "+ 1 ) ( + expl IV )", rec->data_len,
2966 transform->ivlen,
2967 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002969 }
2970
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002971 /*
2972 * Authenticate before decrypt if enabled
2973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002975 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002976 {
Hanno Becker992b6872017-11-09 18:57:39 +00002977 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002980
Hanno Beckerd96a6522019-07-10 13:55:25 +01002981 /* Update data_len in tandem with add_data.
2982 *
2983 * The subtraction is safe because of the previous check
2984 * data_len >= minlen + maclen + 1.
2985 *
2986 * Afterwards, we know that data + data_len is followed by at
2987 * least maclen Bytes, which justifies the call to
2988 * mbedtls_ssl_safer_memcmp() below.
2989 *
2990 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002991 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002992 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002993
Hanno Beckerd96a6522019-07-10 13:55:25 +01002994 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002995 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2996 add_data_len );
2997 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2998 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002999 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3000 data, rec->data_len );
3001 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3002 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003003
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003004 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3005 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003006 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003007 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003008
Hanno Beckerd96a6522019-07-10 13:55:25 +01003009 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003010 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3011 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003015 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003016 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003017 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003019
3020 /*
3021 * Check length sanity
3022 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01003023
3024 /* We know from above that data_len > minlen >= 0,
3025 * so the following check in particular implies that
3026 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003027 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003030 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003032 }
3033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003035 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003036 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003037 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003038 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003039 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01003040 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003041 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003042
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003043 data += transform->ivlen;
3044 rec->data_offset += transform->ivlen;
3045 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003046 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003048
Hanno Beckerd96a6522019-07-10 13:55:25 +01003049 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3050
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003051 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3052 transform->iv_dec, transform->ivlen,
3053 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003056 return( ret );
3057 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003058
Hanno Beckerd96a6522019-07-10 13:55:25 +01003059 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003060 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003064 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003067 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003068 {
3069 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01003070 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3071 * records is equivalent to CBC decryption of the concatenation
3072 * of the records; in other words, IVs are maintained across
3073 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003074 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003075 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3076 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003077 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003078#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003079
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003080 /* Safe since data_len >= minlen + maclen + 1, so after having
3081 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003082 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3083 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003084 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003085
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003086 if( auth_done == 1 )
3087 {
3088 correct *= ( rec->data_len >= padlen + 1 );
3089 padlen *= ( rec->data_len >= padlen + 1 );
3090 }
3091 else
Paul Bakker45829992013-01-03 14:52:21 +01003092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003094 if( rec->data_len < transform->maclen + padlen + 1 )
3095 {
3096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3097 rec->data_len,
3098 transform->maclen,
3099 padlen + 1 ) );
3100 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003101#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003102
3103 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3104 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003105 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003106
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003107 padlen++;
3108
3109 /* Regardless of the validity of the padding,
3110 * we have data_len >= padlen here. */
3111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003113 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003114 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003115 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#if defined(MBEDTLS_SSL_DEBUG_ALL)
3118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003119 "should be no more than %d",
3120 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003121#endif
Paul Bakker45829992013-01-03 14:52:21 +01003122 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003123 }
3124 }
3125 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3127#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3128 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003129 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003131 /* The padding check involves a series of up to 256
3132 * consecutive memory reads at the end of the record
3133 * plaintext buffer. In order to hide the length and
3134 * validity of the padding, always perform exactly
3135 * `min(256,plaintext_len)` reads (but take into account
3136 * only the last `padlen` bytes for the padding check). */
3137 size_t pad_count = 0;
3138 size_t real_count = 0;
3139 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003140
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003141 /* Index of first padding byte; it has been ensured above
3142 * that the subtraction is safe. */
3143 size_t const padding_idx = rec->data_len - padlen;
3144 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3145 size_t const start_idx = rec->data_len - num_checks;
3146 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003147
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003148 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003149 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003150 real_count |= ( idx >= padding_idx );
3151 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003152 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003153 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003156 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003158#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003159 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003160 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003161 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3163 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3166 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003167 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003168
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003169 /* If the padding was found to be invalid, padlen == 0
3170 * and the subtraction is safe. If the padding was found valid,
3171 * padlen hasn't been changed and the previous assertion
3172 * data_len >= padlen still holds. */
3173 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003175 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003177 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003181 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003182
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003183#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003185 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003186#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003187
3188 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003189 * Authenticate if not done yet.
3190 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 */
Hanno Becker52344c22018-01-03 15:24:20 +00003192#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003193 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003194 {
Hanno Becker992b6872017-11-09 18:57:39 +00003195 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003196
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003197 /* If the initial value of padlen was such that
3198 * data_len < maclen + padlen + 1, then padlen
3199 * got reset to 1, and the initial check
3200 * data_len >= minlen + maclen + 1
3201 * guarantees that at this point we still
3202 * have at least data_len >= maclen.
3203 *
3204 * If the initial value of padlen was such that
3205 * data_len >= maclen + padlen + 1, then we have
3206 * subtracted either padlen + 1 (if the padding was correct)
3207 * or 0 (if the padding was incorrect) since then,
3208 * hence data_len >= maclen in any case.
3209 */
3210 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003211 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003214 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003215 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003216 ssl_mac( &transform->md_ctx_dec,
3217 transform->mac_dec,
3218 data, rec->data_len,
3219 rec->ctr, rec->type,
3220 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003221 }
3222 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3224#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3225 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003226 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003227 {
3228 /*
3229 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003230 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003231 *
3232 * Known timing attacks:
3233 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3234 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003235 * To compensate for different timings for the MAC calculation
3236 * depending on how much padding was removed (which is determined
3237 * by padlen), process extra_run more blocks through the hash
3238 * function.
3239 *
3240 * The formula in the paper is
3241 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3242 * where L1 is the size of the header plus the decrypted message
3243 * plus CBC padding and L2 is the size of the header plus the
3244 * decrypted message. This is for an underlying hash function
3245 * with 64-byte blocks.
3246 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3247 * correctly. We round down instead of up, so -56 is the correct
3248 * value for our calculations instead of -55.
3249 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003250 * Repeat the formula rather than defining a block_size variable.
3251 * This avoids requiring division by a variable at runtime
3252 * (which would be marginally less efficient and would require
3253 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003254 */
3255 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003256 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003257
3258 /*
3259 * The next two sizes are the minimum and maximum values of
3260 * in_msglen over all padlen values.
3261 *
3262 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003263 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003264 *
3265 * Note that max_len + maclen is never more than the buffer
3266 * length, as we previously did in_msglen -= maclen too.
3267 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003268 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003269 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3270
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003271 memset( tmp, 0, sizeof( tmp ) );
3272
3273 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003274 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003275#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3276 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003277 case MBEDTLS_MD_MD5:
3278 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003279 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003280 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003281 extra_run =
3282 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3283 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003284 break;
3285#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003286#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003287 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003288 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003289 extra_run =
3290 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3291 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003292 break;
3293#endif
3294 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003296 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3297 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003298
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003299 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003300
Hanno Beckercab87e62019-04-29 13:52:53 +01003301 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3302 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003303 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3304 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003305 /* Make sure we access everything even when padlen > 0. This
3306 * makes the synchronisation requirements for just-in-time
3307 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003308 ssl_read_memory( data + rec->data_len, padlen );
3309 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003310
3311 /* Call mbedtls_md_process at least once due to cache attacks
3312 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003313 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003314 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003315
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003316 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003317
3318 /* Make sure we access all the memory that could contain the MAC,
3319 * before we check it in the next code block. This makes the
3320 * synchronisation requirements for just-in-time Prime+Probe
3321 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003322 ssl_read_memory( data + min_len,
3323 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003324 }
3325 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3327 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3330 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003332
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003333#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003334 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3335 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003336#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003337
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003338 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3339 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341#if defined(MBEDTLS_SSL_DEBUG_ALL)
3342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003343#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003344 correct = 0;
3345 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003346 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003347 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003348
3349 /*
3350 * Finally check the correct flag
3351 */
3352 if( correct == 0 )
3353 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003354#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003355
3356 /* Make extra sure authentication was performed, exactly once */
3357 if( auth_done != 1 )
3358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3360 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003361 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003362
Hanno Beckera0e20d02019-05-15 14:03:01 +01003363#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003364 if( rec->cid_len != 0 )
3365 {
3366 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3367 &rec->type );
3368 if( ret != 0 )
3369 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3370 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003371#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003374
3375 return( 0 );
3376}
3377
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003378#undef MAC_NONE
3379#undef MAC_PLAINTEXT
3380#undef MAC_CIPHERTEXT
3381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003383/*
3384 * Compression/decompression functions
3385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003387{
3388 int ret;
3389 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003390 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003391 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003392 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003395
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003396 if( len_pre == 0 )
3397 return( 0 );
3398
Paul Bakker2770fbd2012-07-03 13:30:23 +00003399 memcpy( msg_pre, ssl->out_msg, len_pre );
3400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003402 ssl->out_msglen ) );
3403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003405 ssl->out_msg, ssl->out_msglen );
3406
Paul Bakker48916f92012-09-16 19:57:18 +00003407 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3408 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3409 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003410 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003411
Paul Bakker48916f92012-09-16 19:57:18 +00003412 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003413 if( ret != Z_OK )
3414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3416 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003417 }
3418
Angus Grattond8213d02016-05-25 20:56:48 +10003419 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003420 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003423 ssl->out_msglen ) );
3424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003426 ssl->out_msg, ssl->out_msglen );
3427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003429
3430 return( 0 );
3431}
3432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003434{
3435 int ret;
3436 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003437 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003438 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003439 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003442
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003443 if( len_pre == 0 )
3444 return( 0 );
3445
Paul Bakker2770fbd2012-07-03 13:30:23 +00003446 memcpy( msg_pre, ssl->in_msg, len_pre );
3447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003449 ssl->in_msglen ) );
3450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003452 ssl->in_msg, ssl->in_msglen );
3453
Paul Bakker48916f92012-09-16 19:57:18 +00003454 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3455 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3456 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003457 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003458 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003459
Paul Bakker48916f92012-09-16 19:57:18 +00003460 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003461 if( ret != Z_OK )
3462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3464 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003465 }
3466
Angus Grattond8213d02016-05-25 20:56:48 +10003467 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003468 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003471 ssl->in_msglen ) );
3472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003474 ssl->in_msg, ssl->in_msglen );
3475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003477
3478 return( 0 );
3479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3483static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485#if defined(MBEDTLS_SSL_PROTO_DTLS)
3486static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003487{
3488 /* If renegotiation is not enforced, retransmit until we would reach max
3489 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003490 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003491 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003492 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003493 unsigned char doublings = 1;
3494
3495 while( ratio != 0 )
3496 {
3497 ++doublings;
3498 ratio >>= 1;
3499 }
3500
3501 if( ++ssl->renego_records_seen > doublings )
3502 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003504 return( 0 );
3505 }
3506 }
3507
3508 return( ssl_write_hello_request( ssl ) );
3509}
3510#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003512
Paul Bakker5121ce52009-01-03 21:22:43 +00003513/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003514 * Fill the input message buffer by appending data to it.
3515 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003516 *
3517 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3518 * available (from this read and/or a previous one). Otherwise, an error code
3519 * is returned (possibly EOF or WANT_READ).
3520 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003521 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3522 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3523 * since we always read a whole datagram at once.
3524 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003525 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003526 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003529{
Paul Bakker23986e52011-04-24 08:57:21 +00003530 int ret;
3531 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003534
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003535 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003538 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003540 }
3541
Angus Grattond8213d02016-05-25 20:56:48 +10003542 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003546 }
3547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003549 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003550 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003551 uint32_t timeout;
3552
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003553 /* Just to be sure */
3554 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3555 {
3556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3557 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3559 }
3560
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003561 /*
3562 * The point is, we need to always read a full datagram at once, so we
3563 * sometimes read more then requested, and handle the additional data.
3564 * It could be the rest of the current record (while fetching the
3565 * header) and/or some other records in the same datagram.
3566 */
3567
3568 /*
3569 * Move to the next record in the already read datagram if applicable
3570 */
3571 if( ssl->next_record_offset != 0 )
3572 {
3573 if( ssl->in_left < ssl->next_record_offset )
3574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3576 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003577 }
3578
3579 ssl->in_left -= ssl->next_record_offset;
3580
3581 if( ssl->in_left != 0 )
3582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003584 ssl->next_record_offset ) );
3585 memmove( ssl->in_hdr,
3586 ssl->in_hdr + ssl->next_record_offset,
3587 ssl->in_left );
3588 }
3589
3590 ssl->next_record_offset = 0;
3591 }
3592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003595
3596 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003597 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003598 */
3599 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003602 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003603 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003604
3605 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003606 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003607 * are not at the beginning of a new record, the caller did something
3608 * wrong.
3609 */
3610 if( ssl->in_left != 0 )
3611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003614 }
3615
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003616 /*
3617 * Don't even try to read if time's out already.
3618 * This avoids by-passing the timer when repeatedly receiving messages
3619 * that will end up being dropped.
3620 */
3621 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003622 {
3623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003624 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003625 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003626 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003627 {
Angus Grattond8213d02016-05-25 20:56:48 +10003628 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003631 timeout = ssl->handshake->retransmit_timeout;
3632 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003633 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003636
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003637 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003638 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3639 timeout );
3640 else
3641 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003644
3645 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003647 }
3648
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003649 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003652 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003655 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003656 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003659 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003660 }
3661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003665 return( ret );
3666 }
3667
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003668 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003669 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003671 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003673 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003674 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003677 return( ret );
3678 }
3679
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003680 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003681 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003683 }
3684
Paul Bakker5121ce52009-01-03 21:22:43 +00003685 if( ret < 0 )
3686 return( ret );
3687
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003688 ssl->in_left = ret;
3689 }
3690 else
3691#endif
3692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003694 ssl->in_left, nb_want ) );
3695
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003696 while( ssl->in_left < nb_want )
3697 {
3698 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003699
3700 if( ssl_check_timer( ssl ) != 0 )
3701 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3702 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003703 {
3704 if( ssl->f_recv_timeout != NULL )
3705 {
3706 ret = ssl->f_recv_timeout( ssl->p_bio,
3707 ssl->in_hdr + ssl->in_left, len,
3708 ssl->conf->read_timeout );
3709 }
3710 else
3711 {
3712 ret = ssl->f_recv( ssl->p_bio,
3713 ssl->in_hdr + ssl->in_left, len );
3714 }
3715 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003718 ssl->in_left, nb_want ) );
3719 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003720
3721 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003723
3724 if( ret < 0 )
3725 return( ret );
3726
mohammad160352aecb92018-03-28 23:41:40 -07003727 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003728 {
Darryl Green11999bb2018-03-13 15:22:58 +00003729 MBEDTLS_SSL_DEBUG_MSG( 1,
3730 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003731 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003732 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3733 }
3734
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003735 ssl->in_left += ret;
3736 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003737 }
3738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003740
3741 return( 0 );
3742}
3743
3744/*
3745 * Flush any data not yet written
3746 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003748{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003749 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003750 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003753
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003754 if( ssl->f_send == NULL )
3755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003757 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003759 }
3760
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003761 /* Avoid incrementing counter if data is flushed */
3762 if( ssl->out_left == 0 )
3763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003765 return( 0 );
3766 }
3767
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 while( ssl->out_left > 0 )
3769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003771 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003772
Hanno Becker2b1e3542018-08-06 11:19:13 +01003773 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003774 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003777
3778 if( ret <= 0 )
3779 return( ret );
3780
mohammad160352aecb92018-03-28 23:41:40 -07003781 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003782 {
Darryl Green11999bb2018-03-13 15:22:58 +00003783 MBEDTLS_SSL_DEBUG_MSG( 1,
3784 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003785 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003786 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3787 }
3788
Paul Bakker5121ce52009-01-03 21:22:43 +00003789 ssl->out_left -= ret;
3790 }
3791
Hanno Becker2b1e3542018-08-06 11:19:13 +01003792#if defined(MBEDTLS_SSL_PROTO_DTLS)
3793 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003794 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003795 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003796 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003797 else
3798#endif
3799 {
3800 ssl->out_hdr = ssl->out_buf + 8;
3801 }
3802 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003805
3806 return( 0 );
3807}
3808
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003809/*
3810 * Functions to handle the DTLS retransmission state machine
3811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003812#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003813/*
3814 * Append current handshake message to current outgoing flight
3815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003817{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3820 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3821 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003822
3823 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003824 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003825 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003828 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003829 }
3830
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003831 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003832 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003835 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003836 }
3837
3838 /* Copy current handshake message with headers */
3839 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3840 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003841 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003842 msg->next = NULL;
3843
3844 /* Append to the current flight */
3845 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003846 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003847 else
3848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003850 while( cur->next != NULL )
3851 cur = cur->next;
3852 cur->next = msg;
3853 }
3854
Hanno Becker3b235902018-08-06 09:54:53 +01003855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003856 return( 0 );
3857}
3858
3859/*
3860 * Free the current flight of handshake messages
3861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003863{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864 mbedtls_ssl_flight_item *cur = flight;
3865 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003866
3867 while( cur != NULL )
3868 {
3869 next = cur->next;
3870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871 mbedtls_free( cur->p );
3872 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003873
3874 cur = next;
3875 }
3876}
3877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3879static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003880#endif
3881
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003882/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003883 * Swap transform_out and out_ctr with the alternative ones
3884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003886{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003888 unsigned char tmp_out_ctr[8];
3889
3890 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003893 return;
3894 }
3895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003897
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003898 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003899 tmp_transform = ssl->transform_out;
3900 ssl->transform_out = ssl->handshake->alt_transform_out;
3901 ssl->handshake->alt_transform_out = tmp_transform;
3902
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003903 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003904 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3905 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003906 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003907
3908 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003909 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3912 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3917 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003918 }
3919 }
3920#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003921}
3922
3923/*
3924 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003925 */
3926int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3927{
3928 int ret = 0;
3929
3930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3931
3932 ret = mbedtls_ssl_flight_transmit( ssl );
3933
3934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3935
3936 return( ret );
3937}
3938
3939/*
3940 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003941 *
3942 * Need to remember the current message in case flush_output returns
3943 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003944 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003945 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003946int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003947{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003948 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003952 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003954
3955 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003956 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003957 ssl_swap_epochs( ssl );
3958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003960 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003961
3962 while( ssl->handshake->cur_msg != NULL )
3963 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003964 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003965 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003966
Hanno Beckere1dcb032018-08-17 16:47:58 +01003967 int const is_finished =
3968 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3969 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3970
Hanno Becker04da1892018-08-14 13:22:10 +01003971 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3972 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3973
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003974 /* Swap epochs before sending Finished: we can't do it after
3975 * sending ChangeCipherSpec, in case write returns WANT_READ.
3976 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003977 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003978 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003980 ssl_swap_epochs( ssl );
3981 }
3982
Hanno Becker67bc7c32018-08-06 11:33:50 +01003983 ret = ssl_get_remaining_payload_in_datagram( ssl );
3984 if( ret < 0 )
3985 return( ret );
3986 max_frag_len = (size_t) ret;
3987
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003988 /* CCS is copied as is, while HS messages may need fragmentation */
3989 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3990 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003991 if( max_frag_len == 0 )
3992 {
3993 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3994 return( ret );
3995
3996 continue;
3997 }
3998
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003999 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004000 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004001 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004002
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004003 /* Update position inside current message */
4004 ssl->handshake->cur_msg_p += cur->len;
4005 }
4006 else
4007 {
4008 const unsigned char * const p = ssl->handshake->cur_msg_p;
4009 const size_t hs_len = cur->len - 12;
4010 const size_t frag_off = p - ( cur->p + 12 );
4011 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004012 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004013
Hanno Beckere1dcb032018-08-17 16:47:58 +01004014 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004015 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004016 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004017 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004018
Hanno Becker67bc7c32018-08-06 11:33:50 +01004019 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4020 return( ret );
4021
4022 continue;
4023 }
4024 max_hs_frag_len = max_frag_len - 12;
4025
4026 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4027 max_hs_frag_len : rem_len;
4028
4029 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004030 {
4031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004032 (unsigned) cur_hs_frag_len,
4033 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004034 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004035
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004036 /* Messages are stored with handshake headers as if not fragmented,
4037 * copy beginning of headers then fill fragmentation fields.
4038 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4039 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004040
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004041 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4042 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4043 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4044
Hanno Becker67bc7c32018-08-06 11:33:50 +01004045 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4046 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4047 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004048
4049 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4050
Hanno Becker3f7b9732018-08-28 09:53:25 +01004051 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004052 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4053 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004054 ssl->out_msgtype = cur->type;
4055
4056 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004057 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004058 }
4059
4060 /* If done with the current message move to the next one if any */
4061 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4062 {
4063 if( cur->next != NULL )
4064 {
4065 ssl->handshake->cur_msg = cur->next;
4066 ssl->handshake->cur_msg_p = cur->next->p + 12;
4067 }
4068 else
4069 {
4070 ssl->handshake->cur_msg = NULL;
4071 ssl->handshake->cur_msg_p = NULL;
4072 }
4073 }
4074
4075 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004076 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004079 return( ret );
4080 }
4081 }
4082
Hanno Becker67bc7c32018-08-06 11:33:50 +01004083 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4084 return( ret );
4085
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004086 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4088 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004089 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004091 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004092 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4093 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004094
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004096
4097 return( 0 );
4098}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004099
4100/*
4101 * To be called when the last message of an incoming flight is received.
4102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004104{
4105 /* We won't need to resend that one any more */
4106 ssl_flight_free( ssl->handshake->flight );
4107 ssl->handshake->flight = NULL;
4108 ssl->handshake->cur_msg = NULL;
4109
4110 /* The next incoming flight will start with this msg_seq */
4111 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4112
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004113 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004114 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004115
Hanno Becker0271f962018-08-16 13:23:47 +01004116 /* Clear future message buffering structure. */
4117 ssl_buffering_free( ssl );
4118
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004119 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004120 ssl_set_timer( ssl, 0 );
4121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4123 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004125 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004126 }
4127 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004129}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004130
4131/*
4132 * To be called when the last message of an outgoing flight is send.
4133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004134void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004135{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004136 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004137 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4140 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004143 }
4144 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004148
Paul Bakker5121ce52009-01-03 21:22:43 +00004149/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004150 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004151 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004152
4153/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004154 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004155 *
4156 * - fill in handshake headers
4157 * - update handshake checksum
4158 * - DTLS: save message for resending
4159 * - then pass to the record layer
4160 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004161 * DTLS: except for HelloRequest, messages are only queued, and will only be
4162 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004163 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004164 * Inputs:
4165 * - ssl->out_msglen: 4 + actual handshake message len
4166 * (4 is the size of handshake headers for TLS)
4167 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4168 * - ssl->out_msg + 4: the handshake message body
4169 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004170 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004171 * - ssl->out_msglen: the length of the record contents
4172 * (including handshake headers but excluding record headers)
4173 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004174 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004175int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004176{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004177 int ret;
4178 const size_t hs_len = ssl->out_msglen - 4;
4179 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004180
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4182
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004183 /*
4184 * Sanity checks
4185 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004186 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004187 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4188 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004189 /* In SSLv3, the client might send a NoCertificate alert. */
4190#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4191 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4192 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4193 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4194#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4195 {
4196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4197 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4198 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004200
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004201 /* Whenever we send anything different from a
4202 * HelloRequest we should be in a handshake - double check. */
4203 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4204 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004205 ssl->handshake == NULL )
4206 {
4207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4208 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4209 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004212 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004213 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004215 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4217 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004218 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004219#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004220
Hanno Beckerb50a2532018-08-06 11:52:54 +01004221 /* Double-check that we did not exceed the bounds
4222 * of the outgoing record buffer.
4223 * This should never fail as the various message
4224 * writing functions must obey the bounds of the
4225 * outgoing record buffer, but better be safe.
4226 *
4227 * Note: We deliberately do not check for the MTU or MFL here.
4228 */
4229 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4230 {
4231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4232 "size %u, maximum %u",
4233 (unsigned) ssl->out_msglen,
4234 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4236 }
4237
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004238 /*
4239 * Fill handshake headers
4240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004242 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004243 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4244 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4245 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004246
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004247 /*
4248 * DTLS has additional fields in the Handshake layer,
4249 * between the length field and the actual payload:
4250 * uint16 message_seq;
4251 * uint24 fragment_offset;
4252 * uint24 fragment_length;
4253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004255 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004256 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004257 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004258 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004259 {
4260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4261 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004262 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004263 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004264 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4265 }
4266
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004267 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004268 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004269
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004270 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004271 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004272 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004273 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4274 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4275 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004276 }
4277 else
4278 {
4279 ssl->out_msg[4] = 0;
4280 ssl->out_msg[5] = 0;
4281 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004282
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004283 /* Handshake hashes are computed without fragmentation,
4284 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004285 memset( ssl->out_msg + 6, 0x00, 3 );
4286 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004287 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004289
Hanno Becker0207e532018-08-28 10:28:28 +01004290 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004291 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4292 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004293 }
4294
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004295 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004297 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004298 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4299 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004300 {
4301 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004304 return( ret );
4305 }
4306 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004307 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004308#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004309 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004310 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004311 {
4312 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4313 return( ret );
4314 }
4315 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004316
4317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4318
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004319 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004320}
4321
4322/*
4323 * Record layer functions
4324 */
4325
4326/*
4327 * Write current record.
4328 *
4329 * Uses:
4330 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4331 * - ssl->out_msglen: length of the record content (excl headers)
4332 * - ssl->out_msg: record content
4333 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004334int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004335{
4336 int ret, done = 0;
4337 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004338 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004339
4340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004343 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004344 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004345 {
4346 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004349 return( ret );
4350 }
4351
4352 len = ssl->out_msglen;
4353 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4357 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361 ret = mbedtls_ssl_hw_record_write( ssl );
4362 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4365 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004366 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004367
4368 if( ret == 0 )
4369 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004370 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004372 if( !done )
4373 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004374 unsigned i;
4375 size_t protected_record_size;
4376
Hanno Becker6430faf2019-05-08 11:57:13 +01004377 /* Skip writing the record content type to after the encryption,
4378 * as it may change when using the CID extension. */
4379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004381 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004382
Hanno Becker19859472018-08-06 09:40:20 +01004383 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004384 ssl->out_len[0] = (unsigned char)( len >> 8 );
4385 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004386
Paul Bakker48916f92012-09-16 19:57:18 +00004387 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004388 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004389 mbedtls_record rec;
4390
4391 rec.buf = ssl->out_iv;
4392 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4393 ( ssl->out_iv - ssl->out_buf );
4394 rec.data_len = ssl->out_msglen;
4395 rec.data_offset = ssl->out_msg - rec.buf;
4396
4397 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4398 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4399 ssl->conf->transport, rec.ver );
4400 rec.type = ssl->out_msgtype;
4401
Hanno Beckera0e20d02019-05-15 14:03:01 +01004402#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004403 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004404 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004405#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004406
Hanno Beckera18d1322018-01-03 14:27:32 +00004407 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004408 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004410 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004411 return( ret );
4412 }
4413
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004414 if( rec.data_offset != 0 )
4415 {
4416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4418 }
4419
Hanno Becker6430faf2019-05-08 11:57:13 +01004420 /* Update the record content type and CID. */
4421 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004422#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004423 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004424#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004425 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004426 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4427 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004428 }
4429
Hanno Becker5903de42019-05-03 14:46:38 +01004430 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004431
4432#if defined(MBEDTLS_SSL_PROTO_DTLS)
4433 /* In case of DTLS, double-check that we don't exceed
4434 * the remaining space in the datagram. */
4435 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4436 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004437 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004438 if( ret < 0 )
4439 return( ret );
4440
4441 if( protected_record_size > (size_t) ret )
4442 {
4443 /* Should never happen */
4444 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4445 }
4446 }
4447#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004448
Hanno Becker6430faf2019-05-08 11:57:13 +01004449 /* Now write the potentially updated record content type. */
4450 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004453 "version = [%d:%d], msglen = %d",
4454 ssl->out_hdr[0], ssl->out_hdr[1],
4455 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004458 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004459
4460 ssl->out_left += protected_record_size;
4461 ssl->out_hdr += protected_record_size;
4462 ssl_update_out_pointers( ssl, ssl->transform_out );
4463
Hanno Becker04484622018-08-06 09:49:38 +01004464 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4465 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4466 break;
4467
4468 /* The loop goes to its end iff the counter is wrapping */
4469 if( i == ssl_ep_len( ssl ) )
4470 {
4471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4472 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004474 }
4475
Hanno Becker67bc7c32018-08-06 11:33:50 +01004476#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004477 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4478 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004479 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004480 size_t remaining;
4481 ret = ssl_get_remaining_payload_in_datagram( ssl );
4482 if( ret < 0 )
4483 {
4484 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4485 ret );
4486 return( ret );
4487 }
4488
4489 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004490 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004491 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004492 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004493 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004494 else
4495 {
Hanno Becker513815a2018-08-20 11:56:09 +01004496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004497 }
4498 }
4499#endif /* MBEDTLS_SSL_PROTO_DTLS */
4500
4501 if( ( flush == SSL_FORCE_FLUSH ) &&
4502 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004505 return( ret );
4506 }
4507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004509
4510 return( 0 );
4511}
4512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004514
4515static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4516{
4517 if( ssl->in_msglen < ssl->in_hslen ||
4518 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4519 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4520 {
4521 return( 1 );
4522 }
4523 return( 0 );
4524}
Hanno Becker44650b72018-08-16 12:51:11 +01004525
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004526static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004527{
4528 return( ( ssl->in_msg[9] << 16 ) |
4529 ( ssl->in_msg[10] << 8 ) |
4530 ssl->in_msg[11] );
4531}
4532
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004533static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004534{
4535 return( ( ssl->in_msg[6] << 16 ) |
4536 ( ssl->in_msg[7] << 8 ) |
4537 ssl->in_msg[8] );
4538}
4539
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004540static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004541{
4542 uint32_t msg_len, frag_off, frag_len;
4543
4544 msg_len = ssl_get_hs_total_len( ssl );
4545 frag_off = ssl_get_hs_frag_off( ssl );
4546 frag_len = ssl_get_hs_frag_len( ssl );
4547
4548 if( frag_off > msg_len )
4549 return( -1 );
4550
4551 if( frag_len > msg_len - frag_off )
4552 return( -1 );
4553
4554 if( frag_len + 12 > ssl->in_msglen )
4555 return( -1 );
4556
4557 return( 0 );
4558}
4559
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004560/*
4561 * Mark bits in bitmask (used for DTLS HS reassembly)
4562 */
4563static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4564{
4565 unsigned int start_bits, end_bits;
4566
4567 start_bits = 8 - ( offset % 8 );
4568 if( start_bits != 8 )
4569 {
4570 size_t first_byte_idx = offset / 8;
4571
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004572 /* Special case */
4573 if( len <= start_bits )
4574 {
4575 for( ; len != 0; len-- )
4576 mask[first_byte_idx] |= 1 << ( start_bits - len );
4577
4578 /* Avoid potential issues with offset or len becoming invalid */
4579 return;
4580 }
4581
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004582 offset += start_bits; /* Now offset % 8 == 0 */
4583 len -= start_bits;
4584
4585 for( ; start_bits != 0; start_bits-- )
4586 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4587 }
4588
4589 end_bits = len % 8;
4590 if( end_bits != 0 )
4591 {
4592 size_t last_byte_idx = ( offset + len ) / 8;
4593
4594 len -= end_bits; /* Now len % 8 == 0 */
4595
4596 for( ; end_bits != 0; end_bits-- )
4597 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4598 }
4599
4600 memset( mask + offset / 8, 0xFF, len / 8 );
4601}
4602
4603/*
4604 * Check that bitmask is full
4605 */
4606static int ssl_bitmask_check( unsigned char *mask, size_t len )
4607{
4608 size_t i;
4609
4610 for( i = 0; i < len / 8; i++ )
4611 if( mask[i] != 0xFF )
4612 return( -1 );
4613
4614 for( i = 0; i < len % 8; i++ )
4615 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4616 return( -1 );
4617
4618 return( 0 );
4619}
4620
Hanno Becker56e205e2018-08-16 09:06:12 +01004621/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004622static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004623 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004624{
Hanno Becker56e205e2018-08-16 09:06:12 +01004625 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004626
Hanno Becker56e205e2018-08-16 09:06:12 +01004627 alloc_len = 12; /* Handshake header */
4628 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004629
Hanno Beckerd07df862018-08-16 09:14:58 +01004630 if( add_bitmap )
4631 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004632
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004633 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004634}
Hanno Becker56e205e2018-08-16 09:06:12 +01004635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004636#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004637
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004638static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004639{
4640 return( ( ssl->in_msg[1] << 16 ) |
4641 ( ssl->in_msg[2] << 8 ) |
4642 ssl->in_msg[3] );
4643}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004644
Simon Butcher99000142016-10-13 17:21:01 +01004645int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004646{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004650 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004651 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004652 }
4653
Hanno Becker12555c62018-08-16 12:47:53 +01004654 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004657 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004658 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004661 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004662 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004663 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004664 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004665
Hanno Becker44650b72018-08-16 12:51:11 +01004666 if( ssl_check_hs_header( ssl ) != 0 )
4667 {
4668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4669 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4670 }
4671
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004672 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004673 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4674 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4675 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4676 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004677 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004678 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4679 {
4680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4681 recv_msg_seq,
4682 ssl->handshake->in_msg_seq ) );
4683 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4684 }
4685
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004686 /* Retransmit only on last message from previous flight, to avoid
4687 * too many retransmissions.
4688 * Besides, No sane server ever retransmits HelloVerifyRequest */
4689 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004690 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004693 "message_seq = %d, start_of_flight = %d",
4694 recv_msg_seq,
4695 ssl->handshake->in_flight_start_seq ) );
4696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004697 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004700 return( ret );
4701 }
4702 }
4703 else
4704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004706 "message_seq = %d, expected = %d",
4707 recv_msg_seq,
4708 ssl->handshake->in_msg_seq ) );
4709 }
4710
Hanno Becker90333da2017-10-10 11:27:13 +01004711 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004712 }
4713 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004714
Hanno Becker6d97ef52018-08-16 13:09:04 +01004715 /* Message reassembly is handled alongside buffering of future
4716 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004717 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004718 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004719 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004722 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004723 }
4724 }
4725 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004726#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004727 /* With TLS we don't handle fragmentation (for now) */
4728 if( ssl->in_msglen < ssl->in_hslen )
4729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4731 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004732 }
4733
Simon Butcher99000142016-10-13 17:21:01 +01004734 return( 0 );
4735}
4736
4737void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4738{
Hanno Becker0271f962018-08-16 13:23:47 +01004739 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004740
Hanno Becker0271f962018-08-16 13:23:47 +01004741 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004742 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004743 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004744 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004745
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004746 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004747#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004748 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004749 ssl->handshake != NULL )
4750 {
Hanno Becker0271f962018-08-16 13:23:47 +01004751 unsigned offset;
4752 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004753
Hanno Becker0271f962018-08-16 13:23:47 +01004754 /* Increment handshake sequence number */
4755 hs->in_msg_seq++;
4756
4757 /*
4758 * Clear up handshake buffering and reassembly structure.
4759 */
4760
4761 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004762 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004763
4764 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004765 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4766 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004767 offset++, hs_buf++ )
4768 {
4769 *hs_buf = *(hs_buf + 1);
4770 }
4771
4772 /* Create a fresh last entry */
4773 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004774 }
4775#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004776}
4777
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004778/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004779 * DTLS anti-replay: RFC 6347 4.1.2.6
4780 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004781 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4782 * Bit n is set iff record number in_window_top - n has been seen.
4783 *
4784 * Usually, in_window_top is the last record number seen and the lsb of
4785 * in_window is set. The only exception is the initial state (record number 0
4786 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4789static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004790{
4791 ssl->in_window_top = 0;
4792 ssl->in_window = 0;
4793}
4794
4795static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4796{
4797 return( ( (uint64_t) buf[0] << 40 ) |
4798 ( (uint64_t) buf[1] << 32 ) |
4799 ( (uint64_t) buf[2] << 24 ) |
4800 ( (uint64_t) buf[3] << 16 ) |
4801 ( (uint64_t) buf[4] << 8 ) |
4802 ( (uint64_t) buf[5] ) );
4803}
4804
4805/*
4806 * Return 0 if sequence number is acceptable, -1 otherwise
4807 */
Hanno Becker0183d692019-07-12 08:50:37 +01004808int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004809{
4810 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4811 uint64_t bit;
4812
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004813 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004814 return( 0 );
4815
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004816 if( rec_seqnum > ssl->in_window_top )
4817 return( 0 );
4818
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004819 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004820
4821 if( bit >= 64 )
4822 return( -1 );
4823
4824 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4825 return( -1 );
4826
4827 return( 0 );
4828}
4829
4830/*
4831 * Update replay window on new validated record
4832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004834{
4835 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4836
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004837 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004838 return;
4839
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004840 if( rec_seqnum > ssl->in_window_top )
4841 {
4842 /* Update window_top and the contents of the window */
4843 uint64_t shift = rec_seqnum - ssl->in_window_top;
4844
4845 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004846 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004847 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004848 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004849 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004850 ssl->in_window |= 1;
4851 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004852
4853 ssl->in_window_top = rec_seqnum;
4854 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004855 else
4856 {
4857 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004858 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004859
4860 if( bit < 64 ) /* Always true, but be extra sure */
4861 ssl->in_window |= (uint64_t) 1 << bit;
4862 }
4863}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004865
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004866#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004867/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004868static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4869
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004870/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004871 * Without any SSL context, check if a datagram looks like a ClientHello with
4872 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004873 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004874 *
4875 * - if cookie is valid, return 0
4876 * - if ClientHello looks superficially valid but cookie is not,
4877 * fill obuf and set olen, then
4878 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4879 * - otherwise return a specific error code
4880 */
4881static int ssl_check_dtls_clihlo_cookie(
4882 mbedtls_ssl_cookie_write_t *f_cookie_write,
4883 mbedtls_ssl_cookie_check_t *f_cookie_check,
4884 void *p_cookie,
4885 const unsigned char *cli_id, size_t cli_id_len,
4886 const unsigned char *in, size_t in_len,
4887 unsigned char *obuf, size_t buf_len, size_t *olen )
4888{
4889 size_t sid_len, cookie_len;
4890 unsigned char *p;
4891
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004892 /*
4893 * Structure of ClientHello with record and handshake headers,
4894 * and expected values. We don't need to check a lot, more checks will be
4895 * done when actually parsing the ClientHello - skipping those checks
4896 * avoids code duplication and does not make cookie forging any easier.
4897 *
4898 * 0-0 ContentType type; copied, must be handshake
4899 * 1-2 ProtocolVersion version; copied
4900 * 3-4 uint16 epoch; copied, must be 0
4901 * 5-10 uint48 sequence_number; copied
4902 * 11-12 uint16 length; (ignored)
4903 *
4904 * 13-13 HandshakeType msg_type; (ignored)
4905 * 14-16 uint24 length; (ignored)
4906 * 17-18 uint16 message_seq; copied
4907 * 19-21 uint24 fragment_offset; copied, must be 0
4908 * 22-24 uint24 fragment_length; (ignored)
4909 *
4910 * 25-26 ProtocolVersion client_version; (ignored)
4911 * 27-58 Random random; (ignored)
4912 * 59-xx SessionID session_id; 1 byte len + sid_len content
4913 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4914 * ...
4915 *
4916 * Minimum length is 61 bytes.
4917 */
4918 if( in_len < 61 ||
4919 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4920 in[3] != 0 || in[4] != 0 ||
4921 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4922 {
4923 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4924 }
4925
4926 sid_len = in[59];
4927 if( sid_len > in_len - 61 )
4928 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4929
4930 cookie_len = in[60 + sid_len];
4931 if( cookie_len > in_len - 60 )
4932 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4933
4934 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4935 cli_id, cli_id_len ) == 0 )
4936 {
4937 /* Valid cookie */
4938 return( 0 );
4939 }
4940
4941 /*
4942 * If we get here, we've got an invalid cookie, let's prepare HVR.
4943 *
4944 * 0-0 ContentType type; copied
4945 * 1-2 ProtocolVersion version; copied
4946 * 3-4 uint16 epoch; copied
4947 * 5-10 uint48 sequence_number; copied
4948 * 11-12 uint16 length; olen - 13
4949 *
4950 * 13-13 HandshakeType msg_type; hello_verify_request
4951 * 14-16 uint24 length; olen - 25
4952 * 17-18 uint16 message_seq; copied
4953 * 19-21 uint24 fragment_offset; copied
4954 * 22-24 uint24 fragment_length; olen - 25
4955 *
4956 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4957 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4958 *
4959 * Minimum length is 28.
4960 */
4961 if( buf_len < 28 )
4962 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4963
4964 /* Copy most fields and adapt others */
4965 memcpy( obuf, in, 25 );
4966 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4967 obuf[25] = 0xfe;
4968 obuf[26] = 0xff;
4969
4970 /* Generate and write actual cookie */
4971 p = obuf + 28;
4972 if( f_cookie_write( p_cookie,
4973 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4974 {
4975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4976 }
4977
4978 *olen = p - obuf;
4979
4980 /* Go back and fill length fields */
4981 obuf[27] = (unsigned char)( *olen - 28 );
4982
4983 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4984 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4985 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4986
4987 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4988 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4989
4990 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4991}
4992
4993/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004994 * Handle possible client reconnect with the same UDP quadruplet
4995 * (RFC 6347 Section 4.2.8).
4996 *
4997 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4998 * that looks like a ClientHello.
4999 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005000 * - if the input looks like a ClientHello without cookies,
5001 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005002 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005003 * - if the input looks like a ClientHello with a valid cookie,
5004 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005005 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005006 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005007 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005008 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005009 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5010 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005011 */
5012static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5013{
5014 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005015 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005016
Hanno Becker2fddd372019-07-10 14:37:41 +01005017 if( ssl->conf->f_cookie_write == NULL ||
5018 ssl->conf->f_cookie_check == NULL )
5019 {
5020 /* If we can't use cookies to verify reachability of the peer,
5021 * drop the record. */
5022 return( 0 );
5023 }
5024
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005025 ret = ssl_check_dtls_clihlo_cookie(
5026 ssl->conf->f_cookie_write,
5027 ssl->conf->f_cookie_check,
5028 ssl->conf->p_cookie,
5029 ssl->cli_id, ssl->cli_id_len,
5030 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005031 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005032
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005033 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5034
5035 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005036 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005037 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005038 * If the error is permanent we'll catch it later,
5039 * if it's not, then hopefully it'll work next time. */
5040 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01005041 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005042 }
5043
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005044 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005045 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005046 /* Got a valid cookie, partially reset context */
5047 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5048 {
5049 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5050 return( ret );
5051 }
5052
5053 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005054 }
5055
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005056 return( ret );
5057}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005058#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005059
Hanno Beckerf661c9c2019-05-03 13:25:54 +01005060static int ssl_check_record_type( uint8_t record_type )
5061{
5062 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5063 record_type != MBEDTLS_SSL_MSG_ALERT &&
5064 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5065 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5066 {
5067 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5068 }
5069
5070 return( 0 );
5071}
5072
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005073/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005074 * ContentType type;
5075 * ProtocolVersion version;
5076 * uint16 epoch; // DTLS only
5077 * uint48 sequence_number; // DTLS only
5078 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005079 *
5080 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005081 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005082 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5083 *
5084 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005085 * 1. proceed with the record if this function returns 0
5086 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5087 * 3. return CLIENT_RECONNECT if this function return that value
5088 * 4. drop the whole datagram if this function returns anything else.
5089 * Point 2 is needed when the peer is resending, and we have already received
5090 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005091 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005092static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005093 unsigned char *buf,
5094 size_t len,
5095 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005096{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005097 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005098
Hanno Beckere5e7e782019-07-11 12:29:35 +01005099 size_t const rec_hdr_type_offset = 0;
5100 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005101
Hanno Beckere5e7e782019-07-11 12:29:35 +01005102 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5103 rec_hdr_type_len;
5104 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005105
Hanno Beckere5e7e782019-07-11 12:29:35 +01005106 size_t const rec_hdr_ctr_len = 8;
5107#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005108 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005109 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5110 rec_hdr_version_len;
5111
Hanno Beckera0e20d02019-05-15 14:03:01 +01005112#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005113 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5114 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005115 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005116#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5117#endif /* MBEDTLS_SSL_PROTO_DTLS */
5118
5119 size_t rec_hdr_len_offset; /* To be determined */
5120 size_t const rec_hdr_len_len = 2;
5121
5122 /*
5123 * Check minimum lengths for record header.
5124 */
5125
5126#if defined(MBEDTLS_SSL_PROTO_DTLS)
5127 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5128 {
5129 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5130 }
5131 else
5132#endif /* MBEDTLS_SSL_PROTO_DTLS */
5133 {
5134 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5135 }
5136
5137 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5138 {
5139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5140 (unsigned) len,
5141 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5142 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5143 }
5144
5145 /*
5146 * Parse and validate record content type
5147 */
5148
5149 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005150
5151 /* Check record content type */
5152#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5153 rec->cid_len = 0;
5154
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005155 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005156 ssl->conf->cid_len != 0 &&
5157 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005158 {
5159 /* Shift pointers to account for record header including CID
5160 * struct {
5161 * ContentType special_type = tls12_cid;
5162 * ProtocolVersion version;
5163 * uint16 epoch;
5164 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005165 * opaque cid[cid_length]; // Additional field compared to
5166 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005167 * uint16 length;
5168 * opaque enc_content[DTLSCiphertext.length];
5169 * } DTLSCiphertext;
5170 */
5171
5172 /* So far, we only support static CID lengths
5173 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005174 rec_hdr_cid_len = ssl->conf->cid_len;
5175 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005176
Hanno Beckere5e7e782019-07-11 12:29:35 +01005177 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005178 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5180 (unsigned) len,
5181 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005182 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005183 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005184
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005185 /* configured CID len is guaranteed at most 255, see
5186 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5187 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005188 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005189 }
5190 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005191#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005192 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005193 if( ssl_check_record_type( rec->type ) )
5194 {
Hanno Becker54229812019-07-12 14:40:00 +01005195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5196 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005197 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5198 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005199 }
5200
Hanno Beckere5e7e782019-07-11 12:29:35 +01005201 /*
5202 * Parse and validate record version
5203 */
5204
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005205 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5206 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005207 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5208 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005209 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005210
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005211 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5214 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005215 }
5216
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005217 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5220 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005221 }
5222
Hanno Beckere5e7e782019-07-11 12:29:35 +01005223 /*
5224 * Parse/Copy record sequence number.
5225 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005226
Hanno Beckere5e7e782019-07-11 12:29:35 +01005227#if defined(MBEDTLS_SSL_PROTO_DTLS)
5228 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005229 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005230 /* Copy explicit record sequence number from input buffer. */
5231 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5232 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005233 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005234 else
5235#endif /* MBEDTLS_SSL_PROTO_DTLS */
5236 {
5237 /* Copy implicit record sequence number from SSL context structure. */
5238 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5239 }
Paul Bakker40e46942009-01-03 21:51:57 +00005240
Hanno Beckere5e7e782019-07-11 12:29:35 +01005241 /*
5242 * Parse record length.
5243 */
5244
Hanno Beckere5e7e782019-07-11 12:29:35 +01005245 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005246 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5247 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005248 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005249
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005251 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005252 rec->type,
5253 major_ver, minor_ver, rec->data_len ) );
5254
5255 rec->buf = buf;
5256 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005257
Hanno Beckerd417cc92019-07-26 08:20:27 +01005258 if( rec->data_len == 0 )
5259 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005260
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005261 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005262 * DTLS-related tests.
5263 * Check epoch before checking length constraint because
5264 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5265 * message gets duplicated before the corresponding Finished message,
5266 * the second ChangeCipherSpec should be discarded because it belongs
5267 * to an old epoch, but not because its length is shorter than
5268 * the minimum record length for packets using the new record transform.
5269 * Note that these two kinds of failures are handled differently,
5270 * as an unexpected record is silently skipped but an invalid
5271 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005272 */
5273#if defined(MBEDTLS_SSL_PROTO_DTLS)
5274 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5275 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005276 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005277
Hanno Becker955a5c92019-07-10 17:12:07 +01005278 /* Check that the datagram is large enough to contain a record
5279 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005280 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005281 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5283 (unsigned) len,
5284 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005285 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5286 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005287
Hanno Becker37cfe732019-07-10 17:20:01 +01005288 /* Records from other, non-matching epochs are silently discarded.
5289 * (The case of same-port Client reconnects must be considered in
5290 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005291 if( rec_epoch != ssl->in_epoch )
5292 {
5293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5294 "expected %d, received %d",
5295 ssl->in_epoch, rec_epoch ) );
5296
Hanno Becker552f7472019-07-19 10:59:12 +01005297 /* Records from the next epoch are considered for buffering
5298 * (concretely: early Finished messages). */
5299 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005300 {
Hanno Becker552f7472019-07-19 10:59:12 +01005301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5302 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005303 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005304
Hanno Becker2fddd372019-07-10 14:37:41 +01005305 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005306 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005307#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005308 /* For records from the correct epoch, check whether their
5309 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005310 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005311 {
5312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5313 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5314 }
5315#endif
5316 }
5317#endif /* MBEDTLS_SSL_PROTO_DTLS */
5318
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005319 return( 0 );
5320}
Paul Bakker5121ce52009-01-03 21:22:43 +00005321
Paul Bakker5121ce52009-01-03 21:22:43 +00005322
Hanno Becker2fddd372019-07-10 14:37:41 +01005323#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5324static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5325{
5326 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5327
5328 /*
5329 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5330 * access the first byte of record content (handshake type), as we
5331 * have an active transform (possibly iv_len != 0), so use the
5332 * fact that the record header len is 13 instead.
5333 */
5334 if( rec_epoch == 0 &&
5335 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5336 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5337 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5338 ssl->in_left > 13 &&
5339 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5340 {
5341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5342 "from the same port" ) );
5343 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005344 }
5345
5346 return( 0 );
5347}
Hanno Becker2fddd372019-07-10 14:37:41 +01005348#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005349
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005350/*
5351 * If applicable, decrypt (and decompress) record content
5352 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005353static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5354 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005355{
5356 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005359 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5362 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366 ret = mbedtls_ssl_hw_record_read( ssl );
5367 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5370 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005371 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005372
5373 if( ret == 0 )
5374 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005375 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005377 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005378 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005379 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005380
Hanno Beckera18d1322018-01-03 14:27:32 +00005381 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005382 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005385
Hanno Beckera0e20d02019-05-15 14:03:01 +01005386#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005387 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5388 ssl->conf->ignore_unexpected_cid
5389 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5390 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005391 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005392 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005393 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005394#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005395
Paul Bakker5121ce52009-01-03 21:22:43 +00005396 return( ret );
5397 }
5398
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005399 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005400 {
5401 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005402 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005403 }
5404
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005405 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005406 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005407
Hanno Beckera0e20d02019-05-15 14:03:01 +01005408#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005409 /* We have already checked the record content type
5410 * in ssl_parse_record_header(), failing or silently
5411 * dropping the record in the case of an unknown type.
5412 *
5413 * Since with the use of CIDs, the record content type
5414 * might change during decryption, re-check the record
5415 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005416 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005417 {
5418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5419 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5420 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005421#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005422
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005423 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005424 {
5425#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5426 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005427 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005428 {
5429 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5431 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5432 }
5433#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5434
5435 ssl->nb_zero++;
5436
5437 /*
5438 * Three or more empty messages may be a DoS attack
5439 * (excessive CPU consumption).
5440 */
5441 if( ssl->nb_zero > 3 )
5442 {
5443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005444 "messages, possible DoS attack" ) );
5445 /* Treat the records as if they were not properly authenticated,
5446 * thereby failing the connection if we see more than allowed
5447 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005448 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5449 }
5450 }
5451 else
5452 ssl->nb_zero = 0;
5453
5454#if defined(MBEDTLS_SSL_PROTO_DTLS)
5455 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5456 {
5457 ; /* in_ctr read from peer, not maintained internally */
5458 }
5459 else
5460#endif
5461 {
5462 unsigned i;
5463 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5464 if( ++ssl->in_ctr[i - 1] != 0 )
5465 break;
5466
5467 /* The loop goes to its end iff the counter is wrapping */
5468 if( i == ssl_ep_len( ssl ) )
5469 {
5470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5471 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5472 }
5473 }
5474
Paul Bakker5121ce52009-01-03 21:22:43 +00005475 }
5476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005478 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005480 {
5481 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005484 return( ret );
5485 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005486 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005490 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005493 }
5494#endif
5495
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005496 /* Check actual (decrypted) record content length against
5497 * configured maximum. */
5498 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5499 {
5500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5501 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5502 }
5503
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005504 return( 0 );
5505}
5506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005508
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005509/*
5510 * Read a record.
5511 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005512 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5513 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5514 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005515 */
Hanno Becker1097b342018-08-15 14:09:41 +01005516
5517/* Helper functions for mbedtls_ssl_read_record(). */
5518static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005519static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5520static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005521
Hanno Becker327c93b2018-08-15 13:56:18 +01005522int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005523 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005524{
5525 int ret;
5526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005528
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005529 if( ssl->keep_current_message == 0 )
5530 {
5531 do {
Simon Butcher99000142016-10-13 17:21:01 +01005532
Hanno Becker26994592018-08-15 14:14:59 +01005533 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005534 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005535 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005536
Hanno Beckere74d5562018-08-15 14:26:08 +01005537 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005538 {
Hanno Becker40f50842018-08-15 14:48:01 +01005539#if defined(MBEDTLS_SSL_PROTO_DTLS)
5540 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005541
Hanno Becker40f50842018-08-15 14:48:01 +01005542 /* We only check for buffered messages if the
5543 * current datagram is fully consumed. */
5544 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005545 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005546 {
Hanno Becker40f50842018-08-15 14:48:01 +01005547 if( ssl_load_buffered_message( ssl ) == 0 )
5548 have_buffered = 1;
5549 }
5550
5551 if( have_buffered == 0 )
5552#endif /* MBEDTLS_SSL_PROTO_DTLS */
5553 {
5554 ret = ssl_get_next_record( ssl );
5555 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5556 continue;
5557
5558 if( ret != 0 )
5559 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005560 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005561 return( ret );
5562 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005563 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005564 }
5565
5566 ret = mbedtls_ssl_handle_message_type( ssl );
5567
Hanno Becker40f50842018-08-15 14:48:01 +01005568#if defined(MBEDTLS_SSL_PROTO_DTLS)
5569 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5570 {
5571 /* Buffer future message */
5572 ret = ssl_buffer_message( ssl );
5573 if( ret != 0 )
5574 return( ret );
5575
5576 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5577 }
5578#endif /* MBEDTLS_SSL_PROTO_DTLS */
5579
Hanno Becker90333da2017-10-10 11:27:13 +01005580 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5581 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005582
5583 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005584 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005585 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005586 return( ret );
5587 }
5588
Hanno Becker327c93b2018-08-15 13:56:18 +01005589 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005590 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005591 {
5592 mbedtls_ssl_update_handshake_status( ssl );
5593 }
Simon Butcher99000142016-10-13 17:21:01 +01005594 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005595 else
Simon Butcher99000142016-10-13 17:21:01 +01005596 {
Hanno Becker02f59072018-08-15 14:00:24 +01005597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005598 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005599 }
5600
5601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5602
5603 return( 0 );
5604}
5605
Hanno Becker40f50842018-08-15 14:48:01 +01005606#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005607static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005608{
Hanno Becker40f50842018-08-15 14:48:01 +01005609 if( ssl->in_left > ssl->next_record_offset )
5610 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005611
Hanno Becker40f50842018-08-15 14:48:01 +01005612 return( 0 );
5613}
5614
5615static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5616{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005617 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005618 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005619 int ret = 0;
5620
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005621 if( hs == NULL )
5622 return( -1 );
5623
Hanno Beckere00ae372018-08-20 09:39:42 +01005624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5625
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005626 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5627 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5628 {
5629 /* Check if we have seen a ChangeCipherSpec before.
5630 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005631 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005632 {
5633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5634 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005635 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005636 }
5637
Hanno Becker39b8bc92018-08-28 17:17:13 +01005638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005639 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5640 ssl->in_msglen = 1;
5641 ssl->in_msg[0] = 1;
5642
5643 /* As long as they are equal, the exact value doesn't matter. */
5644 ssl->in_left = 0;
5645 ssl->next_record_offset = 0;
5646
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005647 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005648 goto exit;
5649 }
Hanno Becker37f95322018-08-16 13:55:32 +01005650
Hanno Beckerb8f50142018-08-28 10:01:34 +01005651#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005652 /* Debug only */
5653 {
5654 unsigned offset;
5655 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5656 {
5657 hs_buf = &hs->buffering.hs[offset];
5658 if( hs_buf->is_valid == 1 )
5659 {
5660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5661 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005662 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005663 }
5664 }
5665 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005666#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005667
5668 /* Check if we have buffered and/or fully reassembled the
5669 * next handshake message. */
5670 hs_buf = &hs->buffering.hs[0];
5671 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5672 {
5673 /* Synthesize a record containing the buffered HS message. */
5674 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5675 ( hs_buf->data[2] << 8 ) |
5676 hs_buf->data[3];
5677
5678 /* Double-check that we haven't accidentally buffered
5679 * a message that doesn't fit into the input buffer. */
5680 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5681 {
5682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5684 }
5685
5686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5687 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5688 hs_buf->data, msg_len + 12 );
5689
5690 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5691 ssl->in_hslen = msg_len + 12;
5692 ssl->in_msglen = msg_len + 12;
5693 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5694
5695 ret = 0;
5696 goto exit;
5697 }
5698 else
5699 {
5700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5701 hs->in_msg_seq ) );
5702 }
5703
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005704 ret = -1;
5705
5706exit:
5707
5708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5709 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005710}
5711
Hanno Beckera02b0b42018-08-21 17:20:27 +01005712static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5713 size_t desired )
5714{
5715 int offset;
5716 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5718 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005719
Hanno Becker01315ea2018-08-21 17:22:17 +01005720 /* Get rid of future records epoch first, if such exist. */
5721 ssl_free_buffered_record( ssl );
5722
5723 /* Check if we have enough space available now. */
5724 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5725 hs->buffering.total_bytes_buffered ) )
5726 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005728 return( 0 );
5729 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005730
Hanno Becker4f432ad2018-08-28 10:02:32 +01005731 /* We don't have enough space to buffer the next expected handshake
5732 * message. Remove buffers used for future messages to gain space,
5733 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005734 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5735 offset >= 0; offset-- )
5736 {
5737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5738 offset ) );
5739
Hanno Beckerb309b922018-08-23 13:18:05 +01005740 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005741
5742 /* Check if we have enough space available now. */
5743 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5744 hs->buffering.total_bytes_buffered ) )
5745 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005747 return( 0 );
5748 }
5749 }
5750
5751 return( -1 );
5752}
5753
Hanno Becker40f50842018-08-15 14:48:01 +01005754static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5755{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005756 int ret = 0;
5757 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5758
5759 if( hs == NULL )
5760 return( 0 );
5761
5762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5763
5764 switch( ssl->in_msgtype )
5765 {
5766 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005768
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005769 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005770 break;
5771
5772 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005773 {
5774 unsigned recv_msg_seq_offset;
5775 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5776 mbedtls_ssl_hs_buffer *hs_buf;
5777 size_t msg_len = ssl->in_hslen - 12;
5778
5779 /* We should never receive an old handshake
5780 * message - double-check nonetheless. */
5781 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5782 {
5783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5785 }
5786
5787 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5788 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5789 {
5790 /* Silently ignore -- message too far in the future */
5791 MBEDTLS_SSL_DEBUG_MSG( 2,
5792 ( "Ignore future HS message with sequence number %u, "
5793 "buffering window %u - %u",
5794 recv_msg_seq, ssl->handshake->in_msg_seq,
5795 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5796
5797 goto exit;
5798 }
5799
5800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5801 recv_msg_seq, recv_msg_seq_offset ) );
5802
5803 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5804
5805 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005806 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005807 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005808 size_t reassembly_buf_sz;
5809
Hanno Becker37f95322018-08-16 13:55:32 +01005810 hs_buf->is_fragmented =
5811 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5812
5813 /* We copy the message back into the input buffer
5814 * after reassembly, so check that it's not too large.
5815 * This is an implementation-specific limitation
5816 * and not one from the standard, hence it is not
5817 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005818 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005819 {
5820 /* Ignore message */
5821 goto exit;
5822 }
5823
Hanno Beckere0b150f2018-08-21 15:51:03 +01005824 /* Check if we have enough space to buffer the message. */
5825 if( hs->buffering.total_bytes_buffered >
5826 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5827 {
5828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5829 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5830 }
5831
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005832 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5833 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005834
5835 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5836 hs->buffering.total_bytes_buffered ) )
5837 {
5838 if( recv_msg_seq_offset > 0 )
5839 {
5840 /* If we can't buffer a future message because
5841 * of space limitations -- ignore. */
5842 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",
5843 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5844 (unsigned) hs->buffering.total_bytes_buffered ) );
5845 goto exit;
5846 }
Hanno Beckere1801392018-08-21 16:51:05 +01005847 else
5848 {
5849 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",
5850 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5851 (unsigned) hs->buffering.total_bytes_buffered ) );
5852 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005853
Hanno Beckera02b0b42018-08-21 17:20:27 +01005854 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005855 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005856 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",
5857 (unsigned) msg_len,
5858 (unsigned) reassembly_buf_sz,
5859 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005860 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005861 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5862 goto exit;
5863 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005864 }
5865
5866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5867 msg_len ) );
5868
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005869 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5870 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005871 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005872 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005873 goto exit;
5874 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005875 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005876
5877 /* Prepare final header: copy msg_type, length and message_seq,
5878 * then add standardised fragment_offset and fragment_length */
5879 memcpy( hs_buf->data, ssl->in_msg, 6 );
5880 memset( hs_buf->data + 6, 0, 3 );
5881 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5882
5883 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005884
5885 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005886 }
5887 else
5888 {
5889 /* Make sure msg_type and length are consistent */
5890 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5891 {
5892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5893 /* Ignore */
5894 goto exit;
5895 }
5896 }
5897
Hanno Becker4422bbb2018-08-20 09:40:19 +01005898 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005899 {
5900 size_t frag_len, frag_off;
5901 unsigned char * const msg = hs_buf->data + 12;
5902
5903 /*
5904 * Check and copy current fragment
5905 */
5906
5907 /* Validation of header fields already done in
5908 * mbedtls_ssl_prepare_handshake_record(). */
5909 frag_off = ssl_get_hs_frag_off( ssl );
5910 frag_len = ssl_get_hs_frag_len( ssl );
5911
5912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5913 frag_off, frag_len ) );
5914 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5915
5916 if( hs_buf->is_fragmented )
5917 {
5918 unsigned char * const bitmask = msg + msg_len;
5919 ssl_bitmask_set( bitmask, frag_off, frag_len );
5920 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5921 msg_len ) == 0 );
5922 }
5923 else
5924 {
5925 hs_buf->is_complete = 1;
5926 }
5927
5928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5929 hs_buf->is_complete ? "" : "not yet " ) );
5930 }
5931
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005932 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005933 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005934
5935 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005936 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005937 break;
5938 }
5939
5940exit:
5941
5942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5943 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005944}
5945#endif /* MBEDTLS_SSL_PROTO_DTLS */
5946
Hanno Becker1097b342018-08-15 14:09:41 +01005947static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005948{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005949 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005950 * Consume last content-layer message and potentially
5951 * update in_msglen which keeps track of the contents'
5952 * consumption state.
5953 *
5954 * (1) Handshake messages:
5955 * Remove last handshake message, move content
5956 * and adapt in_msglen.
5957 *
5958 * (2) Alert messages:
5959 * Consume whole record content, in_msglen = 0.
5960 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005961 * (3) Change cipher spec:
5962 * Consume whole record content, in_msglen = 0.
5963 *
5964 * (4) Application data:
5965 * Don't do anything - the record layer provides
5966 * the application data as a stream transport
5967 * and consumes through mbedtls_ssl_read only.
5968 *
5969 */
5970
5971 /* Case (1): Handshake messages */
5972 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005973 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005974 /* Hard assertion to be sure that no application data
5975 * is in flight, as corrupting ssl->in_msglen during
5976 * ssl->in_offt != NULL is fatal. */
5977 if( ssl->in_offt != NULL )
5978 {
5979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5981 }
5982
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005983 /*
5984 * Get next Handshake message in the current record
5985 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005986
Hanno Becker4a810fb2017-05-24 16:27:30 +01005987 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005988 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005989 * current handshake content: If DTLS handshake
5990 * fragmentation is used, that's the fragment
5991 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005992 * size here is faulty and should be changed at
5993 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005994 * (2) While it doesn't seem to cause problems, one
5995 * has to be very careful not to assume that in_hslen
5996 * is always <= in_msglen in a sensible communication.
5997 * Again, it's wrong for DTLS handshake fragmentation.
5998 * The following check is therefore mandatory, and
5999 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006000 * Additionally, ssl->in_hslen might be arbitrarily out of
6001 * bounds after handling a DTLS message with an unexpected
6002 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006003 */
6004 if( ssl->in_hslen < ssl->in_msglen )
6005 {
6006 ssl->in_msglen -= ssl->in_hslen;
6007 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6008 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006009
Hanno Becker4a810fb2017-05-24 16:27:30 +01006010 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6011 ssl->in_msg, ssl->in_msglen );
6012 }
6013 else
6014 {
6015 ssl->in_msglen = 0;
6016 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006017
Hanno Becker4a810fb2017-05-24 16:27:30 +01006018 ssl->in_hslen = 0;
6019 }
6020 /* Case (4): Application data */
6021 else if( ssl->in_offt != NULL )
6022 {
6023 return( 0 );
6024 }
6025 /* Everything else (CCS & Alerts) */
6026 else
6027 {
6028 ssl->in_msglen = 0;
6029 }
6030
Hanno Becker1097b342018-08-15 14:09:41 +01006031 return( 0 );
6032}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006033
Hanno Beckere74d5562018-08-15 14:26:08 +01006034static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6035{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006036 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006037 return( 1 );
6038
6039 return( 0 );
6040}
6041
Hanno Becker5f066e72018-08-16 14:56:31 +01006042#if defined(MBEDTLS_SSL_PROTO_DTLS)
6043
6044static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6045{
6046 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6047 if( hs == NULL )
6048 return;
6049
Hanno Becker01315ea2018-08-21 17:22:17 +01006050 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006051 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006052 hs->buffering.total_bytes_buffered -=
6053 hs->buffering.future_record.len;
6054
6055 mbedtls_free( hs->buffering.future_record.data );
6056 hs->buffering.future_record.data = NULL;
6057 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006058}
6059
6060static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6061{
6062 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6063 unsigned char * rec;
6064 size_t rec_len;
6065 unsigned rec_epoch;
6066
6067 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6068 return( 0 );
6069
6070 if( hs == NULL )
6071 return( 0 );
6072
Hanno Becker5f066e72018-08-16 14:56:31 +01006073 rec = hs->buffering.future_record.data;
6074 rec_len = hs->buffering.future_record.len;
6075 rec_epoch = hs->buffering.future_record.epoch;
6076
6077 if( rec == NULL )
6078 return( 0 );
6079
Hanno Becker4cb782d2018-08-20 11:19:05 +01006080 /* Only consider loading future records if the
6081 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006082 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006083 return( 0 );
6084
Hanno Becker5f066e72018-08-16 14:56:31 +01006085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6086
6087 if( rec_epoch != ssl->in_epoch )
6088 {
6089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6090 goto exit;
6091 }
6092
6093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6094
6095 /* Double-check that the record is not too large */
6096 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6097 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6098 {
6099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6100 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6101 }
6102
6103 memcpy( ssl->in_hdr, rec, rec_len );
6104 ssl->in_left = rec_len;
6105 ssl->next_record_offset = 0;
6106
6107 ssl_free_buffered_record( ssl );
6108
6109exit:
6110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6111 return( 0 );
6112}
6113
Hanno Becker519f15d2019-07-11 12:43:20 +01006114static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6115 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006116{
6117 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006118
6119 /* Don't buffer future records outside handshakes. */
6120 if( hs == NULL )
6121 return( 0 );
6122
6123 /* Only buffer handshake records (we are only interested
6124 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006125 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006126 return( 0 );
6127
6128 /* Don't buffer more than one future epoch record. */
6129 if( hs->buffering.future_record.data != NULL )
6130 return( 0 );
6131
Hanno Becker01315ea2018-08-21 17:22:17 +01006132 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006133 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006134 hs->buffering.total_bytes_buffered ) )
6135 {
6136 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 +01006137 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006138 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006139 return( 0 );
6140 }
6141
Hanno Becker5f066e72018-08-16 14:56:31 +01006142 /* Buffer record */
6143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6144 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006145 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006146
6147 /* ssl_parse_record_header() only considers records
6148 * of the next epoch as candidates for buffering. */
6149 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006150 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006151
6152 hs->buffering.future_record.data =
6153 mbedtls_calloc( 1, hs->buffering.future_record.len );
6154 if( hs->buffering.future_record.data == NULL )
6155 {
6156 /* If we run out of RAM trying to buffer a
6157 * record from the next epoch, just ignore. */
6158 return( 0 );
6159 }
6160
Hanno Becker519f15d2019-07-11 12:43:20 +01006161 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006162
Hanno Becker519f15d2019-07-11 12:43:20 +01006163 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006164 return( 0 );
6165}
6166
6167#endif /* MBEDTLS_SSL_PROTO_DTLS */
6168
Hanno Beckere74d5562018-08-15 14:26:08 +01006169static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006170{
6171 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006172 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006173
Hanno Becker5f066e72018-08-16 14:56:31 +01006174#if defined(MBEDTLS_SSL_PROTO_DTLS)
6175 /* We might have buffered a future record; if so,
6176 * and if the epoch matches now, load it.
6177 * On success, this call will set ssl->in_left to
6178 * the length of the buffered record, so that
6179 * the calls to ssl_fetch_input() below will
6180 * essentially be no-ops. */
6181 ret = ssl_load_buffered_record( ssl );
6182 if( ret != 0 )
6183 return( ret );
6184#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006185
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006186 /* Ensure that we have enough space available for the default form
6187 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6188 * with no space for CIDs counted in). */
6189 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6190 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006193 return( ret );
6194 }
6195
Hanno Beckere5e7e782019-07-11 12:29:35 +01006196 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6197 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006200 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006201 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006202 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6203 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006204 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006205 if( ret != 0 )
6206 return( ret );
6207
6208 /* Fall through to handling of unexpected records */
6209 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6210 }
6211
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006212 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6213 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006214#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006215 /* Reset in pointers to default state for TLS/DTLS records,
6216 * assuming no CID and no offset between record content and
6217 * record plaintext. */
6218 ssl_update_in_pointers( ssl );
6219
Hanno Becker7ae20e02019-07-12 08:33:49 +01006220 /* Setup internal message pointers from record structure. */
6221 ssl->in_msgtype = rec.type;
6222#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6223 ssl->in_len = ssl->in_cid + rec.cid_len;
6224#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6225 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6226 ssl->in_msglen = rec.data_len;
6227
Hanno Becker2fddd372019-07-10 14:37:41 +01006228 ret = ssl_check_client_reconnect( ssl );
6229 if( ret != 0 )
6230 return( ret );
6231#endif
6232
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006233 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006234 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006235
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6237 "(header)" ) );
6238 }
6239 else
6240 {
6241 /* Skip invalid record and the rest of the datagram */
6242 ssl->next_record_offset = 0;
6243 ssl->in_left = 0;
6244
6245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6246 "(header)" ) );
6247 }
6248
6249 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006250 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006251 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006252 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006253#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006254 {
6255 return( ret );
6256 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006257 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006260 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006261 {
Hanno Beckera8814792019-07-10 15:01:45 +01006262 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006263 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006264 if( ssl->next_record_offset < ssl->in_left )
6265 {
6266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6267 }
6268 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006269 else
6270#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006271 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006272 /*
6273 * Fetch record contents from underlying transport.
6274 */
Hanno Beckera3175662019-07-11 12:50:29 +01006275 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006276 if( ret != 0 )
6277 {
6278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6279 return( ret );
6280 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006281
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006282 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006283 }
6284
6285 /*
6286 * Decrypt record contents.
6287 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006288
Hanno Beckerfdf66042019-07-11 13:07:45 +01006289 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006292 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006293 {
6294 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006295 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006296 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006297 /* Except when waiting for Finished as a bad mac here
6298 * probably means something went wrong in the handshake
6299 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6300 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6301 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6302 {
6303#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6304 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6305 {
6306 mbedtls_ssl_send_alert_message( ssl,
6307 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6308 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6309 }
6310#endif
6311 return( ret );
6312 }
6313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006315 if( ssl->conf->badmac_limit != 0 &&
6316 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6319 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006320 }
6321#endif
6322
Hanno Becker4a810fb2017-05-24 16:27:30 +01006323 /* As above, invalid records cause
6324 * dismissal of the whole datagram. */
6325
6326 ssl->next_record_offset = 0;
6327 ssl->in_left = 0;
6328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006330 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006331 }
6332
6333 return( ret );
6334 }
6335 else
6336#endif
6337 {
6338 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6340 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 mbedtls_ssl_send_alert_message( ssl,
6343 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6344 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006345 }
6346#endif
6347 return( ret );
6348 }
6349 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006350
Hanno Becker44d89b22019-07-12 09:40:44 +01006351
6352 /* Reset in pointers to default state for TLS/DTLS records,
6353 * assuming no CID and no offset between record content and
6354 * record plaintext. */
6355 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006356#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6357 ssl->in_len = ssl->in_cid + rec.cid_len;
6358#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6359 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006360
Hanno Becker8685c822019-07-12 09:37:30 +01006361 /* The record content type may change during decryption,
6362 * so re-read it. */
6363 ssl->in_msgtype = rec.type;
6364 /* Also update the input buffer, because unfortunately
6365 * the server-side ssl_parse_client_hello() reparses the
6366 * record header when receiving a ClientHello initiating
6367 * a renegotiation. */
6368 ssl->in_hdr[0] = rec.type;
6369 ssl->in_msg = rec.buf + rec.data_offset;
6370 ssl->in_msglen = rec.data_len;
6371 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6372 ssl->in_len[1] = (unsigned char)( rec.data_len );
6373
Simon Butcher99000142016-10-13 17:21:01 +01006374 return( 0 );
6375}
6376
6377int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6378{
6379 int ret;
6380
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006381 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006382 * Handle particular types of records
6383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006385 {
Simon Butcher99000142016-10-13 17:21:01 +01006386 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6387 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006388 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006389 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006390 }
6391
Hanno Beckere678eaa2018-08-21 14:57:46 +01006392 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006393 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006394 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006395 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6397 ssl->in_msglen ) );
6398 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006399 }
6400
Hanno Beckere678eaa2018-08-21 14:57:46 +01006401 if( ssl->in_msg[0] != 1 )
6402 {
6403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6404 ssl->in_msg[0] ) );
6405 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6406 }
6407
6408#if defined(MBEDTLS_SSL_PROTO_DTLS)
6409 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6410 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6411 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6412 {
6413 if( ssl->handshake == NULL )
6414 {
6415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6416 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6417 }
6418
6419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6420 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6421 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006422#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006423 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006426 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006427 if( ssl->in_msglen != 2 )
6428 {
6429 /* Note: Standard allows for more than one 2 byte alert
6430 to be packed in a single message, but Mbed TLS doesn't
6431 currently support this. */
6432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6433 ssl->in_msglen ) );
6434 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6435 }
6436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006438 ssl->in_msg[0], ssl->in_msg[1] ) );
6439
6440 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006441 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006446 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006448 }
6449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6451 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6454 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006455 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006456
6457#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6458 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6459 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6460 {
Hanno Becker90333da2017-10-10 11:27:13 +01006461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006462 /* Will be handled when trying to parse ServerHello */
6463 return( 0 );
6464 }
6465#endif
6466
6467#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6468 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6469 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6470 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6471 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6472 {
6473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6474 /* Will be handled in mbedtls_ssl_parse_certificate() */
6475 return( 0 );
6476 }
6477#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6478
6479 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006480 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006481 }
6482
Hanno Beckerc76c6192017-06-06 10:03:17 +01006483#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006484 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006485 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006486 /* Drop unexpected ApplicationData records,
6487 * except at the beginning of renegotiations */
6488 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6489 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6490#if defined(MBEDTLS_SSL_RENEGOTIATION)
6491 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6492 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006493#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006494 )
6495 {
6496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6497 return( MBEDTLS_ERR_SSL_NON_FATAL );
6498 }
6499
6500 if( ssl->handshake != NULL &&
6501 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6502 {
6503 ssl_handshake_wrapup_free_hs_transform( ssl );
6504 }
6505 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006506#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006507
Paul Bakker5121ce52009-01-03 21:22:43 +00006508 return( 0 );
6509}
6510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006512{
6513 int ret;
6514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6516 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6517 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006518 {
6519 return( ret );
6520 }
6521
6522 return( 0 );
6523}
6524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006526 unsigned char level,
6527 unsigned char message )
6528{
6529 int ret;
6530
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006531 if( ssl == NULL || ssl->conf == NULL )
6532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006535 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006538 ssl->out_msglen = 2;
6539 ssl->out_msg[0] = level;
6540 ssl->out_msg[1] = message;
6541
Hanno Becker67bc7c32018-08-06 11:33:50 +01006542 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006545 return( ret );
6546 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006548
6549 return( 0 );
6550}
6551
Hanno Beckerb9d44792019-02-08 07:19:04 +00006552#if defined(MBEDTLS_X509_CRT_PARSE_C)
6553static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6554{
6555#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6556 if( session->peer_cert != NULL )
6557 {
6558 mbedtls_x509_crt_free( session->peer_cert );
6559 mbedtls_free( session->peer_cert );
6560 session->peer_cert = NULL;
6561 }
6562#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6563 if( session->peer_cert_digest != NULL )
6564 {
6565 /* Zeroization is not necessary. */
6566 mbedtls_free( session->peer_cert_digest );
6567 session->peer_cert_digest = NULL;
6568 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6569 session->peer_cert_digest_len = 0;
6570 }
6571#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6572}
6573#endif /* MBEDTLS_X509_CRT_PARSE_C */
6574
Paul Bakker5121ce52009-01-03 21:22:43 +00006575/*
6576 * Handshake functions
6577 */
Hanno Becker21489932019-02-05 13:20:55 +00006578#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006579/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006581{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006582 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6583 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006586
Hanno Becker7177a882019-02-05 13:36:46 +00006587 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006590 ssl->state++;
6591 return( 0 );
6592 }
6593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006596}
6597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006599{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006600 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6601 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006604
Hanno Becker7177a882019-02-05 13:36:46 +00006605 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006608 ssl->state++;
6609 return( 0 );
6610 }
6611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006614}
Gilles Peskinef9828522017-05-03 12:28:43 +02006615
Hanno Becker21489932019-02-05 13:20:55 +00006616#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006617/* Some certificate support -> implement write and parse */
6618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006620{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006622 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006624 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6625 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006628
Hanno Becker7177a882019-02-05 13:36:46 +00006629 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006632 ssl->state++;
6633 return( 0 );
6634 }
6635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006637 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006638 {
6639 if( ssl->client_auth == 0 )
6640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006642 ssl->state++;
6643 return( 0 );
6644 }
6645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006647 /*
6648 * If using SSLv3 and got no cert, send an Alert message
6649 * (otherwise an empty Certificate message will be sent).
6650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6652 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006653 {
6654 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6656 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6657 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006660 goto write_msg;
6661 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006663 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664#endif /* MBEDTLS_SSL_CLI_C */
6665#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006666 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6671 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006672 }
6673 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006674#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006677
6678 /*
6679 * 0 . 0 handshake type
6680 * 1 . 3 handshake length
6681 * 4 . 6 length of all certs
6682 * 7 . 9 length of cert. 1
6683 * 10 . n-1 peer certificate
6684 * n . n+2 length of cert. 2
6685 * n+3 . ... upper level cert, etc.
6686 */
6687 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006689
Paul Bakker29087132010-03-21 21:03:34 +00006690 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006691 {
6692 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006693 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006696 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006698 }
6699
6700 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6701 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6702 ssl->out_msg[i + 2] = (unsigned char)( n );
6703
6704 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6705 i += n; crt = crt->next;
6706 }
6707
6708 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6709 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6710 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6711
6712 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6714 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006715
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006716#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006717write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006718#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006719
6720 ssl->state++;
6721
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006722 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006723 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006725 return( ret );
6726 }
6727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006729
Paul Bakkered27a042013-04-18 22:46:23 +02006730 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006731}
6732
Hanno Becker84879e32019-01-31 07:44:03 +00006733#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006734
6735#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006736static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6737 unsigned char *crt_buf,
6738 size_t crt_buf_len )
6739{
6740 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6741
6742 if( peer_crt == NULL )
6743 return( -1 );
6744
6745 if( peer_crt->raw.len != crt_buf_len )
6746 return( -1 );
6747
Hanno Becker46f34d02019-02-08 14:00:04 +00006748 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006749}
Hanno Becker177475a2019-02-05 17:02:46 +00006750#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6751static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6752 unsigned char *crt_buf,
6753 size_t crt_buf_len )
6754{
6755 int ret;
6756 unsigned char const * const peer_cert_digest =
6757 ssl->session->peer_cert_digest;
6758 mbedtls_md_type_t const peer_cert_digest_type =
6759 ssl->session->peer_cert_digest_type;
6760 mbedtls_md_info_t const * const digest_info =
6761 mbedtls_md_info_from_type( peer_cert_digest_type );
6762 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6763 size_t digest_len;
6764
6765 if( peer_cert_digest == NULL || digest_info == NULL )
6766 return( -1 );
6767
6768 digest_len = mbedtls_md_get_size( digest_info );
6769 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6770 return( -1 );
6771
6772 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6773 if( ret != 0 )
6774 return( -1 );
6775
6776 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6777}
6778#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006779#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006780
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006781/*
6782 * Once the certificate message is read, parse it into a cert chain and
6783 * perform basic checks, but leave actual verification to the caller
6784 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006785static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6786 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006787{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006788 int ret;
6789#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6790 int crt_cnt=0;
6791#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006792 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006793 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006798 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6799 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006801 }
6802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6804 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006807 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6808 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006810 }
6811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006813
Paul Bakker5121ce52009-01-03 21:22:43 +00006814 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006816 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006817 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006819 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006823 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6824 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006826 }
6827
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006828 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6829 i += 3;
6830
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006831 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006832 while( i < ssl->in_hslen )
6833 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006834 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006835 if ( i + 3 > ssl->in_hslen ) {
6836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006837 mbedtls_ssl_send_alert_message( ssl,
6838 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6839 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006840 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6841 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006842 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6843 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006844 if( ssl->in_msg[i] != 0 )
6845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006847 mbedtls_ssl_send_alert_message( ssl,
6848 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6849 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006851 }
6852
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006853 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006854 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6855 | (unsigned int) ssl->in_msg[i + 2];
6856 i += 3;
6857
6858 if( n < 128 || i + n > ssl->in_hslen )
6859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006861 mbedtls_ssl_send_alert_message( ssl,
6862 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6863 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006865 }
6866
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006867 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006868#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6869 if( crt_cnt++ == 0 &&
6870 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6871 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006872 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006873 /* During client-side renegotiation, check that the server's
6874 * end-CRTs hasn't changed compared to the initial handshake,
6875 * mitigating the triple handshake attack. On success, reuse
6876 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6878 if( ssl_check_peer_crt_unchanged( ssl,
6879 &ssl->in_msg[i],
6880 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006881 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6883 mbedtls_ssl_send_alert_message( ssl,
6884 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6885 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6886 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006887 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006888
6889 /* Now we can safely free the original chain. */
6890 ssl_clear_peer_cert( ssl->session );
6891 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006892#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6893
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006894 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006895#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006896 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006897#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006898 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006899 * it in-place from the input buffer instead of making a copy. */
6900 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6901#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006902 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006903 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006904 case 0: /*ok*/
6905 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6906 /* Ignore certificate with an unknown algorithm: maybe a
6907 prior certificate was already trusted. */
6908 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006909
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006910 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6911 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6912 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006913
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006914 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6915 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6916 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006917
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006918 default:
6919 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6920 crt_parse_der_failed:
6921 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6922 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6923 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006924 }
6925
6926 i += n;
6927 }
6928
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006929 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006930 return( 0 );
6931}
6932
Hanno Becker4a55f632019-02-05 12:49:06 +00006933#if defined(MBEDTLS_SSL_SRV_C)
6934static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6935{
6936 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6937 return( -1 );
6938
6939#if defined(MBEDTLS_SSL_PROTO_SSL3)
6940 /*
6941 * Check if the client sent an empty certificate
6942 */
6943 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6944 {
6945 if( ssl->in_msglen == 2 &&
6946 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6947 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6948 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6949 {
6950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6951 return( 0 );
6952 }
6953
6954 return( -1 );
6955 }
6956#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6957
6958#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6959 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6960 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6961 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6962 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6963 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6964 {
6965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6966 return( 0 );
6967 }
6968
6969 return( -1 );
6970#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6971 MBEDTLS_SSL_PROTO_TLS1_2 */
6972}
6973#endif /* MBEDTLS_SSL_SRV_C */
6974
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006975/* Check if a certificate message is expected.
6976 * Return either
6977 * - SSL_CERTIFICATE_EXPECTED, or
6978 * - SSL_CERTIFICATE_SKIP
6979 * indicating whether a Certificate message is expected or not.
6980 */
6981#define SSL_CERTIFICATE_EXPECTED 0
6982#define SSL_CERTIFICATE_SKIP 1
6983static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6984 int authmode )
6985{
6986 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006987 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006988
6989 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6990 return( SSL_CERTIFICATE_SKIP );
6991
6992#if defined(MBEDTLS_SSL_SRV_C)
6993 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6994 {
6995 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6996 return( SSL_CERTIFICATE_SKIP );
6997
6998 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6999 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007000 ssl->session_negotiate->verify_result =
7001 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7002 return( SSL_CERTIFICATE_SKIP );
7003 }
7004 }
Hanno Becker84d9d272019-03-01 08:10:46 +00007005#else
7006 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007007#endif /* MBEDTLS_SSL_SRV_C */
7008
7009 return( SSL_CERTIFICATE_EXPECTED );
7010}
7011
Hanno Becker68636192019-02-05 14:36:34 +00007012static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7013 int authmode,
7014 mbedtls_x509_crt *chain,
7015 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007016{
Hanno Becker6bdfab22019-02-05 13:11:17 +00007017 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007018 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00007019 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007020 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00007021
Hanno Becker8927c832019-04-03 12:52:50 +01007022 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7023 void *p_vrfy;
7024
Hanno Becker68636192019-02-05 14:36:34 +00007025 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7026 return( 0 );
7027
Hanno Becker8927c832019-04-03 12:52:50 +01007028 if( ssl->f_vrfy != NULL )
7029 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01007030 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01007031 f_vrfy = ssl->f_vrfy;
7032 p_vrfy = ssl->p_vrfy;
7033 }
7034 else
7035 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01007036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01007037 f_vrfy = ssl->conf->f_vrfy;
7038 p_vrfy = ssl->conf->p_vrfy;
7039 }
7040
Hanno Becker68636192019-02-05 14:36:34 +00007041 /*
7042 * Main check: verify certificate
7043 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007044#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7045 if( ssl->conf->f_ca_cb != NULL )
7046 {
7047 ((void) rs_ctx);
7048 have_ca_chain = 1;
7049
7050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03007051 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007052 chain,
7053 ssl->conf->f_ca_cb,
7054 ssl->conf->p_ca_cb,
7055 ssl->conf->cert_profile,
7056 ssl->hostname,
7057 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007058 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007059 }
7060 else
7061#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7062 {
7063 mbedtls_x509_crt *ca_chain;
7064 mbedtls_x509_crl *ca_crl;
7065
7066#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7067 if( ssl->handshake->sni_ca_chain != NULL )
7068 {
7069 ca_chain = ssl->handshake->sni_ca_chain;
7070 ca_crl = ssl->handshake->sni_ca_crl;
7071 }
7072 else
7073#endif
7074 {
7075 ca_chain = ssl->conf->ca_chain;
7076 ca_crl = ssl->conf->ca_crl;
7077 }
7078
7079 if( ca_chain != NULL )
7080 have_ca_chain = 1;
7081
7082 ret = mbedtls_x509_crt_verify_restartable(
7083 chain,
7084 ca_chain, ca_crl,
7085 ssl->conf->cert_profile,
7086 ssl->hostname,
7087 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007088 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007089 }
Hanno Becker68636192019-02-05 14:36:34 +00007090
7091 if( ret != 0 )
7092 {
7093 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7094 }
7095
7096#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7097 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7098 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7099#endif
7100
7101 /*
7102 * Secondary checks: always done, but change 'ret' only if it was 0
7103 */
7104
7105#if defined(MBEDTLS_ECP_C)
7106 {
7107 const mbedtls_pk_context *pk = &chain->pk;
7108
7109 /* If certificate uses an EC key, make sure the curve is OK */
7110 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7111 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7112 {
7113 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7114
7115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7116 if( ret == 0 )
7117 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7118 }
7119 }
7120#endif /* MBEDTLS_ECP_C */
7121
7122 if( mbedtls_ssl_check_cert_usage( chain,
7123 ciphersuite_info,
7124 ! ssl->conf->endpoint,
7125 &ssl->session_negotiate->verify_result ) != 0 )
7126 {
7127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7128 if( ret == 0 )
7129 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7130 }
7131
7132 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7133 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7134 * with details encoded in the verification flags. All other kinds
7135 * of error codes, including those from the user provided f_vrfy
7136 * functions, are treated as fatal and lead to a failure of
7137 * ssl_parse_certificate even if verification was optional. */
7138 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7139 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7140 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7141 {
7142 ret = 0;
7143 }
7144
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007145 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007146 {
7147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7148 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7149 }
7150
7151 if( ret != 0 )
7152 {
7153 uint8_t alert;
7154
7155 /* The certificate may have been rejected for several reasons.
7156 Pick one and send the corresponding alert. Which alert to send
7157 may be a subject of debate in some cases. */
7158 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7159 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7160 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7161 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7162 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7163 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7164 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7165 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7166 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7167 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7168 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7169 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7170 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7171 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7172 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7173 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7174 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7175 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7176 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7177 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7178 else
7179 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7180 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7181 alert );
7182 }
7183
7184#if defined(MBEDTLS_DEBUG_C)
7185 if( ssl->session_negotiate->verify_result != 0 )
7186 {
7187 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7188 ssl->session_negotiate->verify_result ) );
7189 }
7190 else
7191 {
7192 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7193 }
7194#endif /* MBEDTLS_DEBUG_C */
7195
7196 return( ret );
7197}
7198
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007199#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7200static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7201 unsigned char *start, size_t len )
7202{
7203 int ret;
7204 /* Remember digest of the peer's end-CRT. */
7205 ssl->session_negotiate->peer_cert_digest =
7206 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7207 if( ssl->session_negotiate->peer_cert_digest == NULL )
7208 {
7209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7210 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7211 mbedtls_ssl_send_alert_message( ssl,
7212 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7213 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7214
7215 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7216 }
7217
7218 ret = mbedtls_md( mbedtls_md_info_from_type(
7219 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7220 start, len,
7221 ssl->session_negotiate->peer_cert_digest );
7222
7223 ssl->session_negotiate->peer_cert_digest_type =
7224 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7225 ssl->session_negotiate->peer_cert_digest_len =
7226 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7227
7228 return( ret );
7229}
7230
7231static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7232 unsigned char *start, size_t len )
7233{
7234 unsigned char *end = start + len;
7235 int ret;
7236
7237 /* Make a copy of the peer's raw public key. */
7238 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7239 ret = mbedtls_pk_parse_subpubkey( &start, end,
7240 &ssl->handshake->peer_pubkey );
7241 if( ret != 0 )
7242 {
7243 /* We should have parsed the public key before. */
7244 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7245 }
7246
7247 return( 0 );
7248}
7249#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7250
Hanno Becker68636192019-02-05 14:36:34 +00007251int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7252{
7253 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007254 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007255#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7256 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7257 ? ssl->handshake->sni_authmode
7258 : ssl->conf->authmode;
7259#else
7260 const int authmode = ssl->conf->authmode;
7261#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007262 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007263 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007264
7265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7266
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007267 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7268 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007269 {
7270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007271 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007272 }
7273
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007274#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7275 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007276 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007277 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007278 chain = ssl->handshake->ecrs_peer_cert;
7279 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007280 goto crt_verify;
7281 }
7282#endif
7283
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007284 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007285 {
7286 /* mbedtls_ssl_read_record may have sent an alert already. We
7287 let it decide whether to alert. */
7288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007289 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007290 }
7291
Hanno Becker4a55f632019-02-05 12:49:06 +00007292#if defined(MBEDTLS_SSL_SRV_C)
7293 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7294 {
7295 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007296
7297 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007298 ret = 0;
7299 else
7300 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007301
Hanno Becker6bdfab22019-02-05 13:11:17 +00007302 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007303 }
7304#endif /* MBEDTLS_SSL_SRV_C */
7305
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007306 /* Clear existing peer CRT structure in case we tried to
7307 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007308 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007309
7310 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7311 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007312 {
7313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7314 sizeof( mbedtls_x509_crt ) ) );
7315 mbedtls_ssl_send_alert_message( ssl,
7316 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7317 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007318
Hanno Becker3dad3112019-02-05 17:19:52 +00007319 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7320 goto exit;
7321 }
7322 mbedtls_x509_crt_init( chain );
7323
7324 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007325 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007326 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007327
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007328#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7329 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007330 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007331
7332crt_verify:
7333 if( ssl->handshake->ecrs_enabled)
7334 rs_ctx = &ssl->handshake->ecrs_ctx;
7335#endif
7336
Hanno Becker68636192019-02-05 14:36:34 +00007337 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007338 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007339 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007340 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007341
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007342#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007343 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007344 unsigned char *crt_start, *pk_start;
7345 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007346
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007347 /* We parse the CRT chain without copying, so
7348 * these pointers point into the input buffer,
7349 * and are hence still valid after freeing the
7350 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007351
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007352 crt_start = chain->raw.p;
7353 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007354
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007355 pk_start = chain->pk_raw.p;
7356 pk_len = chain->pk_raw.len;
7357
7358 /* Free the CRT structures before computing
7359 * digest and copying the peer's public key. */
7360 mbedtls_x509_crt_free( chain );
7361 mbedtls_free( chain );
7362 chain = NULL;
7363
7364 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007365 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007366 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007367
7368 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7369 if( ret != 0 )
7370 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007371 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007372#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7373 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007374 ssl->session_negotiate->peer_cert = chain;
7375 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007376#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007379
Hanno Becker6bdfab22019-02-05 13:11:17 +00007380exit:
7381
Hanno Becker3dad3112019-02-05 17:19:52 +00007382 if( ret == 0 )
7383 ssl->state++;
7384
7385#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7386 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7387 {
7388 ssl->handshake->ecrs_peer_cert = chain;
7389 chain = NULL;
7390 }
7391#endif
7392
7393 if( chain != NULL )
7394 {
7395 mbedtls_x509_crt_free( chain );
7396 mbedtls_free( chain );
7397 }
7398
Paul Bakker5121ce52009-01-03 21:22:43 +00007399 return( ret );
7400}
Hanno Becker21489932019-02-05 13:20:55 +00007401#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007404{
7405 int ret;
7406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007410 ssl->out_msglen = 1;
7411 ssl->out_msg[0] = 1;
7412
Paul Bakker5121ce52009-01-03 21:22:43 +00007413 ssl->state++;
7414
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007415 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007416 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007418 return( ret );
7419 }
7420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007422
7423 return( 0 );
7424}
7425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007427{
7428 int ret;
7429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007431
Hanno Becker327c93b2018-08-15 13:56:18 +01007432 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007435 return( ret );
7436 }
7437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007441 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7442 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007444 }
7445
Hanno Beckere678eaa2018-08-21 14:57:46 +01007446 /* CCS records are only accepted if they have length 1 and content '1',
7447 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007448
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007449 /*
7450 * Switch to our negotiated transform and session parameters for inbound
7451 * data.
7452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007454 ssl->transform_in = ssl->transform_negotiate;
7455 ssl->session_in = ssl->session_negotiate;
7456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007458 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007461 ssl_dtls_replay_reset( ssl );
7462#endif
7463
7464 /* Increment epoch */
7465 if( ++ssl->in_epoch == 0 )
7466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007468 /* This is highly unlikely to happen for legitimate reasons, so
7469 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007471 }
7472 }
7473 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007475 memset( ssl->in_ctr, 0, 8 );
7476
Hanno Becker79594fd2019-05-08 09:38:41 +01007477 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7480 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007485 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7486 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007488 }
7489 }
7490#endif
7491
Paul Bakker5121ce52009-01-03 21:22:43 +00007492 ssl->state++;
7493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007495
7496 return( 0 );
7497}
7498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7500 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007501{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007502 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7505 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7506 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007507 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007508 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007509#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7511#if defined(MBEDTLS_SHA512_C)
7512 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007513 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7514 else
7515#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516#if defined(MBEDTLS_SHA256_C)
7517 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007518 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007519 else
7520#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007524 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007525 }
Paul Bakker380da532012-04-18 16:10:25 +00007526}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007529{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7531 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007532 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7533 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007534#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7536#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007537#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007538 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007539 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7540#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007541 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007542#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007543#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007545#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007546 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007547 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007548#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007549 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007550#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007551#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007552#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007553}
7554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007556 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007557{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7559 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007560 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7561 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007562#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7564#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007565#if defined(MBEDTLS_USE_PSA_CRYPTO)
7566 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7567#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007568 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007569#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007570#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007572#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007573 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007574#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007575 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007576#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007577#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007579}
7580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7582 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7583static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007584 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007585{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007586 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7587 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007588}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007589#endif
Paul Bakker380da532012-04-18 16:10:25 +00007590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7592#if defined(MBEDTLS_SHA256_C)
7593static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007594 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007595{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007596#if defined(MBEDTLS_USE_PSA_CRYPTO)
7597 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7598#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007599 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007600#endif
Paul Bakker380da532012-04-18 16:10:25 +00007601}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007602#endif
Paul Bakker380da532012-04-18 16:10:25 +00007603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604#if defined(MBEDTLS_SHA512_C)
7605static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007606 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007607{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007608#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007609 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007610#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007611 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007612#endif
Paul Bakker380da532012-04-18 16:10:25 +00007613}
Paul Bakker769075d2012-11-24 11:26:46 +01007614#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007618static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007620{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007621 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007622 mbedtls_md5_context md5;
7623 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007624
Paul Bakker5121ce52009-01-03 21:22:43 +00007625 unsigned char padbuf[48];
7626 unsigned char md5sum[16];
7627 unsigned char sha1sum[20];
7628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007630 if( !session )
7631 session = ssl->session;
7632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007634
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007635 mbedtls_md5_init( &md5 );
7636 mbedtls_sha1_init( &sha1 );
7637
7638 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7639 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007640
7641 /*
7642 * SSLv3:
7643 * hash =
7644 * MD5( master + pad2 +
7645 * MD5( handshake + sender + master + pad1 ) )
7646 * + SHA1( master + pad2 +
7647 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007648 */
7649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007651 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7652 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007653#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007656 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7657 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007658#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007661 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007662
Paul Bakker1ef83d62012-04-11 12:09:53 +00007663 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007664
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007665 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7666 mbedtls_md5_update_ret( &md5, session->master, 48 );
7667 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7668 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007669
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007670 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7671 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7672 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7673 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007674
Paul Bakker1ef83d62012-04-11 12:09:53 +00007675 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007676
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007677 mbedtls_md5_starts_ret( &md5 );
7678 mbedtls_md5_update_ret( &md5, session->master, 48 );
7679 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7680 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7681 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007682
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007683 mbedtls_sha1_starts_ret( &sha1 );
7684 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7685 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7686 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7687 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007690
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007691 mbedtls_md5_free( &md5 );
7692 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007693
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007694 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7695 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7696 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007699}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007703static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007705{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007706 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007707 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007708 mbedtls_md5_context md5;
7709 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007710 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007713 if( !session )
7714 session = ssl->session;
7715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007717
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007718 mbedtls_md5_init( &md5 );
7719 mbedtls_sha1_init( &sha1 );
7720
7721 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7722 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007723
Paul Bakker1ef83d62012-04-11 12:09:53 +00007724 /*
7725 * TLSv1:
7726 * hash = PRF( master, finished_label,
7727 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7728 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007731 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7732 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007733#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007736 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7737 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007738#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007741 ? "client finished"
7742 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007743
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007744 mbedtls_md5_finish_ret( &md5, padbuf );
7745 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007746
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007747 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007748 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007751
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007752 mbedtls_md5_free( &md5 );
7753 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007754
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007755 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7762#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007763static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007765{
7766 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007767 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007768 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007769#if defined(MBEDTLS_USE_PSA_CRYPTO)
7770 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007771 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007772 psa_status_t status;
7773#else
7774 mbedtls_sha256_context sha256;
7775#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007778 if( !session )
7779 session = ssl->session;
7780
Andrzej Kurekeb342242019-01-29 09:14:33 -05007781 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7782 ? "client finished"
7783 : "server finished";
7784
7785#if defined(MBEDTLS_USE_PSA_CRYPTO)
7786 sha256_psa = psa_hash_operation_init();
7787
7788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7789
7790 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7791 if( status != PSA_SUCCESS )
7792 {
7793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7794 return;
7795 }
7796
7797 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7798 if( status != PSA_SUCCESS )
7799 {
7800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7801 return;
7802 }
7803 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7804#else
7805
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007806 mbedtls_sha256_init( &sha256 );
7807
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007809
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007810 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007811
7812 /*
7813 * TLSv1.2:
7814 * hash = PRF( master, finished_label,
7815 * Hash( handshake ) )[0.11]
7816 */
7817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818#if !defined(MBEDTLS_SHA256_ALT)
7819 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007820 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007821#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007822
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007823 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007824 mbedtls_sha256_free( &sha256 );
7825#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007826
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007827 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007828 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007831
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007832 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007839static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007841{
7842 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007843 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007844 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007845#if defined(MBEDTLS_USE_PSA_CRYPTO)
7846 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007847 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007848 psa_status_t status;
7849#else
7850 mbedtls_sha512_context sha512;
7851#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007854 if( !session )
7855 session = ssl->session;
7856
Andrzej Kurekeb342242019-01-29 09:14:33 -05007857 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7858 ? "client finished"
7859 : "server finished";
7860
7861#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007862 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007863
7864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7865
Andrzej Kurek972fba52019-01-30 03:29:12 -05007866 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007867 if( status != PSA_SUCCESS )
7868 {
7869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7870 return;
7871 }
7872
Andrzej Kurek972fba52019-01-30 03:29:12 -05007873 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007874 if( status != PSA_SUCCESS )
7875 {
7876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7877 return;
7878 }
7879 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7880#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007881 mbedtls_sha512_init( &sha512 );
7882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007884
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007885 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007886
7887 /*
7888 * TLSv1.2:
7889 * hash = PRF( master, finished_label,
7890 * Hash( handshake ) )[0.11]
7891 */
7892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007894 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7895 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007896#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007897
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007898 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007899 mbedtls_sha512_free( &sha512 );
7900#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007901
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007902 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007903 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007906
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007907 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007910}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911#endif /* MBEDTLS_SHA512_C */
7912#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007915{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007917
7918 /*
7919 * Free our handshake params
7920 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007921 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007923 ssl->handshake = NULL;
7924
7925 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007926 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007927 */
7928 if( ssl->transform )
7929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930 mbedtls_ssl_transform_free( ssl->transform );
7931 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007932 }
7933 ssl->transform = ssl->transform_negotiate;
7934 ssl->transform_negotiate = NULL;
7935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007937}
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007940{
7941 int resume = ssl->handshake->resume;
7942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945#if defined(MBEDTLS_SSL_RENEGOTIATION)
7946 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007949 ssl->renego_records_seen = 0;
7950 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007951#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007952
7953 /*
7954 * Free the previous session and switch in the current one
7955 */
Paul Bakker0a597072012-09-25 21:55:46 +00007956 if( ssl->session )
7957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007959 /* RFC 7366 3.1: keep the EtM state */
7960 ssl->session_negotiate->encrypt_then_mac =
7961 ssl->session->encrypt_then_mac;
7962#endif
7963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 mbedtls_ssl_session_free( ssl->session );
7965 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007966 }
7967 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007968 ssl->session_negotiate = NULL;
7969
Paul Bakker0a597072012-09-25 21:55:46 +00007970 /*
7971 * Add cache entry
7972 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007973 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007974 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007975 resume == 0 )
7976 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007977 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007979 }
Paul Bakker0a597072012-09-25 21:55:46 +00007980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007982 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007983 ssl->handshake->flight != NULL )
7984 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007985 /* Cancel handshake timer */
7986 ssl_set_timer( ssl, 0 );
7987
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007988 /* Keep last flight around in case we need to resend it:
7989 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007991 }
7992 else
7993#endif
7994 ssl_handshake_wrapup_free_hs_transform( ssl );
7995
Paul Bakker48916f92012-09-16 19:57:18 +00007996 ssl->state++;
7997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007999}
8000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008002{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008003 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008006
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008007 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008008
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008009 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008010
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008011 /*
8012 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8013 * may define some other value. Currently (early 2016), no defined
8014 * ciphersuite does this (and this is unlikely to change as activity has
8015 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008020 ssl->verify_data_len = hash_len;
8021 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008022#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008023
Paul Bakker5121ce52009-01-03 21:22:43 +00008024 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8026 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008027
8028 /*
8029 * In case of session resuming, invert the client and server
8030 * ChangeCipherSpec messages order.
8031 */
Paul Bakker0a597072012-09-25 21:55:46 +00008032 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008035 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008039 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008040 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008041#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008042 }
8043 else
8044 ssl->state++;
8045
Paul Bakker48916f92012-09-16 19:57:18 +00008046 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008047 * Switch to our negotiated transform and session parameters for outbound
8048 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008053 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008054 {
8055 unsigned char i;
8056
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008057 /* Remember current epoch settings for resending */
8058 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01008059 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008060
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008061 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008062 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008063
8064 /* Increment epoch */
8065 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008066 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008067 break;
8068
8069 /* The loop goes to its end iff the counter is wrapping */
8070 if( i == 0 )
8071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8073 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008074 }
8075 }
8076 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008078 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008079
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008080 ssl->transform_out = ssl->transform_negotiate;
8081 ssl->session_out = ssl->session_negotiate;
8082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8084 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008086 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8089 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008090 }
8091 }
8092#endif
8093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008095 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008097#endif
8098
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008099 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008100 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008102 return( ret );
8103 }
8104
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008105#if defined(MBEDTLS_SSL_PROTO_DTLS)
8106 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8107 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8108 {
8109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8110 return( ret );
8111 }
8112#endif
8113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008115
8116 return( 0 );
8117}
8118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008119#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008120#define SSL_MAX_HASH_LEN 36
8121#else
8122#define SSL_MAX_HASH_LEN 12
8123#endif
8124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008126{
Paul Bakker23986e52011-04-24 08:57:21 +00008127 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008128 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008129 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008132
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008133 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008134
Hanno Becker327c93b2018-08-15 13:56:18 +01008135 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008138 return( ret );
8139 }
8140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008144 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8145 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008147 }
8148
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008149 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150#if defined(MBEDTLS_SSL_PROTO_SSL3)
8151 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008152 hash_len = 36;
8153 else
8154#endif
8155 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008157 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8158 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008161 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8162 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008164 }
8165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008167 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008170 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8171 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008173 }
8174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008176 ssl->verify_data_len = hash_len;
8177 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008178#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008179
Paul Bakker0a597072012-09-25 21:55:46 +00008180 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008182#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008183 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008185#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008187 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008189#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008190 }
8191 else
8192 ssl->state++;
8193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008195 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008197#endif
8198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008200
8201 return( 0 );
8202}
8203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008204static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008205{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008208#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8209 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8210 mbedtls_md5_init( &handshake->fin_md5 );
8211 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008212 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8213 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008214#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008215#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8216#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008217#if defined(MBEDTLS_USE_PSA_CRYPTO)
8218 handshake->fin_sha256_psa = psa_hash_operation_init();
8219 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8220#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008222 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008223#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008224#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008226#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008227 handshake->fin_sha384_psa = psa_hash_operation_init();
8228 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008229#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008230 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008231 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008232#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008233#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008234#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008235
8236 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008237
8238#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8239 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8240 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8241#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243#if defined(MBEDTLS_DHM_C)
8244 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008245#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246#if defined(MBEDTLS_ECDH_C)
8247 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008248#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008249#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008250 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008251#if defined(MBEDTLS_SSL_CLI_C)
8252 handshake->ecjpake_cache = NULL;
8253 handshake->ecjpake_cache_len = 0;
8254#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008255#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008256
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008257#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008258 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008259#endif
8260
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008261#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8262 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8263#endif
Hanno Becker75173122019-02-06 16:18:31 +00008264
8265#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8266 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8267 mbedtls_pk_init( &handshake->peer_pubkey );
8268#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008269}
8270
Hanno Beckera18d1322018-01-03 14:27:32 +00008271void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8276 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008277
Hanno Beckerd56ed242018-01-03 15:32:51 +00008278#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008279 mbedtls_md_init( &transform->md_ctx_enc );
8280 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008281#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008282}
8283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008285{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008287}
8288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008289static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008290{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008291 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008292 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008294 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008296 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008297 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008298
8299 /*
8300 * Either the pointers are now NULL or cleared properly and can be freed.
8301 * Now allocate missing structures.
8302 */
8303 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008304 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008305 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008306 }
Paul Bakker48916f92012-09-16 19:57:18 +00008307
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008308 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008309 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008310 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008311 }
Paul Bakker48916f92012-09-16 19:57:18 +00008312
Paul Bakker82788fb2014-10-20 13:59:19 +02008313 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008314 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008315 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008316 }
Paul Bakker48916f92012-09-16 19:57:18 +00008317
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008318 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008319 if( ssl->handshake == NULL ||
8320 ssl->transform_negotiate == NULL ||
8321 ssl->session_negotiate == NULL )
8322 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008325 mbedtls_free( ssl->handshake );
8326 mbedtls_free( ssl->transform_negotiate );
8327 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008328
8329 ssl->handshake = NULL;
8330 ssl->transform_negotiate = NULL;
8331 ssl->session_negotiate = NULL;
8332
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008333 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008334 }
8335
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008336 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008338 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008339 ssl_handshake_params_init( ssl->handshake );
8340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008342 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8343 {
8344 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008345
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008346 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8347 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8348 else
8349 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008350
8351 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008352 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008353#endif
8354
Paul Bakker48916f92012-09-16 19:57:18 +00008355 return( 0 );
8356}
8357
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008358#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008359/* Dummy cookie callbacks for defaults */
8360static int ssl_cookie_write_dummy( void *ctx,
8361 unsigned char **p, unsigned char *end,
8362 const unsigned char *cli_id, size_t cli_id_len )
8363{
8364 ((void) ctx);
8365 ((void) p);
8366 ((void) end);
8367 ((void) cli_id);
8368 ((void) cli_id_len);
8369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008371}
8372
8373static int ssl_cookie_check_dummy( void *ctx,
8374 const unsigned char *cookie, size_t cookie_len,
8375 const unsigned char *cli_id, size_t cli_id_len )
8376{
8377 ((void) ctx);
8378 ((void) cookie);
8379 ((void) cookie_len);
8380 ((void) cli_id);
8381 ((void) cli_id_len);
8382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008384}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008385#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008386
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008387/* Once ssl->out_hdr as the address of the beginning of the
8388 * next outgoing record is set, deduce the other pointers.
8389 *
8390 * Note: For TLS, we save the implicit record sequence number
8391 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8392 * and the caller has to make sure there's space for this.
8393 */
8394
8395static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8396 mbedtls_ssl_transform *transform )
8397{
8398#if defined(MBEDTLS_SSL_PROTO_DTLS)
8399 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8400 {
8401 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008402#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008403 ssl->out_cid = ssl->out_ctr + 8;
8404 ssl->out_len = ssl->out_cid;
8405 if( transform != NULL )
8406 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008407#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008408 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008409#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008410 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008411 }
8412 else
8413#endif
8414 {
8415 ssl->out_ctr = ssl->out_hdr - 8;
8416 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008417#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008418 ssl->out_cid = ssl->out_len;
8419#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008420 ssl->out_iv = ssl->out_hdr + 5;
8421 }
8422
8423 /* Adjust out_msg to make space for explicit IV, if used. */
8424 if( transform != NULL &&
8425 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8426 {
8427 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8428 }
8429 else
8430 ssl->out_msg = ssl->out_iv;
8431}
8432
8433/* Once ssl->in_hdr as the address of the beginning of the
8434 * next incoming record is set, deduce the other pointers.
8435 *
8436 * Note: For TLS, we save the implicit record sequence number
8437 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8438 * and the caller has to make sure there's space for this.
8439 */
8440
Hanno Becker79594fd2019-05-08 09:38:41 +01008441static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008442{
Hanno Becker79594fd2019-05-08 09:38:41 +01008443 /* This function sets the pointers to match the case
8444 * of unprotected TLS/DTLS records, with both ssl->in_iv
8445 * and ssl->in_msg pointing to the beginning of the record
8446 * content.
8447 *
8448 * When decrypting a protected record, ssl->in_msg
8449 * will be shifted to point to the beginning of the
8450 * record plaintext.
8451 */
8452
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008453#if defined(MBEDTLS_SSL_PROTO_DTLS)
8454 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8455 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008456 /* This sets the header pointers to match records
8457 * without CID. When we receive a record containing
8458 * a CID, the fields are shifted accordingly in
8459 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008460 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008461#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008462 ssl->in_cid = ssl->in_ctr + 8;
8463 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008464#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008465 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008466#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008467 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008468 }
8469 else
8470#endif
8471 {
8472 ssl->in_ctr = ssl->in_hdr - 8;
8473 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008474#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008475 ssl->in_cid = ssl->in_len;
8476#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008477 ssl->in_iv = ssl->in_hdr + 5;
8478 }
8479
Hanno Becker79594fd2019-05-08 09:38:41 +01008480 /* This will be adjusted at record decryption time. */
8481 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008482}
8483
Paul Bakker5121ce52009-01-03 21:22:43 +00008484/*
8485 * Initialize an SSL context
8486 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008487void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8488{
8489 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8490}
8491
8492/*
8493 * Setup an SSL context
8494 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008495
8496static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8497{
8498 /* Set the incoming and outgoing record pointers. */
8499#if defined(MBEDTLS_SSL_PROTO_DTLS)
8500 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8501 {
8502 ssl->out_hdr = ssl->out_buf;
8503 ssl->in_hdr = ssl->in_buf;
8504 }
8505 else
8506#endif /* MBEDTLS_SSL_PROTO_DTLS */
8507 {
8508 ssl->out_hdr = ssl->out_buf + 8;
8509 ssl->in_hdr = ssl->in_buf + 8;
8510 }
8511
8512 /* Derive other internal pointers. */
8513 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008514 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008515}
8516
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008517int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008518 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008519{
Paul Bakker48916f92012-09-16 19:57:18 +00008520 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008521
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008522 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008523
8524 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008525 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008526 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008527
8528 /* Set to NULL in case of an error condition */
8529 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008530
Angus Grattond8213d02016-05-25 20:56:48 +10008531 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8532 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008533 {
Angus Grattond8213d02016-05-25 20:56:48 +10008534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008535 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008536 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008537 }
8538
8539 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8540 if( ssl->out_buf == NULL )
8541 {
8542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008543 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008544 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008545 }
8546
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008547 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008548
Paul Bakker48916f92012-09-16 19:57:18 +00008549 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008550 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008551
8552 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008553
8554error:
8555 mbedtls_free( ssl->in_buf );
8556 mbedtls_free( ssl->out_buf );
8557
8558 ssl->conf = NULL;
8559
8560 ssl->in_buf = NULL;
8561 ssl->out_buf = NULL;
8562
8563 ssl->in_hdr = NULL;
8564 ssl->in_ctr = NULL;
8565 ssl->in_len = NULL;
8566 ssl->in_iv = NULL;
8567 ssl->in_msg = NULL;
8568
8569 ssl->out_hdr = NULL;
8570 ssl->out_ctr = NULL;
8571 ssl->out_len = NULL;
8572 ssl->out_iv = NULL;
8573 ssl->out_msg = NULL;
8574
k-stachowiak9f7798e2018-07-31 16:52:32 +02008575 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008576}
8577
8578/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008579 * Reset an initialized and used SSL context for re-use while retaining
8580 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008581 *
8582 * If partial is non-zero, keep data in the input buffer and client ID.
8583 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008584 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008585static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008586{
Paul Bakker48916f92012-09-16 19:57:18 +00008587 int ret;
8588
Hanno Becker7e772132018-08-10 12:38:21 +01008589#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8590 !defined(MBEDTLS_SSL_SRV_C)
8591 ((void) partial);
8592#endif
8593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008594 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008595
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008596 /* Cancel any possibly running timer */
8597 ssl_set_timer( ssl, 0 );
8598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599#if defined(MBEDTLS_SSL_RENEGOTIATION)
8600 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008601 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008602
8603 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8605 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008608
Paul Bakker7eb013f2011-10-06 12:37:39 +00008609 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008610 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008611
8612 ssl->in_msgtype = 0;
8613 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008615 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008616 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008617#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008619 ssl_dtls_replay_reset( ssl );
8620#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008621
8622 ssl->in_hslen = 0;
8623 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008624
8625 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008626
8627 ssl->out_msgtype = 0;
8628 ssl->out_msglen = 0;
8629 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8631 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008632 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008633#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008634
Hanno Becker19859472018-08-06 09:40:20 +01008635 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8636
Paul Bakker48916f92012-09-16 19:57:18 +00008637 ssl->transform_in = NULL;
8638 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008639
Hanno Becker78640902018-08-13 16:35:15 +01008640 ssl->session_in = NULL;
8641 ssl->session_out = NULL;
8642
Angus Grattond8213d02016-05-25 20:56:48 +10008643 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008644
8645#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008646 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008647#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8648 {
8649 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008650 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008651 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008653#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8654 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8657 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8660 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008661 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008662 }
8663#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008664
Paul Bakker48916f92012-09-16 19:57:18 +00008665 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667 mbedtls_ssl_transform_free( ssl->transform );
8668 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008669 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008670 }
Paul Bakker48916f92012-09-16 19:57:18 +00008671
Paul Bakkerc0463502013-02-14 11:19:38 +01008672 if( ssl->session )
8673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674 mbedtls_ssl_session_free( ssl->session );
8675 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008676 ssl->session = NULL;
8677 }
8678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008680 ssl->alpn_chosen = NULL;
8681#endif
8682
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008683#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008684#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008685 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008686#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008687 {
8688 mbedtls_free( ssl->cli_id );
8689 ssl->cli_id = NULL;
8690 ssl->cli_id_len = 0;
8691 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008692#endif
8693
Paul Bakker48916f92012-09-16 19:57:18 +00008694 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8695 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008696
8697 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008698}
8699
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008700/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008701 * Reset an initialized and used SSL context for re-use while retaining
8702 * all application-set variables, function pointers and data.
8703 */
8704int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8705{
8706 return( ssl_session_reset_int( ssl, 0 ) );
8707}
8708
8709/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008710 * SSL set accessors
8711 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008712void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008713{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008714 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008715}
8716
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008717void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008718{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008719 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008720}
8721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008722#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008723void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008724{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008725 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008726}
8727#endif
8728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008730void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008731{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008732 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008733}
8734#endif
8735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008736#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008737
Hanno Becker1841b0a2018-08-24 11:13:57 +01008738void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8739 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008740{
8741 ssl->disable_datagram_packing = !allow_packing;
8742}
8743
8744void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8745 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008746{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008747 conf->hs_timeout_min = min;
8748 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008749}
8750#endif
8751
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008752void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008753{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008754 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008755}
8756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008758void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008759 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008760 void *p_vrfy )
8761{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008762 conf->f_vrfy = f_vrfy;
8763 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008764}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008766
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008767void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008768 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008769 void *p_rng )
8770{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008771 conf->f_rng = f_rng;
8772 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008773}
8774
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008775void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008776 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008777 void *p_dbg )
8778{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008779 conf->f_dbg = f_dbg;
8780 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008781}
8782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008784 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008785 mbedtls_ssl_send_t *f_send,
8786 mbedtls_ssl_recv_t *f_recv,
8787 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008788{
8789 ssl->p_bio = p_bio;
8790 ssl->f_send = f_send;
8791 ssl->f_recv = f_recv;
8792 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008793}
8794
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008795#if defined(MBEDTLS_SSL_PROTO_DTLS)
8796void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8797{
8798 ssl->mtu = mtu;
8799}
8800#endif
8801
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008802void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008803{
8804 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008805}
8806
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008807void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8808 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008809 mbedtls_ssl_set_timer_t *f_set_timer,
8810 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008811{
8812 ssl->p_timer = p_timer;
8813 ssl->f_set_timer = f_set_timer;
8814 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008815
8816 /* Make sure we start with no timer running */
8817 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008818}
8819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008820#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008821void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008822 void *p_cache,
8823 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8824 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008825{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008826 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008827 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008828 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008830#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832#if defined(MBEDTLS_SSL_CLI_C)
8833int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008834{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008835 int ret;
8836
8837 if( ssl == NULL ||
8838 session == NULL ||
8839 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008840 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008843 }
8844
Hanno Becker52055ae2019-02-06 14:30:46 +00008845 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8846 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008847 return( ret );
8848
Paul Bakker0a597072012-09-25 21:55:46 +00008849 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008850
8851 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008854
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008855void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008856 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008857{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008858 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8859 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8860 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8861 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008862}
8863
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008864void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008865 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008866 int major, int minor )
8867{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008869 return;
8870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008871 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008872 return;
8873
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008874 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008875}
8876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008877#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008878void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008879 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008880{
8881 conf->cert_profile = profile;
8882}
8883
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008884/* Append a new keycert entry to a (possibly empty) list */
8885static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8886 mbedtls_x509_crt *cert,
8887 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008888{
niisato8ee24222018-06-25 19:05:48 +09008889 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008890
niisato8ee24222018-06-25 19:05:48 +09008891 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8892 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008893 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008894
niisato8ee24222018-06-25 19:05:48 +09008895 new_cert->cert = cert;
8896 new_cert->key = key;
8897 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008898
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008899 /* Update head is the list was null, else add to the end */
8900 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008901 {
niisato8ee24222018-06-25 19:05:48 +09008902 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008903 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008904 else
8905 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008906 mbedtls_ssl_key_cert *cur = *head;
8907 while( cur->next != NULL )
8908 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008909 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008910 }
8911
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008912 return( 0 );
8913}
8914
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008915int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008916 mbedtls_x509_crt *own_cert,
8917 mbedtls_pk_context *pk_key )
8918{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008919 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008920}
8921
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008922void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008923 mbedtls_x509_crt *ca_chain,
8924 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008925{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008926 conf->ca_chain = ca_chain;
8927 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008928
8929#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8930 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8931 * cannot be used together. */
8932 conf->f_ca_cb = NULL;
8933 conf->p_ca_cb = NULL;
8934#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008935}
Hanno Becker5adaad92019-03-27 16:54:37 +00008936
8937#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8938void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008939 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008940 void *p_ca_cb )
8941{
8942 conf->f_ca_cb = f_ca_cb;
8943 conf->p_ca_cb = p_ca_cb;
8944
8945 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8946 * cannot be used together. */
8947 conf->ca_chain = NULL;
8948 conf->ca_crl = NULL;
8949}
8950#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008951#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008952
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008953#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8954int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8955 mbedtls_x509_crt *own_cert,
8956 mbedtls_pk_context *pk_key )
8957{
8958 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8959 own_cert, pk_key ) );
8960}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008961
8962void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8963 mbedtls_x509_crt *ca_chain,
8964 mbedtls_x509_crl *ca_crl )
8965{
8966 ssl->handshake->sni_ca_chain = ca_chain;
8967 ssl->handshake->sni_ca_crl = ca_crl;
8968}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008969
8970void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8971 int authmode )
8972{
8973 ssl->handshake->sni_authmode = authmode;
8974}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008975#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8976
Hanno Becker8927c832019-04-03 12:52:50 +01008977#if defined(MBEDTLS_X509_CRT_PARSE_C)
8978void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8979 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8980 void *p_vrfy )
8981{
8982 ssl->f_vrfy = f_vrfy;
8983 ssl->p_vrfy = p_vrfy;
8984}
8985#endif
8986
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008987#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008988/*
8989 * Set EC J-PAKE password for current handshake
8990 */
8991int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8992 const unsigned char *pw,
8993 size_t pw_len )
8994{
8995 mbedtls_ecjpake_role role;
8996
Janos Follath8eb64132016-06-03 15:40:57 +01008997 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008998 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8999
9000 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
9001 role = MBEDTLS_ECJPAKE_SERVER;
9002 else
9003 role = MBEDTLS_ECJPAKE_CLIENT;
9004
9005 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9006 role,
9007 MBEDTLS_MD_SHA256,
9008 MBEDTLS_ECP_DP_SECP256R1,
9009 pw, pw_len ) );
9010}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009011#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009014
9015static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
9016{
9017 /* Remove reference to existing PSK, if any. */
9018#if defined(MBEDTLS_USE_PSA_CRYPTO)
9019 if( conf->psk_opaque != 0 )
9020 {
9021 /* The maintenance of the PSK key slot is the
9022 * user's responsibility. */
9023 conf->psk_opaque = 0;
9024 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00009025 /* This and the following branch should never
9026 * be taken simultaenously as we maintain the
9027 * invariant that raw and opaque PSKs are never
9028 * configured simultaneously. As a safeguard,
9029 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009030#endif /* MBEDTLS_USE_PSA_CRYPTO */
9031 if( conf->psk != NULL )
9032 {
9033 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
9034
9035 mbedtls_free( conf->psk );
9036 conf->psk = NULL;
9037 conf->psk_len = 0;
9038 }
9039
9040 /* Remove reference to PSK identity, if any. */
9041 if( conf->psk_identity != NULL )
9042 {
9043 mbedtls_free( conf->psk_identity );
9044 conf->psk_identity = NULL;
9045 conf->psk_identity_len = 0;
9046 }
9047}
9048
Hanno Becker7390c712018-11-15 13:33:04 +00009049/* This function assumes that PSK identity in the SSL config is unset.
9050 * It checks that the provided identity is well-formed and attempts
9051 * to make a copy of it in the SSL config.
9052 * On failure, the PSK identity in the config remains unset. */
9053static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
9054 unsigned char const *psk_identity,
9055 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009056{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009057 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00009058 if( psk_identity == NULL ||
9059 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009060 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009061 {
9062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9063 }
9064
Hanno Becker7390c712018-11-15 13:33:04 +00009065 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
9066 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009067 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02009068
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009069 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009070 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02009071
9072 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02009073}
9074
Hanno Becker7390c712018-11-15 13:33:04 +00009075int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9076 const unsigned char *psk, size_t psk_len,
9077 const unsigned char *psk_identity, size_t psk_identity_len )
9078{
9079 int ret;
9080 /* Remove opaque/raw PSK + PSK Identity */
9081 ssl_conf_remove_psk( conf );
9082
9083 /* Check and set raw PSK */
9084 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9086 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9087 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9088 conf->psk_len = psk_len;
9089 memcpy( conf->psk, psk, conf->psk_len );
9090
9091 /* Check and set PSK Identity */
9092 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9093 if( ret != 0 )
9094 ssl_conf_remove_psk( conf );
9095
9096 return( ret );
9097}
9098
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009099static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9100{
9101#if defined(MBEDTLS_USE_PSA_CRYPTO)
9102 if( ssl->handshake->psk_opaque != 0 )
9103 {
9104 ssl->handshake->psk_opaque = 0;
9105 }
9106 else
9107#endif /* MBEDTLS_USE_PSA_CRYPTO */
9108 if( ssl->handshake->psk != NULL )
9109 {
9110 mbedtls_platform_zeroize( ssl->handshake->psk,
9111 ssl->handshake->psk_len );
9112 mbedtls_free( ssl->handshake->psk );
9113 ssl->handshake->psk_len = 0;
9114 }
9115}
9116
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009117int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9118 const unsigned char *psk, size_t psk_len )
9119{
9120 if( psk == NULL || ssl->handshake == NULL )
9121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9122
9123 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9125
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009126 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009127
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009128 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009129 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009130
9131 ssl->handshake->psk_len = psk_len;
9132 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9133
9134 return( 0 );
9135}
9136
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009137#if defined(MBEDTLS_USE_PSA_CRYPTO)
9138int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009139 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009140 const unsigned char *psk_identity,
9141 size_t psk_identity_len )
9142{
Hanno Becker7390c712018-11-15 13:33:04 +00009143 int ret;
9144 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009145 ssl_conf_remove_psk( conf );
9146
Hanno Becker7390c712018-11-15 13:33:04 +00009147 /* Check and set opaque PSK */
9148 if( psk_slot == 0 )
9149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009150 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009151
9152 /* Check and set PSK Identity */
9153 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9154 psk_identity_len );
9155 if( ret != 0 )
9156 ssl_conf_remove_psk( conf );
9157
9158 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009159}
9160
9161int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009162 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009163{
9164 if( psk_slot == 0 || ssl->handshake == NULL )
9165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9166
9167 ssl_remove_psk( ssl );
9168 ssl->handshake->psk_opaque = psk_slot;
9169 return( 0 );
9170}
9171#endif /* MBEDTLS_USE_PSA_CRYPTO */
9172
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009173void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009174 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009175 size_t),
9176 void *p_psk )
9177{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009178 conf->f_psk = f_psk;
9179 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009180}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009181#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009182
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009183#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009184
9185#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009186int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009187{
9188 int ret;
9189
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009190 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9191 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9192 {
9193 mbedtls_mpi_free( &conf->dhm_P );
9194 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009195 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009196 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009197
9198 return( 0 );
9199}
Hanno Becker470a8c42017-10-04 15:28:46 +01009200#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009201
Hanno Beckera90658f2017-10-04 15:29:08 +01009202int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9203 const unsigned char *dhm_P, size_t P_len,
9204 const unsigned char *dhm_G, size_t G_len )
9205{
9206 int ret;
9207
9208 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9209 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9210 {
9211 mbedtls_mpi_free( &conf->dhm_P );
9212 mbedtls_mpi_free( &conf->dhm_G );
9213 return( ret );
9214 }
9215
9216 return( 0 );
9217}
Paul Bakker5121ce52009-01-03 21:22:43 +00009218
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009219int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009220{
9221 int ret;
9222
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009223 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9224 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9225 {
9226 mbedtls_mpi_free( &conf->dhm_P );
9227 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009228 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009229 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009230
9231 return( 0 );
9232}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009233#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009234
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009235#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9236/*
9237 * Set the minimum length for Diffie-Hellman parameters
9238 */
9239void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9240 unsigned int bitlen )
9241{
9242 conf->dhm_min_bitlen = bitlen;
9243}
9244#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9245
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009246#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009247/*
9248 * Set allowed/preferred hashes for handshake signatures
9249 */
9250void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9251 const int *hashes )
9252{
9253 conf->sig_hashes = hashes;
9254}
Hanno Becker947194e2017-04-07 13:25:49 +01009255#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009256
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009257#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009258/*
9259 * Set the allowed elliptic curves
9260 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009261void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009262 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009263{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009264 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009265}
Hanno Becker947194e2017-04-07 13:25:49 +01009266#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009267
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009268#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009269int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009270{
Hanno Becker947194e2017-04-07 13:25:49 +01009271 /* Initialize to suppress unnecessary compiler warning */
9272 size_t hostname_len = 0;
9273
9274 /* Check if new hostname is valid before
9275 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009276 if( hostname != NULL )
9277 {
9278 hostname_len = strlen( hostname );
9279
9280 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9281 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9282 }
9283
9284 /* Now it's clear that we will overwrite the old hostname,
9285 * so we can free it safely */
9286
9287 if( ssl->hostname != NULL )
9288 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009289 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009290 mbedtls_free( ssl->hostname );
9291 }
9292
9293 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009294
Paul Bakker5121ce52009-01-03 21:22:43 +00009295 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009296 {
9297 ssl->hostname = NULL;
9298 }
9299 else
9300 {
9301 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009302 if( ssl->hostname == NULL )
9303 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009304
Hanno Becker947194e2017-04-07 13:25:49 +01009305 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009306
Hanno Becker947194e2017-04-07 13:25:49 +01009307 ssl->hostname[hostname_len] = '\0';
9308 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009309
9310 return( 0 );
9311}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009312#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009313
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009314#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009315void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009317 const unsigned char *, size_t),
9318 void *p_sni )
9319{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009320 conf->f_sni = f_sni;
9321 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009322}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009326int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009327{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009328 size_t cur_len, tot_len;
9329 const char **p;
9330
9331 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009332 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9333 * MUST NOT be truncated."
9334 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009335 */
9336 tot_len = 0;
9337 for( p = protos; *p != NULL; p++ )
9338 {
9339 cur_len = strlen( *p );
9340 tot_len += cur_len;
9341
9342 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009344 }
9345
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009346 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009347
9348 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009349}
9350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009352{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009353 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009356
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009357void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009358{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009359 conf->max_major_ver = major;
9360 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009361}
9362
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009363void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009364{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009365 conf->min_major_ver = major;
9366 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009367}
9368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009370void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009371{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009372 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009373}
9374#endif
9375
Janos Follath088ce432017-04-10 12:42:31 +01009376#if defined(MBEDTLS_SSL_SRV_C)
9377void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9378 char cert_req_ca_list )
9379{
9380 conf->cert_req_ca_list = cert_req_ca_list;
9381}
9382#endif
9383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009385void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009386{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009387 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009388}
9389#endif
9390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009392void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009393{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009394 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009395}
9396#endif
9397
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009398#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009399void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009400{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009401 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009402}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009403#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009405#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009406int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009409 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009412 }
9413
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009414 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009415
9416 return( 0 );
9417}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009418#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009420#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009421void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009422{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009423 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009424}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009425#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009428void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009429{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009430 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009431}
9432#endif
9433
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009434void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009435{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009436 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009437}
9438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009440void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009441{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009442 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009443}
9444
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009445void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009446{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009447 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009448}
9449
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009450void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009451 const unsigned char period[8] )
9452{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009453 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009458#if defined(MBEDTLS_SSL_CLI_C)
9459void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009460{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009461 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009462}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009463#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009464
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009465#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009466void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9467 mbedtls_ssl_ticket_write_t *f_ticket_write,
9468 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9469 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009470{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009471 conf->f_ticket_write = f_ticket_write;
9472 conf->f_ticket_parse = f_ticket_parse;
9473 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009474}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009476#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009477
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009478#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9479void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9480 mbedtls_ssl_export_keys_t *f_export_keys,
9481 void *p_export_keys )
9482{
9483 conf->f_export_keys = f_export_keys;
9484 conf->p_export_keys = p_export_keys;
9485}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009486
9487void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9488 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9489 void *p_export_keys )
9490{
9491 conf->f_export_keys_ext = f_export_keys_ext;
9492 conf->p_export_keys = p_export_keys;
9493}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009494#endif
9495
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009496#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009497void mbedtls_ssl_conf_async_private_cb(
9498 mbedtls_ssl_config *conf,
9499 mbedtls_ssl_async_sign_t *f_async_sign,
9500 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9501 mbedtls_ssl_async_resume_t *f_async_resume,
9502 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009503 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009504{
9505 conf->f_async_sign_start = f_async_sign;
9506 conf->f_async_decrypt_start = f_async_decrypt;
9507 conf->f_async_resume = f_async_resume;
9508 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009509 conf->p_async_config_data = async_config_data;
9510}
9511
Gilles Peskine8f97af72018-04-26 11:46:10 +02009512void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9513{
9514 return( conf->p_async_config_data );
9515}
9516
Gilles Peskine1febfef2018-04-30 11:54:39 +02009517void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009518{
9519 if( ssl->handshake == NULL )
9520 return( NULL );
9521 else
9522 return( ssl->handshake->user_async_ctx );
9523}
9524
Gilles Peskine1febfef2018-04-30 11:54:39 +02009525void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009526 void *ctx )
9527{
9528 if( ssl->handshake != NULL )
9529 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009530}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009531#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009532
Paul Bakker5121ce52009-01-03 21:22:43 +00009533/*
9534 * SSL get accessors
9535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009537{
9538 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9539}
9540
Hanno Becker8b170a02017-10-10 11:51:19 +01009541int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9542{
9543 /*
9544 * Case A: We're currently holding back
9545 * a message for further processing.
9546 */
9547
9548 if( ssl->keep_current_message == 1 )
9549 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009551 return( 1 );
9552 }
9553
9554 /*
9555 * Case B: Further records are pending in the current datagram.
9556 */
9557
9558#if defined(MBEDTLS_SSL_PROTO_DTLS)
9559 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9560 ssl->in_left > ssl->next_record_offset )
9561 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009563 return( 1 );
9564 }
9565#endif /* MBEDTLS_SSL_PROTO_DTLS */
9566
9567 /*
9568 * Case C: A handshake message is being processed.
9569 */
9570
Hanno Becker8b170a02017-10-10 11:51:19 +01009571 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9572 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009574 return( 1 );
9575 }
9576
9577 /*
9578 * Case D: An application data message is being processed
9579 */
9580 if( ssl->in_offt != NULL )
9581 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009582 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009583 return( 1 );
9584 }
9585
9586 /*
9587 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009588 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009589 * we implement support for multiple alerts in single records.
9590 */
9591
9592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9593 return( 0 );
9594}
9595
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009596uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009597{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009598 if( ssl->session != NULL )
9599 return( ssl->session->verify_result );
9600
9601 if( ssl->session_negotiate != NULL )
9602 return( ssl->session_negotiate->verify_result );
9603
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009604 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009605}
9606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009608{
Paul Bakker926c8e42013-03-06 10:23:34 +01009609 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009610 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009613}
9614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009616{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009618 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009619 {
9620 switch( ssl->minor_ver )
9621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009623 return( "DTLSv1.0" );
9624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009626 return( "DTLSv1.2" );
9627
9628 default:
9629 return( "unknown (DTLS)" );
9630 }
9631 }
9632#endif
9633
Paul Bakker43ca69c2011-01-15 17:35:19 +00009634 switch( ssl->minor_ver )
9635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009637 return( "SSLv3.0" );
9638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009640 return( "TLSv1.0" );
9641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009643 return( "TLSv1.1" );
9644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009646 return( "TLSv1.2" );
9647
Paul Bakker43ca69c2011-01-15 17:35:19 +00009648 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009649 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009650 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009651}
9652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009653int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009654{
Hanno Becker3136ede2018-08-17 15:28:19 +01009655 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009657 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009658
Hanno Becker5903de42019-05-03 14:46:38 +01009659 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9660
Hanno Becker78640902018-08-13 16:35:15 +01009661 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009662 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664#if defined(MBEDTLS_ZLIB_SUPPORT)
9665 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9666 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009667#endif
9668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671 case MBEDTLS_MODE_GCM:
9672 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009673 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009675 transform_expansion = transform->minlen;
9676 break;
9677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009679
9680 block_size = mbedtls_cipher_get_block_size(
9681 &transform->cipher_ctx_enc );
9682
Hanno Becker3136ede2018-08-17 15:28:19 +01009683 /* Expansion due to the addition of the MAC. */
9684 transform_expansion += transform->maclen;
9685
9686 /* Expansion due to the addition of CBC padding;
9687 * Theoretically up to 256 bytes, but we never use
9688 * more than the block size of the underlying cipher. */
9689 transform_expansion += block_size;
9690
9691 /* For TLS 1.1 or higher, an explicit IV is added
9692 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009693#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9694 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009695 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009696#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009697
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009698 break;
9699
9700 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009703 }
9704
Hanno Beckera0e20d02019-05-15 14:03:01 +01009705#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009706 if( transform->out_cid_len != 0 )
9707 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009708#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009709
Hanno Becker5903de42019-05-03 14:46:38 +01009710 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009711}
9712
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009713#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9714size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9715{
9716 size_t max_len;
9717
9718 /*
9719 * Assume mfl_code is correct since it was checked when set
9720 */
Angus Grattond8213d02016-05-25 20:56:48 +10009721 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009722
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009723 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009724 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009725 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009726 {
Angus Grattond8213d02016-05-25 20:56:48 +10009727 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009728 }
9729
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009730 /* During a handshake, use the value being negotiated */
9731 if( ssl->session_negotiate != NULL &&
9732 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9733 {
9734 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9735 }
9736
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009737 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009738}
9739#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9740
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009741#if defined(MBEDTLS_SSL_PROTO_DTLS)
9742static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9743{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009744 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9745 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9746 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9747 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9748 return ( 0 );
9749
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009750 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9751 return( ssl->mtu );
9752
9753 if( ssl->mtu == 0 )
9754 return( ssl->handshake->mtu );
9755
9756 return( ssl->mtu < ssl->handshake->mtu ?
9757 ssl->mtu : ssl->handshake->mtu );
9758}
9759#endif /* MBEDTLS_SSL_PROTO_DTLS */
9760
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009761int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9762{
9763 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9764
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009765#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9766 !defined(MBEDTLS_SSL_PROTO_DTLS)
9767 (void) ssl;
9768#endif
9769
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009770#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9771 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9772
9773 if( max_len > mfl )
9774 max_len = mfl;
9775#endif
9776
9777#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009778 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009779 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009780 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009781 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9782 const size_t overhead = (size_t) ret;
9783
9784 if( ret < 0 )
9785 return( ret );
9786
9787 if( mtu <= overhead )
9788 {
9789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9790 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9791 }
9792
9793 if( max_len > mtu - overhead )
9794 max_len = mtu - overhead;
9795 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009796#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009797
Hanno Becker0defedb2018-08-10 12:35:02 +01009798#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9799 !defined(MBEDTLS_SSL_PROTO_DTLS)
9800 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009801#endif
9802
9803 return( (int) max_len );
9804}
9805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806#if defined(MBEDTLS_X509_CRT_PARSE_C)
9807const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009808{
9809 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009810 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009811
Hanno Beckere6824572019-02-07 13:18:46 +00009812#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009813 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009814#else
9815 return( NULL );
9816#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009817}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009821int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9822 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009823{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009824 if( ssl == NULL ||
9825 dst == NULL ||
9826 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009827 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009830 }
9831
Hanno Becker52055ae2019-02-06 14:30:46 +00009832 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009833}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009835
Paul Bakker5121ce52009-01-03 21:22:43 +00009836/*
Paul Bakker1961b702013-01-25 14:49:24 +01009837 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009840{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009842
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009843 if( ssl == NULL || ssl->conf == NULL )
9844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009847 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009849#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009851 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009853#endif
9854
Paul Bakker1961b702013-01-25 14:49:24 +01009855 return( ret );
9856}
9857
9858/*
9859 * Perform the SSL handshake
9860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009862{
9863 int ret = 0;
9864
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009865 if( ssl == NULL || ssl->conf == NULL )
9866 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009870 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009873
9874 if( ret != 0 )
9875 break;
9876 }
9877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009879
9880 return( ret );
9881}
9882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883#if defined(MBEDTLS_SSL_RENEGOTIATION)
9884#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009885/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009886 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009888static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009889{
9890 int ret;
9891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009893
9894 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009895 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9896 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009897
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009898 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009899 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009901 return( ret );
9902 }
9903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009905
9906 return( 0 );
9907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009908#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009909
9910/*
9911 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912 * - any side: calling mbedtls_ssl_renegotiate(),
9913 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9914 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009915 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009916 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009920{
9921 int ret;
9922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009924
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009925 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9926 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009927
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009928 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9929 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009931 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009933 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009934 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009935 ssl->handshake->out_msg_seq = 1;
9936 else
9937 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009938 }
9939#endif
9940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009941 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9942 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009947 return( ret );
9948 }
9949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009951
9952 return( 0 );
9953}
9954
9955/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009956 * Renegotiate current connection on client,
9957 * or request renegotiation on server
9958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009959int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009962
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009963 if( ssl == NULL || ssl->conf == NULL )
9964 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009966#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009967 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009968 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009970 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9971 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009974
9975 /* Did we already try/start sending HelloRequest? */
9976 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009978
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009979 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009980 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009983#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009984 /*
9985 * On client, either start the renegotiation process or,
9986 * if already in progress, continue the handshake
9987 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9991 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009992
9993 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009996 return( ret );
9997 }
9998 }
9999 else
10000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010004 return( ret );
10005 }
10006 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010008
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010009 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010010}
10011
10012/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010013 * Check record counters and renegotiate if they're above the limit.
10014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010015static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010016{
Andres AG2196c7f2016-12-15 17:01:16 +000010017 size_t ep_len = ssl_ep_len( ssl );
10018 int in_ctr_cmp;
10019 int out_ctr_cmp;
10020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010021 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10022 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010023 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010024 {
10025 return( 0 );
10026 }
10027
Andres AG2196c7f2016-12-15 17:01:16 +000010028 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10029 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010030 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010031 ssl->conf->renego_period + ep_len, 8 - ep_len );
10032
10033 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010034 {
10035 return( 0 );
10036 }
10037
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010040}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010041#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010042
10043/*
10044 * Receive application data decrypted from the SSL layer
10045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010046int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010047{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010048 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010049 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010050
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010051 if( ssl == NULL || ssl->conf == NULL )
10052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010057 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010059 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010060 return( ret );
10061
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010062 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010064 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010065 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010066 return( ret );
10067 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010068 }
10069#endif
10070
Hanno Becker4a810fb2017-05-24 16:27:30 +010010071 /*
10072 * Check if renegotiation is necessary and/or handshake is
10073 * in process. If yes, perform/continue, and fall through
10074 * if an unexpected packet is received while the client
10075 * is waiting for the ServerHello.
10076 *
10077 * (There is no equivalent to the last condition on
10078 * the server-side as it is not treated as within
10079 * a handshake while waiting for the ClientHello
10080 * after a renegotiation request.)
10081 */
10082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010084 ret = ssl_check_ctr_renegotiate( ssl );
10085 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10086 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010089 return( ret );
10090 }
10091#endif
10092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010096 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10097 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010100 return( ret );
10101 }
10102 }
10103
Hanno Beckere41158b2017-10-23 13:30:32 +010010104 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010105 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010106 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010107 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010108 if( ssl->f_get_timer != NULL &&
10109 ssl->f_get_timer( ssl->p_timer ) == -1 )
10110 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010111 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010112 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010113
Hanno Becker327c93b2018-08-15 13:56:18 +010010114 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010115 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010116 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10117 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010118
Hanno Becker4a810fb2017-05-24 16:27:30 +010010119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10120 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010121 }
10122
10123 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010125 {
10126 /*
10127 * OpenSSL sends empty messages to randomize the IV
10128 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010129 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010131 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010132 return( 0 );
10133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010135 return( ret );
10136 }
10137 }
10138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010139 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010142
Hanno Becker4a810fb2017-05-24 16:27:30 +010010143 /*
10144 * - For client-side, expect SERVER_HELLO_REQUEST.
10145 * - For server-side, expect CLIENT_HELLO.
10146 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10147 */
10148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010149#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010150 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010151 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010152 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010155
10156 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010159 {
10160 continue;
10161 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010162#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010163 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010164 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010165#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010166
Hanno Becker4a810fb2017-05-24 16:27:30 +010010167#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010168 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010172
10173 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010174#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010175 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010176 {
10177 continue;
10178 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010179#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010181 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010182#endif /* MBEDTLS_SSL_SRV_C */
10183
Hanno Becker21df7f92017-10-17 11:03:26 +010010184#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010185 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010186 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10187 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10188 ssl->conf->allow_legacy_renegotiation ==
10189 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10190 {
10191 /*
10192 * Accept renegotiation request
10193 */
Paul Bakker48916f92012-09-16 19:57:18 +000010194
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010195 /* DTLS clients need to know renego is server-initiated */
10196#if defined(MBEDTLS_SSL_PROTO_DTLS)
10197 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10198 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10199 {
10200 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10201 }
10202#endif
10203 ret = ssl_start_renegotiation( ssl );
10204 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10205 ret != 0 )
10206 {
10207 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10208 return( ret );
10209 }
10210 }
10211 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010212#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010213 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010214 /*
10215 * Refuse renegotiation
10216 */
10217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220#if defined(MBEDTLS_SSL_PROTO_SSL3)
10221 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010222 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010223 /* SSLv3 does not have a "no_renegotiation" warning, so
10224 we send a fatal alert and abort the connection. */
10225 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10226 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10227 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010228 }
10229 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10231#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10232 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10233 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10236 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10237 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010238 {
10239 return( ret );
10240 }
Paul Bakker48916f92012-09-16 19:57:18 +000010241 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010242 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010243#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10244 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10247 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010248 }
Paul Bakker48916f92012-09-16 19:57:18 +000010249 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010250
Hanno Becker90333da2017-10-10 11:27:13 +010010251 /* At this point, we don't know whether the renegotiation has been
10252 * completed or not. The cases to consider are the following:
10253 * 1) The renegotiation is complete. In this case, no new record
10254 * has been read yet.
10255 * 2) The renegotiation is incomplete because the client received
10256 * an application data record while awaiting the ServerHello.
10257 * 3) The renegotiation is incomplete because the client received
10258 * a non-handshake, non-application data message while awaiting
10259 * the ServerHello.
10260 * In each of these case, looping will be the proper action:
10261 * - For 1), the next iteration will read a new record and check
10262 * if it's application data.
10263 * - For 2), the loop condition isn't satisfied as application data
10264 * is present, hence continue is the same as break
10265 * - For 3), the loop condition is satisfied and read_record
10266 * will re-deliver the message that was held back by the client
10267 * when expecting the ServerHello.
10268 */
10269 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010270 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010271#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010272 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010273 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010274 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010275 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010276 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010279 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010281 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010282 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010283 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010284#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010286 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10287 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010290 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010291 }
10292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10296 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010297 }
10298
10299 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010300
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010301 /* We're going to return something now, cancel timer,
10302 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010303 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010304 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010305
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010306#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010307 /* If we requested renego but received AppData, resend HelloRequest.
10308 * Do it now, after setting in_offt, to avoid taking this branch
10309 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010310#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010311 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010313 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010314 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010316 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010317 return( ret );
10318 }
10319 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010320#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010321#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010322 }
10323
10324 n = ( len < ssl->in_msglen )
10325 ? len : ssl->in_msglen;
10326
10327 memcpy( buf, ssl->in_offt, n );
10328 ssl->in_msglen -= n;
10329
10330 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010331 {
10332 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010333 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010334 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010335 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010336 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010337 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010338 /* more data available */
10339 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010340 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010343
Paul Bakker23986e52011-04-24 08:57:21 +000010344 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010345}
10346
10347/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010348 * Send application data to be encrypted by the SSL layer, taking care of max
10349 * fragment length and buffer size.
10350 *
10351 * According to RFC 5246 Section 6.2.1:
10352 *
10353 * Zero-length fragments of Application data MAY be sent as they are
10354 * potentially useful as a traffic analysis countermeasure.
10355 *
10356 * Therefore, it is possible that the input message length is 0 and the
10357 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010358 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010359static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010360 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010361{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010362 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10363 const size_t max_len = (size_t) ret;
10364
10365 if( ret < 0 )
10366 {
10367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10368 return( ret );
10369 }
10370
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010371 if( len > max_len )
10372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010374 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010377 "maximum fragment length: %d > %d",
10378 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010380 }
10381 else
10382#endif
10383 len = max_len;
10384 }
Paul Bakker887bd502011-06-08 13:10:54 +000010385
Paul Bakker5121ce52009-01-03 21:22:43 +000010386 if( ssl->out_left != 0 )
10387 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010388 /*
10389 * The user has previously tried to send the data and
10390 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10391 * written. In this case, we expect the high-level write function
10392 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010394 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010397 return( ret );
10398 }
10399 }
Paul Bakker887bd502011-06-08 13:10:54 +000010400 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010401 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010402 /*
10403 * The user is trying to send a message the first time, so we need to
10404 * copy the data into the internal buffers and setup the data structure
10405 * to keep track of partial writes
10406 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010407 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010408 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010409 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010410
Hanno Becker67bc7c32018-08-06 11:33:50 +010010411 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010414 return( ret );
10415 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010416 }
10417
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010418 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010419}
10420
10421/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010422 * Write application data, doing 1/n-1 splitting if necessary.
10423 *
10424 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010425 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010426 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010428#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010429static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010430 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010431{
10432 int ret;
10433
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010434 if( ssl->conf->cbc_record_splitting ==
10435 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010436 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10438 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10439 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010440 {
10441 return( ssl_write_real( ssl, buf, len ) );
10442 }
10443
10444 if( ssl->split_done == 0 )
10445 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010446 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010447 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010448 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010449 }
10450
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010451 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10452 return( ret );
10453 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010454
10455 return( ret + 1 );
10456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010457#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010458
10459/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010460 * Write application data (public-facing wrapper)
10461 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010462int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010463{
10464 int ret;
10465
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010467
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010468 if( ssl == NULL || ssl->conf == NULL )
10469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10470
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010471#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010472 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10473 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010474 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010475 return( ret );
10476 }
10477#endif
10478
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010479 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010480 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010481 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010482 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010484 return( ret );
10485 }
10486 }
10487
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010488#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010489 ret = ssl_write_split( ssl, buf, len );
10490#else
10491 ret = ssl_write_real( ssl, buf, len );
10492#endif
10493
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010495
10496 return( ret );
10497}
10498
10499/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010500 * Notify the peer that the connection is being closed
10501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010502int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010503{
10504 int ret;
10505
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010506 if( ssl == NULL || ssl->conf == NULL )
10507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010510
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010511 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010512 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10517 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10518 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010521 return( ret );
10522 }
10523 }
10524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010526
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010527 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010528}
10529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010530void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010531{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010532 if( transform == NULL )
10533 return;
10534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010535#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010536 deflateEnd( &transform->ctx_deflate );
10537 inflateEnd( &transform->ctx_inflate );
10538#endif
10539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010540 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10541 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010542
Hanno Beckerd56ed242018-01-03 15:32:51 +000010543#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010544 mbedtls_md_free( &transform->md_ctx_enc );
10545 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010546#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010547
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010548 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010549}
10550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010551#if defined(MBEDTLS_X509_CRT_PARSE_C)
10552static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010553{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010554 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010555
10556 while( cur != NULL )
10557 {
10558 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010559 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010560 cur = next;
10561 }
10562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010563#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010564
Hanno Becker0271f962018-08-16 13:23:47 +010010565#if defined(MBEDTLS_SSL_PROTO_DTLS)
10566
10567static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10568{
10569 unsigned offset;
10570 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10571
10572 if( hs == NULL )
10573 return;
10574
Hanno Becker283f5ef2018-08-24 09:34:47 +010010575 ssl_free_buffered_record( ssl );
10576
Hanno Becker0271f962018-08-16 13:23:47 +010010577 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010578 ssl_buffering_free_slot( ssl, offset );
10579}
10580
10581static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10582 uint8_t slot )
10583{
10584 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10585 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010586
10587 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10588 return;
10589
Hanno Beckere605b192018-08-21 15:59:07 +010010590 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010591 {
Hanno Beckere605b192018-08-21 15:59:07 +010010592 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010593 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010594 mbedtls_free( hs_buf->data );
10595 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010596 }
10597}
10598
10599#endif /* MBEDTLS_SSL_PROTO_DTLS */
10600
Gilles Peskine9b562d52018-04-25 20:32:43 +020010601void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010602{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010603 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10604
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010605 if( handshake == NULL )
10606 return;
10607
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010608#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10609 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10610 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010611 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010612 handshake->async_in_progress = 0;
10613 }
10614#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10615
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010616#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10617 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10618 mbedtls_md5_free( &handshake->fin_md5 );
10619 mbedtls_sha1_free( &handshake->fin_sha1 );
10620#endif
10621#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10622#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010623#if defined(MBEDTLS_USE_PSA_CRYPTO)
10624 psa_hash_abort( &handshake->fin_sha256_psa );
10625#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010626 mbedtls_sha256_free( &handshake->fin_sha256 );
10627#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010628#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010629#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010630#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010631 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010632#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010633 mbedtls_sha512_free( &handshake->fin_sha512 );
10634#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010635#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010636#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010638#if defined(MBEDTLS_DHM_C)
10639 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010640#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010641#if defined(MBEDTLS_ECDH_C)
10642 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010643#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010644#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010645 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010646#if defined(MBEDTLS_SSL_CLI_C)
10647 mbedtls_free( handshake->ecjpake_cache );
10648 handshake->ecjpake_cache = NULL;
10649 handshake->ecjpake_cache_len = 0;
10650#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010651#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010652
Janos Follath4ae5c292016-02-10 11:27:43 +000010653#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10654 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010655 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010657#endif
10658
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010659#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10660 if( handshake->psk != NULL )
10661 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010662 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010663 mbedtls_free( handshake->psk );
10664 }
10665#endif
10666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010667#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10668 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010669 /*
10670 * Free only the linked list wrapper, not the keys themselves
10671 * since the belong to the SNI callback
10672 */
10673 if( handshake->sni_key_cert != NULL )
10674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010676
10677 while( cur != NULL )
10678 {
10679 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010680 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010681 cur = next;
10682 }
10683 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010685
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010686#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010687 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010688 if( handshake->ecrs_peer_cert != NULL )
10689 {
10690 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10691 mbedtls_free( handshake->ecrs_peer_cert );
10692 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010693#endif
10694
Hanno Becker75173122019-02-06 16:18:31 +000010695#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10696 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10697 mbedtls_pk_free( &handshake->peer_pubkey );
10698#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010700#if defined(MBEDTLS_SSL_PROTO_DTLS)
10701 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010702 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010703 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010704#endif
10705
Hanno Becker4a63ed42019-01-08 11:39:35 +000010706#if defined(MBEDTLS_ECDH_C) && \
10707 defined(MBEDTLS_USE_PSA_CRYPTO)
10708 psa_destroy_key( handshake->ecdh_psa_privkey );
10709#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10710
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010711 mbedtls_platform_zeroize( handshake,
10712 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010713}
10714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010715void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010716{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010717 if( session == NULL )
10718 return;
10719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010721 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010722#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010723
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010724#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010725 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010726#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010727
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010728 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010729}
10730
Paul Bakker5121ce52009-01-03 21:22:43 +000010731/*
10732 * Free an SSL context
10733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010734void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010735{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010736 if( ssl == NULL )
10737 return;
10738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010740
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010741 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010742 {
Angus Grattond8213d02016-05-25 20:56:48 +100010743 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010744 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010745 }
10746
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010747 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010748 {
Angus Grattond8213d02016-05-25 20:56:48 +100010749 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010750 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010751 }
10752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010753#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010754 if( ssl->compress_buf != NULL )
10755 {
Angus Grattond8213d02016-05-25 20:56:48 +100010756 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010757 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010758 }
10759#endif
10760
Paul Bakker48916f92012-09-16 19:57:18 +000010761 if( ssl->transform )
10762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010763 mbedtls_ssl_transform_free( ssl->transform );
10764 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010765 }
10766
10767 if( ssl->handshake )
10768 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010769 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010770 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10771 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010773 mbedtls_free( ssl->handshake );
10774 mbedtls_free( ssl->transform_negotiate );
10775 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010776 }
10777
Paul Bakkerc0463502013-02-14 11:19:38 +010010778 if( ssl->session )
10779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010780 mbedtls_ssl_session_free( ssl->session );
10781 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010782 }
10783
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010784#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010785 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010786 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010787 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010788 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010789 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010790#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10793 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10796 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010797 }
10798#endif
10799
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010800#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010801 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010802#endif
10803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010805
Paul Bakker86f04f42013-02-14 11:20:09 +010010806 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010807 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010808}
10809
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010810/*
10811 * Initialze mbedtls_ssl_config
10812 */
10813void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10814{
10815 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10816}
10817
Simon Butcherc97b6972015-12-27 23:48:17 +000010818#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010819static int ssl_preset_default_hashes[] = {
10820#if defined(MBEDTLS_SHA512_C)
10821 MBEDTLS_MD_SHA512,
10822 MBEDTLS_MD_SHA384,
10823#endif
10824#if defined(MBEDTLS_SHA256_C)
10825 MBEDTLS_MD_SHA256,
10826 MBEDTLS_MD_SHA224,
10827#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010828#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010829 MBEDTLS_MD_SHA1,
10830#endif
10831 MBEDTLS_MD_NONE
10832};
Simon Butcherc97b6972015-12-27 23:48:17 +000010833#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010834
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010835static int ssl_preset_suiteb_ciphersuites[] = {
10836 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10837 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10838 0
10839};
10840
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010841#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010842static int ssl_preset_suiteb_hashes[] = {
10843 MBEDTLS_MD_SHA256,
10844 MBEDTLS_MD_SHA384,
10845 MBEDTLS_MD_NONE
10846};
10847#endif
10848
10849#if defined(MBEDTLS_ECP_C)
10850static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010851#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010852 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010853#endif
10854#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010855 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010856#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010857 MBEDTLS_ECP_DP_NONE
10858};
10859#endif
10860
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010861/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010862 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010863 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010864int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010865 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010866{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010867#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010868 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010869#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010870
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010871 /* Use the functions here so that they are covered in tests,
10872 * but otherwise access member directly for efficiency */
10873 mbedtls_ssl_conf_endpoint( conf, endpoint );
10874 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010875
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010876 /*
10877 * Things that are common to all presets
10878 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010879#if defined(MBEDTLS_SSL_CLI_C)
10880 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10881 {
10882 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10883#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10884 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10885#endif
10886 }
10887#endif
10888
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010889#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010890 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010891#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010892
10893#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10894 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10895#endif
10896
10897#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10898 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10899#endif
10900
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010901#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10902 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10903#endif
10904
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010905#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010906 conf->f_cookie_write = ssl_cookie_write_dummy;
10907 conf->f_cookie_check = ssl_cookie_check_dummy;
10908#endif
10909
10910#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10911 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10912#endif
10913
Janos Follath088ce432017-04-10 12:42:31 +010010914#if defined(MBEDTLS_SSL_SRV_C)
10915 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10916#endif
10917
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010918#if defined(MBEDTLS_SSL_PROTO_DTLS)
10919 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10920 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10921#endif
10922
10923#if defined(MBEDTLS_SSL_RENEGOTIATION)
10924 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010925 memset( conf->renego_period, 0x00, 2 );
10926 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010927#endif
10928
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010929#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10930 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10931 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010932 const unsigned char dhm_p[] =
10933 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10934 const unsigned char dhm_g[] =
10935 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10936
Hanno Beckera90658f2017-10-04 15:29:08 +010010937 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10938 dhm_p, sizeof( dhm_p ),
10939 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010940 {
10941 return( ret );
10942 }
10943 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010944#endif
10945
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010946 /*
10947 * Preset-specific defaults
10948 */
10949 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010950 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010951 /*
10952 * NSA Suite B
10953 */
10954 case MBEDTLS_SSL_PRESET_SUITEB:
10955 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10956 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10957 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10958 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10959
10960 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10961 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10962 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10963 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10964 ssl_preset_suiteb_ciphersuites;
10965
10966#if defined(MBEDTLS_X509_CRT_PARSE_C)
10967 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010968#endif
10969
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010970#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010971 conf->sig_hashes = ssl_preset_suiteb_hashes;
10972#endif
10973
10974#if defined(MBEDTLS_ECP_C)
10975 conf->curve_list = ssl_preset_suiteb_curves;
10976#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010977 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010978
10979 /*
10980 * Default
10981 */
10982 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010983 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10984 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10985 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10986 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10987 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10988 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10989 MBEDTLS_SSL_MIN_MINOR_VERSION :
10990 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010991 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10992 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10993
10994#if defined(MBEDTLS_SSL_PROTO_DTLS)
10995 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10996 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10997#endif
10998
10999 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11000 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11001 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11002 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11003 mbedtls_ssl_list_ciphersuites();
11004
11005#if defined(MBEDTLS_X509_CRT_PARSE_C)
11006 conf->cert_profile = &mbedtls_x509_crt_profile_default;
11007#endif
11008
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011009#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011010 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011011#endif
11012
11013#if defined(MBEDTLS_ECP_C)
11014 conf->curve_list = mbedtls_ecp_grp_id_list();
11015#endif
11016
11017#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
11018 conf->dhm_min_bitlen = 1024;
11019#endif
11020 }
11021
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011022 return( 0 );
11023}
11024
11025/*
11026 * Free mbedtls_ssl_config
11027 */
11028void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
11029{
11030#if defined(MBEDTLS_DHM_C)
11031 mbedtls_mpi_free( &conf->dhm_P );
11032 mbedtls_mpi_free( &conf->dhm_G );
11033#endif
11034
11035#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11036 if( conf->psk != NULL )
11037 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011038 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011039 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000011040 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011041 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090011042 }
11043
11044 if( conf->psk_identity != NULL )
11045 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011046 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090011047 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000011048 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011049 conf->psk_identity_len = 0;
11050 }
11051#endif
11052
11053#if defined(MBEDTLS_X509_CRT_PARSE_C)
11054 ssl_key_cert_free( conf->key_cert );
11055#endif
11056
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011057 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011058}
11059
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011060#if defined(MBEDTLS_PK_C) && \
11061 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011062/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011063 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011065unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011067#if defined(MBEDTLS_RSA_C)
11068 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
11069 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011071#if defined(MBEDTLS_ECDSA_C)
11072 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
11073 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011075 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011076}
11077
Hanno Becker7e5437a2017-04-28 17:15:26 +010011078unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11079{
11080 switch( type ) {
11081 case MBEDTLS_PK_RSA:
11082 return( MBEDTLS_SSL_SIG_RSA );
11083 case MBEDTLS_PK_ECDSA:
11084 case MBEDTLS_PK_ECKEY:
11085 return( MBEDTLS_SSL_SIG_ECDSA );
11086 default:
11087 return( MBEDTLS_SSL_SIG_ANON );
11088 }
11089}
11090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011091mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011092{
11093 switch( sig )
11094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011095#if defined(MBEDTLS_RSA_C)
11096 case MBEDTLS_SSL_SIG_RSA:
11097 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011099#if defined(MBEDTLS_ECDSA_C)
11100 case MBEDTLS_SSL_SIG_ECDSA:
11101 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011102#endif
11103 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011104 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011105 }
11106}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011107#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011108
Hanno Becker7e5437a2017-04-28 17:15:26 +010011109#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11110 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11111
11112/* Find an entry in a signature-hash set matching a given hash algorithm. */
11113mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11114 mbedtls_pk_type_t sig_alg )
11115{
11116 switch( sig_alg )
11117 {
11118 case MBEDTLS_PK_RSA:
11119 return( set->rsa );
11120 case MBEDTLS_PK_ECDSA:
11121 return( set->ecdsa );
11122 default:
11123 return( MBEDTLS_MD_NONE );
11124 }
11125}
11126
11127/* Add a signature-hash-pair to a signature-hash set */
11128void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11129 mbedtls_pk_type_t sig_alg,
11130 mbedtls_md_type_t md_alg )
11131{
11132 switch( sig_alg )
11133 {
11134 case MBEDTLS_PK_RSA:
11135 if( set->rsa == MBEDTLS_MD_NONE )
11136 set->rsa = md_alg;
11137 break;
11138
11139 case MBEDTLS_PK_ECDSA:
11140 if( set->ecdsa == MBEDTLS_MD_NONE )
11141 set->ecdsa = md_alg;
11142 break;
11143
11144 default:
11145 break;
11146 }
11147}
11148
11149/* Allow exactly one hash algorithm for each signature. */
11150void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11151 mbedtls_md_type_t md_alg )
11152{
11153 set->rsa = md_alg;
11154 set->ecdsa = md_alg;
11155}
11156
11157#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11158 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11159
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011160/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011161 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011163mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011164{
11165 switch( hash )
11166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011167#if defined(MBEDTLS_MD5_C)
11168 case MBEDTLS_SSL_HASH_MD5:
11169 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011171#if defined(MBEDTLS_SHA1_C)
11172 case MBEDTLS_SSL_HASH_SHA1:
11173 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011174#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011175#if defined(MBEDTLS_SHA256_C)
11176 case MBEDTLS_SSL_HASH_SHA224:
11177 return( MBEDTLS_MD_SHA224 );
11178 case MBEDTLS_SSL_HASH_SHA256:
11179 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011181#if defined(MBEDTLS_SHA512_C)
11182 case MBEDTLS_SSL_HASH_SHA384:
11183 return( MBEDTLS_MD_SHA384 );
11184 case MBEDTLS_SSL_HASH_SHA512:
11185 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011186#endif
11187 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011188 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011189 }
11190}
11191
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011192/*
11193 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11194 */
11195unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11196{
11197 switch( md )
11198 {
11199#if defined(MBEDTLS_MD5_C)
11200 case MBEDTLS_MD_MD5:
11201 return( MBEDTLS_SSL_HASH_MD5 );
11202#endif
11203#if defined(MBEDTLS_SHA1_C)
11204 case MBEDTLS_MD_SHA1:
11205 return( MBEDTLS_SSL_HASH_SHA1 );
11206#endif
11207#if defined(MBEDTLS_SHA256_C)
11208 case MBEDTLS_MD_SHA224:
11209 return( MBEDTLS_SSL_HASH_SHA224 );
11210 case MBEDTLS_MD_SHA256:
11211 return( MBEDTLS_SSL_HASH_SHA256 );
11212#endif
11213#if defined(MBEDTLS_SHA512_C)
11214 case MBEDTLS_MD_SHA384:
11215 return( MBEDTLS_SSL_HASH_SHA384 );
11216 case MBEDTLS_MD_SHA512:
11217 return( MBEDTLS_SSL_HASH_SHA512 );
11218#endif
11219 default:
11220 return( MBEDTLS_SSL_HASH_NONE );
11221 }
11222}
11223
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011224#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011225/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011226 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011227 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011228 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011229int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011230{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011231 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011232
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011233 if( ssl->conf->curve_list == NULL )
11234 return( -1 );
11235
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011236 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011237 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011238 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011239
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011240 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011241}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011242#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011243
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011244#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011245/*
11246 * Check if a hash proposed by the peer is in our list.
11247 * Return 0 if we're willing to use it, -1 otherwise.
11248 */
11249int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11250 mbedtls_md_type_t md )
11251{
11252 const int *cur;
11253
11254 if( ssl->conf->sig_hashes == NULL )
11255 return( -1 );
11256
11257 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11258 if( *cur == (int) md )
11259 return( 0 );
11260
11261 return( -1 );
11262}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011263#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011265#if defined(MBEDTLS_X509_CRT_PARSE_C)
11266int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11267 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011268 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011269 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011270{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011271 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011272#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011273 int usage = 0;
11274#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011275#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011276 const char *ext_oid;
11277 size_t ext_len;
11278#endif
11279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011280#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11281 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011282 ((void) cert);
11283 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011284 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011285#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011287#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11288 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011289 {
11290 /* Server part of the key exchange */
11291 switch( ciphersuite->key_exchange )
11292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011293 case MBEDTLS_KEY_EXCHANGE_RSA:
11294 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011295 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011296 break;
11297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011298 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11299 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11300 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11301 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011302 break;
11303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011304 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11305 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011306 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011307 break;
11308
11309 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011310 case MBEDTLS_KEY_EXCHANGE_NONE:
11311 case MBEDTLS_KEY_EXCHANGE_PSK:
11312 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11313 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011314 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011315 usage = 0;
11316 }
11317 }
11318 else
11319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011320 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11321 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011322 }
11323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011324 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011325 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011326 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011327 ret = -1;
11328 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011329#else
11330 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011331#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011333#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11334 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011336 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11337 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011338 }
11339 else
11340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011341 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11342 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011343 }
11344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011345 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011346 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011347 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011348 ret = -1;
11349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011350#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011351
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011352 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011354#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011355
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011356/*
11357 * Convert version numbers to/from wire format
11358 * and, for DTLS, to/from TLS equivalent.
11359 *
11360 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011361 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011362 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11363 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011365void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011366 unsigned char ver[2] )
11367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011368#if defined(MBEDTLS_SSL_PROTO_DTLS)
11369 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011371 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011372 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11373
11374 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11375 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11376 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011377 else
11378#else
11379 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011380#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011381 {
11382 ver[0] = (unsigned char) major;
11383 ver[1] = (unsigned char) minor;
11384 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011385}
11386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011387void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011388 const unsigned char ver[2] )
11389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011390#if defined(MBEDTLS_SSL_PROTO_DTLS)
11391 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011392 {
11393 *major = 255 - ver[0] + 2;
11394 *minor = 255 - ver[1] + 1;
11395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011396 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011397 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11398 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011399 else
11400#else
11401 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011402#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011403 {
11404 *major = ver[0];
11405 *minor = ver[1];
11406 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011407}
11408
Simon Butcher99000142016-10-13 17:21:01 +010011409int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11410{
11411#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11412 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11413 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11414
11415 switch( md )
11416 {
11417#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11418#if defined(MBEDTLS_MD5_C)
11419 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011420 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011421#endif
11422#if defined(MBEDTLS_SHA1_C)
11423 case MBEDTLS_SSL_HASH_SHA1:
11424 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11425 break;
11426#endif
11427#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11428#if defined(MBEDTLS_SHA512_C)
11429 case MBEDTLS_SSL_HASH_SHA384:
11430 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11431 break;
11432#endif
11433#if defined(MBEDTLS_SHA256_C)
11434 case MBEDTLS_SSL_HASH_SHA256:
11435 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11436 break;
11437#endif
11438 default:
11439 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11440 }
11441
11442 return 0;
11443#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11444 (void) ssl;
11445 (void) md;
11446
11447 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11448#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11449}
11450
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011451#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11452 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11453int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11454 unsigned char *output,
11455 unsigned char *data, size_t data_len )
11456{
11457 int ret = 0;
11458 mbedtls_md5_context mbedtls_md5;
11459 mbedtls_sha1_context mbedtls_sha1;
11460
11461 mbedtls_md5_init( &mbedtls_md5 );
11462 mbedtls_sha1_init( &mbedtls_sha1 );
11463
11464 /*
11465 * digitally-signed struct {
11466 * opaque md5_hash[16];
11467 * opaque sha_hash[20];
11468 * };
11469 *
11470 * md5_hash
11471 * MD5(ClientHello.random + ServerHello.random
11472 * + ServerParams);
11473 * sha_hash
11474 * SHA(ClientHello.random + ServerHello.random
11475 * + ServerParams);
11476 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011477 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011478 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011480 goto exit;
11481 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011482 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011483 ssl->handshake->randbytes, 64 ) ) != 0 )
11484 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011486 goto exit;
11487 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011488 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011489 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011491 goto exit;
11492 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011493 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011494 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011496 goto exit;
11497 }
11498
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011499 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011500 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011502 goto exit;
11503 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011504 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011505 ssl->handshake->randbytes, 64 ) ) != 0 )
11506 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011508 goto exit;
11509 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011510 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011511 data_len ) ) != 0 )
11512 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011514 goto exit;
11515 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011516 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011517 output + 16 ) ) != 0 )
11518 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011520 goto exit;
11521 }
11522
11523exit:
11524 mbedtls_md5_free( &mbedtls_md5 );
11525 mbedtls_sha1_free( &mbedtls_sha1 );
11526
11527 if( ret != 0 )
11528 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11529 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11530
11531 return( ret );
11532
11533}
11534#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11535 MBEDTLS_SSL_PROTO_TLS1_1 */
11536
11537#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11538 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011539
11540#if defined(MBEDTLS_USE_PSA_CRYPTO)
11541int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11542 unsigned char *hash, size_t *hashlen,
11543 unsigned char *data, size_t data_len,
11544 mbedtls_md_type_t md_alg )
11545{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011546 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011547 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011548 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11549
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011551
11552 if( ( status = psa_hash_setup( &hash_operation,
11553 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011554 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011555 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011556 goto exit;
11557 }
11558
Andrzej Kurek814feff2019-01-14 04:35:19 -050011559 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11560 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011561 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011562 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011563 goto exit;
11564 }
11565
Andrzej Kurek814feff2019-01-14 04:35:19 -050011566 if( ( status = psa_hash_update( &hash_operation,
11567 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011568 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011569 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011570 goto exit;
11571 }
11572
Andrzej Kurek814feff2019-01-14 04:35:19 -050011573 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11574 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011575 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011576 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011577 goto exit;
11578 }
11579
11580exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011581 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011582 {
11583 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11584 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011585 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011586 {
11587 case PSA_ERROR_NOT_SUPPORTED:
11588 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011589 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011590 case PSA_ERROR_BUFFER_TOO_SMALL:
11591 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11592 case PSA_ERROR_INSUFFICIENT_MEMORY:
11593 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11594 default:
11595 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11596 }
11597 }
11598 return( 0 );
11599}
11600
11601#else
11602
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011603int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011604 unsigned char *hash, size_t *hashlen,
11605 unsigned char *data, size_t data_len,
11606 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011607{
11608 int ret = 0;
11609 mbedtls_md_context_t ctx;
11610 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011611 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011612
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011614
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011615 mbedtls_md_init( &ctx );
11616
11617 /*
11618 * digitally-signed struct {
11619 * opaque client_random[32];
11620 * opaque server_random[32];
11621 * ServerDHParams params;
11622 * };
11623 */
11624 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11625 {
11626 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11627 goto exit;
11628 }
11629 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11630 {
11631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11632 goto exit;
11633 }
11634 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11635 {
11636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11637 goto exit;
11638 }
11639 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11640 {
11641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11642 goto exit;
11643 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011644 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011645 {
11646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11647 goto exit;
11648 }
11649
11650exit:
11651 mbedtls_md_free( &ctx );
11652
11653 if( ret != 0 )
11654 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11655 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11656
11657 return( ret );
11658}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011659#endif /* MBEDTLS_USE_PSA_CRYPTO */
11660
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011661#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11662 MBEDTLS_SSL_PROTO_TLS1_2 */
11663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011664#endif /* MBEDTLS_SSL_TLS_C */