blob: 822d972f6e3b8583c3e42970c0155560c69c582e [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é-Gonnard9b108c22019-05-06 13:32:17 +0200993 * - [in] session: used: ciphersuite, encrypt_then_mac, master, compression
994 * - [in] tls_prf: pointer to PRF to use for key derivation
995 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +0200996 * - [in] minor_ver: SSL/TLS minor version
997 * - [in] endpoint: client or server
998 * - [in] ssl: optionally used for:
999 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1000 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1001 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001002 */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001003static int ssl_populate_transform( mbedtls_ssl_transform *transform,
1004 const mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001005 ssl_tls_prf_t tls_prf,
1006 const unsigned char randbytes[64],
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001007 int minor_ver,
1008 unsigned endpoint,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001009 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001010{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001011 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001012#if defined(MBEDTLS_USE_PSA_CRYPTO)
1013 int psa_fallthrough;
1014#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 unsigned char keyblk[256];
1016 unsigned char *key1;
1017 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001018 unsigned char *mac_enc;
1019 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001020 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001021 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001022 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001023 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 const mbedtls_cipher_info_t *cipher_info;
1025 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001026
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001027#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1028 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1029 !defined(MBEDTLS_DEBUG_C)
1030 (void) ssl;
1031#endif
1032
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001033 /* Copy info about negotiated version and extensions */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001034#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1035 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001036 transform->encrypt_then_mac = session->encrypt_then_mac;
1037#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001038 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001039
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001040 /*
1041 * Get various info structures
1042 */
1043 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1044 if( ciphersuite_info == NULL )
1045 {
1046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
1047 session->ciphersuite ) );
1048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1049 }
1050
Hanno Beckere694c3e2017-12-27 21:34:08 +00001051 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001052 if( cipher_info == NULL )
1053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001055 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001057 }
1058
Hanno Beckere694c3e2017-12-27 21:34:08 +00001059 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001060 if( md_info == NULL )
1061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001063 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001065 }
1066
Hanno Beckera0e20d02019-05-15 14:03:01 +01001067#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001068 /* Copy own and peer's CID if the use of the CID
1069 * extension has been negotiated. */
1070 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1071 {
1072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001073
Hanno Becker05154c32019-05-03 15:23:51 +01001074 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001075 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001076 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001077 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001078
1079 transform->out_cid_len = ssl->handshake->peer_cid_len;
1080 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1081 ssl->handshake->peer_cid_len );
1082 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1083 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001084 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001085#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001086
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001088 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 */
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001090 ret = tls_prf( session->master, 48, "key expansion",
1091 randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001092 if( ret != 0 )
1093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001095 return( ret );
1096 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1099 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1100 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001101 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 /*
1105 * Determine the appropriate key, IV and MAC length.
1106 */
Paul Bakker68884e32013-01-07 18:20:04 +01001107
Hanno Becker88aaf652017-12-27 08:17:40 +00001108 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001109
Hanno Becker8031d062018-01-03 15:32:31 +00001110#if defined(MBEDTLS_GCM_C) || \
1111 defined(MBEDTLS_CCM_C) || \
1112 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001114 cipher_info->mode == MBEDTLS_MODE_CCM ||
1115 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001117 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001118
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001119 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001120 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001121 transform->taglen =
1122 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001123
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001124 /* All modes haves 96-bit IVs;
1125 * GCM and CCM has 4 implicit and 8 explicit bytes
1126 * ChachaPoly has all 12 bytes implicit
1127 */
Paul Bakker68884e32013-01-07 18:20:04 +01001128 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001129 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1130 transform->fixed_ivlen = 12;
1131 else
1132 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001133
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001134 /* Minimum length of encrypted record */
1135 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001136 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001137 }
1138 else
Hanno Becker8031d062018-01-03 15:32:31 +00001139#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1140#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1141 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1142 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001143 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001144 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1146 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001149 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001150 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001152 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001153 mac_key_len = mbedtls_md_get_size( md_info );
1154 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001157 /*
1158 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1159 * (rfc 6066 page 13 or rfc 2104 section 4),
1160 * so we only need to adjust the length here.
1161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001165
1166#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1167 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001168 * HMAC implementation which also truncates the key
1169 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001170 mac_key_len = transform->maclen;
1171#endif
1172 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001174
1175 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001176 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001178 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001180 transform->minlen = transform->maclen;
1181 else
Paul Bakker68884e32013-01-07 18:20:04 +01001182 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001183 /*
1184 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001185 * 1. if EtM is in use: one block plus MAC
1186 * otherwise: * first multiple of blocklen greater than maclen
1187 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1190 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001191 {
1192 transform->minlen = transform->maclen
1193 + cipher_info->block_size;
1194 }
1195 else
1196#endif
1197 {
1198 transform->minlen = transform->maclen
1199 + cipher_info->block_size
1200 - transform->maclen % cipher_info->block_size;
1201 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001204 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1205 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001206 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001207 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001208#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001210 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1211 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001212 {
1213 transform->minlen += transform->ivlen;
1214 }
1215 else
1216#endif
1217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001219 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1220 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001221 }
Paul Bakker68884e32013-01-07 18:20:04 +01001222 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 }
Hanno Becker8031d062018-01-03 15:32:31 +00001224 else
1225#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1226 {
1227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1228 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1229 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
Hanno Becker88aaf652017-12-27 08:17:40 +00001231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1232 (unsigned) keylen,
1233 (unsigned) transform->minlen,
1234 (unsigned) transform->ivlen,
1235 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001236
1237 /*
1238 * Finally setup the cipher contexts, IVs and MAC secrets.
1239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001241 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001243 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001244 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Paul Bakker68884e32013-01-07 18:20:04 +01001246 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001247 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001249 /*
1250 * This is not used in TLS v1.1.
1251 */
Paul Bakker48916f92012-09-16 19:57:18 +00001252 iv_copy_len = ( transform->fixed_ivlen ) ?
1253 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001254 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1255 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001256 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 }
1258 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#endif /* MBEDTLS_SSL_CLI_C */
1260#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001261 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001263 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001264 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
Hanno Becker81c7b182017-11-09 18:39:33 +00001266 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001267 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001268
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001269 /*
1270 * This is not used in TLS v1.1.
1271 */
Paul Bakker48916f92012-09-16 19:57:18 +00001272 iv_copy_len = ( transform->fixed_ivlen ) ?
1273 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001274 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1275 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001276 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001278 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001282 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1283 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001284 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001285
Hanno Beckerd56ed242018-01-03 15:32:51 +00001286#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001288 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001289 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001290 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001293 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1294 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001295 }
1296
Hanno Becker81c7b182017-11-09 18:39:33 +00001297 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1298 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001299 }
1300 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1302#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1303 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001304 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001305 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001306 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1307 For AEAD-based ciphersuites, there is nothing to do here. */
1308 if( mac_key_len != 0 )
1309 {
1310 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1311 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1312 }
Paul Bakker68884e32013-01-07 18:20:04 +01001313 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001314 else
1315#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001318 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1319 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001320 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001321#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1324 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001325 {
1326 int ret = 0;
1327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001329
Hanno Becker88aaf652017-12-27 08:17:40 +00001330 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001331 transform->iv_enc, transform->iv_dec,
1332 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001333 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001334 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001337 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1338 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001339 }
1340 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001341#else
1342 ((void) mac_dec);
1343 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001345
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001346#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1347 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001348 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001349 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1350 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001351 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001352 iv_copy_len );
1353 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001354
1355 if( ssl->conf->f_export_keys_ext != NULL )
1356 {
1357 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1358 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001359 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001360 iv_copy_len,
Manuel Pégourié-Gonnard344460c2019-07-25 13:17:38 +02001361 /* work around bug in exporter type */
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001362 (unsigned char *) randbytes + 32,
1363 (unsigned char *) randbytes,
1364 tls_prf_get_type( tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001365 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001366#endif
1367
Hanno Beckerf704bef2018-11-16 15:21:18 +00001368#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001369
1370 /* Only use PSA-based ciphers for TLS-1.2.
1371 * That's relevant at least for TLS-1.0, where
1372 * we assume that mbedtls_cipher_crypt() updates
1373 * the structure field for the IV, which the PSA-based
1374 * implementation currently doesn't. */
1375#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1376 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001377 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001378 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001379 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001380 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1381 {
1382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001383 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001384 }
1385
1386 if( ret == 0 )
1387 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001389 psa_fallthrough = 0;
1390 }
1391 else
1392 {
1393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1394 psa_fallthrough = 1;
1395 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001396 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001397 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001398 psa_fallthrough = 1;
1399#else
1400 psa_fallthrough = 1;
1401#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001402
Hanno Beckercb1cc802018-11-17 22:27:38 +00001403 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001404#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001405 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001406 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001409 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001410 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001411
Hanno Beckerf704bef2018-11-16 15:21:18 +00001412#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001413 /* Only use PSA-based ciphers for TLS-1.2.
1414 * That's relevant at least for TLS-1.0, where
1415 * we assume that mbedtls_cipher_crypt() updates
1416 * the structure field for the IV, which the PSA-based
1417 * implementation currently doesn't. */
1418#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1419 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001420 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001421 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001422 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001423 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1424 {
1425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001426 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001427 }
1428
1429 if( ret == 0 )
1430 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001431 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001432 psa_fallthrough = 0;
1433 }
1434 else
1435 {
1436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1437 psa_fallthrough = 1;
1438 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001439 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001440 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001441 psa_fallthrough = 1;
1442#else
1443 psa_fallthrough = 1;
1444#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001445
Hanno Beckercb1cc802018-11-17 22:27:38 +00001446 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001447#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001448 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001449 cipher_info ) ) != 0 )
1450 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001452 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001453 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001456 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001460 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001461 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001464 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001468 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001469 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_CIPHER_MODE_CBC)
1472 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1475 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001478 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001479 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1482 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001485 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001486 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001489
Paul Bakker5121ce52009-01-03 21:22:43 +00001490
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001491 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001496
Paul Bakker48916f92012-09-16 19:57:18 +00001497 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1498 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001499
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001500 if( deflateInit( &transform->ctx_deflate,
1501 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001502 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001505 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1506 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001507 }
1508 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001510
Ron Eldore6992702019-05-07 18:27:13 +03001511end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001512 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Ron Eldore6992702019-05-07 18:27:13 +03001513 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514}
1515
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001516/*
1517 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
1518 *
1519 * Inputs:
1520 * - SSL/TLS minor version
1521 * - hash associated with the ciphersuite (only used by TLS 1.2)
1522 *
1523 * Ouputs:
1524 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1525 */
1526static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1527 int minor_ver,
1528 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001529{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001530#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1531 (void) hash;
1532#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001533
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001534#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001535 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001536 {
1537 handshake->tls_prf = ssl3_prf;
1538 handshake->calc_verify = ssl_calc_verify_ssl;
1539 handshake->calc_finished = ssl_calc_finished_ssl;
1540 }
1541 else
1542#endif
1543#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001544 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001545 {
1546 handshake->tls_prf = tls1_prf;
1547 handshake->calc_verify = ssl_calc_verify_tls;
1548 handshake->calc_finished = ssl_calc_finished_tls;
1549 }
1550 else
1551#endif
1552#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1553#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001554 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1555 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001556 {
1557 handshake->tls_prf = tls_prf_sha384;
1558 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1559 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1560 }
1561 else
1562#endif
1563#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001564 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001565 {
1566 handshake->tls_prf = tls_prf_sha256;
1567 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1568 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1569 }
1570 else
1571#endif
1572#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1573 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1575 }
1576
1577 return( 0 );
1578}
1579
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001580/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001581 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001582 *
1583 * Parameters:
1584 * [in/out] handshake
1585 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001586 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001587 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001588 * [out] master
1589 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1590 * debug: conf->f_dbg, conf->p_dbg
1591 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001592 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001593 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001594static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001595 unsigned char *master,
Manuel Pégourié-Gonnard0d56aaa2019-05-03 09:58:33 +02001596 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001597{
1598 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001599
1600 /* cf. RFC 5246, Section 8.1:
1601 * "The master secret is always exactly 48 bytes in length." */
1602 size_t const master_secret_len = 48;
1603
1604#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1605 unsigned char session_hash[48];
1606#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1607
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001608 /* The label for the KDF used for key expansion.
1609 * This is either "master secret" or "extended master secret"
1610 * depending on whether the Extended Master Secret extension
1611 * is used. */
1612 char const *lbl = "master secret";
1613
1614 /* The salt for the KDF used for key expansion.
1615 * - If the Extended Master Secret extension is not used,
1616 * this is ClientHello.Random + ServerHello.Random
1617 * (see Sect. 8.1 in RFC 5246).
1618 * - If the Extended Master Secret extension is used,
1619 * this is the transcript of the handshake so far.
1620 * (see Sect. 4 in RFC 7627). */
1621 unsigned char const *salt = handshake->randbytes;
1622 size_t salt_len = 64;
1623
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001624#if !defined(MBEDTLS_DEBUG_C) && \
1625 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1626 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1627 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
1628 (void) ssl;
1629#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001630
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001631 if( handshake->resume != 0 )
1632 {
1633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001634 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001635 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001636
1637#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001638 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001639 {
1640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001641
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001642 lbl = "extended master secret";
1643 salt = session_hash;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001644 handshake->calc_verify( ssl, session_hash, &salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001645
1646 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
1647 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001648#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1649
1650#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001651 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1652 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001653 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001654 ssl_use_opaque_psk( ssl ) == 1 )
1655 {
1656 /* Perform PSK-to-MS expansion in a single step. */
1657 psa_status_t status;
1658 psa_algorithm_t alg;
1659 psa_key_handle_t psk;
1660 psa_key_derivation_operation_t derivation =
1661 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001662 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001663
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001665
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001666 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001667 if( handshake->psk_opaque != 0 )
1668 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001669
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001670 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001671 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001672 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001673 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1674
1675 status = psa_key_derivation( &derivation, psk, alg,
1676 salt, salt_len,
1677 (unsigned char const *) lbl,
1678 (size_t) strlen( lbl ),
1679 master_secret_len );
1680 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001681 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001682 psa_key_derivation_abort( &derivation );
1683 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001684 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001685
1686 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001687 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001688 master_secret_len );
1689 if( status != PSA_SUCCESS )
1690 {
1691 psa_key_derivation_abort( &derivation );
1692 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1693 }
1694
1695 status = psa_key_derivation_abort( &derivation );
1696 if( status != PSA_SUCCESS )
1697 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1698 }
1699 else
1700#endif
1701 {
1702 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1703 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001704 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001705 master_secret_len );
1706 if( ret != 0 )
1707 {
1708 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1709 return( ret );
1710 }
1711
1712 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1713 handshake->premaster,
1714 handshake->pmslen );
1715
1716 mbedtls_platform_zeroize( handshake->premaster,
1717 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001718 }
1719
1720 return( 0 );
1721}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001722
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001723int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1724{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001725 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001726 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1727 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001728
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1730
1731 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001732 ret = ssl_set_handshake_prfs( ssl->handshake,
1733 ssl->minor_ver,
1734 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001735 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001736 {
1737 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001738 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001739 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001740
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001741 /* Compute master secret if needed */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001742 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001743 ssl->session_negotiate->master,
1744 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001745 if( ret != 0 )
1746 {
1747 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1748 return( ret );
1749 }
1750
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001751 /* Swap the client and server random values:
1752 * - MS derivation wanted client+server (RFC 5246 8.1)
1753 * - key derivation wants server+client (RFC 5246 6.3) */
1754 {
1755 unsigned char tmp[64];
1756 memcpy( tmp, ssl->handshake->randbytes, 64 );
1757 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1758 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1759 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1760 }
1761
1762 /* Populate transform structure */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001763 ret = ssl_populate_transform( ssl->transform_negotiate,
1764 ssl->session_negotiate,
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001765 ssl->handshake->tls_prf,
1766 ssl->handshake->randbytes,
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001767 ssl->minor_ver,
1768 ssl->conf->endpoint,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001769 ssl );
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001770 if( ret != 0 )
1771 {
1772 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1773 return( ret );
1774 }
1775
1776 /* We no longer need Server/ClientHello.random values */
1777 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1778 sizeof( ssl->handshake->randbytes ) );
1779
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001780 /* Allocate compression buffer */
1781#if defined(MBEDTLS_ZLIB_SUPPORT)
1782 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1783 ssl->compress_buf == NULL )
1784 {
1785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1786 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1787 if( ssl->compress_buf == NULL )
1788 {
1789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
1790 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
1791 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1792 }
1793 }
1794#endif
1795
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1797
1798 return( 0 );
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001799}
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001802void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1803 unsigned char hash[36],
1804 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001805{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001806 mbedtls_md5_context md5;
1807 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001808 unsigned char pad_1[48];
1809 unsigned char pad_2[48];
1810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001812
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001813 mbedtls_md5_init( &md5 );
1814 mbedtls_sha1_init( &sha1 );
1815
1816 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1817 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001818
Paul Bakker380da532012-04-18 16:10:25 +00001819 memset( pad_1, 0x36, 48 );
1820 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001821
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001822 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1823 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1824 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001825
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001826 mbedtls_md5_starts_ret( &md5 );
1827 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1828 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1829 mbedtls_md5_update_ret( &md5, hash, 16 );
1830 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001831
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001832 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1833 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1834 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001835
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001836 mbedtls_sha1_starts_ret( &sha1 );
1837 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1838 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1839 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1840 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001841
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001842 *hlen = 36;
1843
1844 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001846
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001847 mbedtls_md5_free( &md5 );
1848 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001849
Paul Bakker380da532012-04-18 16:10:25 +00001850 return;
1851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001855void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1856 unsigned char hash[36],
1857 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001858{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001859 mbedtls_md5_context md5;
1860 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001863
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001864 mbedtls_md5_init( &md5 );
1865 mbedtls_sha1_init( &sha1 );
1866
1867 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1868 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001869
Andrzej Kurekeb342242019-01-29 09:14:33 -05001870 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001871 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001872
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001873 *hlen = 36;
1874
1875 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001877
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001878 mbedtls_md5_free( &md5 );
1879 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001880
Paul Bakker380da532012-04-18 16:10:25 +00001881 return;
1882}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1886#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001887void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1888 unsigned char hash[32],
1889 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001890{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001891#if defined(MBEDTLS_USE_PSA_CRYPTO)
1892 size_t hash_size;
1893 psa_status_t status;
1894 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1895
1896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1897 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1898 if( status != PSA_SUCCESS )
1899 {
1900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1901 return;
1902 }
1903
1904 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1905 if( status != PSA_SUCCESS )
1906 {
1907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1908 return;
1909 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001910
1911 *hlen = 32;
1912 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1914#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001915 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001916
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001917 mbedtls_sha256_init( &sha256 );
1918
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001920
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001921 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001922 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001923
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001924 *hlen = 32;
1925
1926 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001928
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001929 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001930#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001931 return;
1932}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001936void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1937 unsigned char hash[48],
1938 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001939{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001940#if defined(MBEDTLS_USE_PSA_CRYPTO)
1941 size_t hash_size;
1942 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001943 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001944
1945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001946 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001947 if( status != PSA_SUCCESS )
1948 {
1949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1950 return;
1951 }
1952
Andrzej Kurek972fba52019-01-30 03:29:12 -05001953 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001954 if( status != PSA_SUCCESS )
1955 {
1956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1957 return;
1958 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001959
1960 *hlen = 48;
1961 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1963#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001964 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001965
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001966 mbedtls_sha512_init( &sha512 );
1967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001969
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001970 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001971 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001973 *hlen = 48;
1974
1975 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001977
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001978 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001979#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 return;
1981}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#endif /* MBEDTLS_SHA512_C */
1983#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1986int 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 +02001987{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001988 unsigned char *p = ssl->handshake->premaster;
1989 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001990 const unsigned char *psk = ssl->conf->psk;
1991 size_t psk_len = ssl->conf->psk_len;
1992
1993 /* If the psk callback was called, use its result */
1994 if( ssl->handshake->psk != NULL )
1995 {
1996 psk = ssl->handshake->psk;
1997 psk_len = ssl->handshake->psk_len;
1998 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001999
2000 /*
2001 * PMS = struct {
2002 * opaque other_secret<0..2^16-1>;
2003 * opaque psk<0..2^16-1>;
2004 * };
2005 * with "other_secret" depending on the particular key exchange
2006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2008 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002009 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002010 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002012
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002013 *(p++) = (unsigned char)( psk_len >> 8 );
2014 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002015
2016 if( end < p || (size_t)( end - p ) < psk_len )
2017 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2018
2019 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002020 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002021 }
2022 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2024#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2025 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002026 {
2027 /*
2028 * other_secret already set by the ClientKeyExchange message,
2029 * and is 48 bytes long
2030 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002031 if( end - p < 2 )
2032 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2033
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002034 *p++ = 0;
2035 *p++ = 48;
2036 p += 48;
2037 }
2038 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2040#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2041 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002042 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002043 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002044 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002045
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002046 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002048 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002049 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002052 return( ret );
2053 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002054 *(p++) = (unsigned char)( len >> 8 );
2055 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002056 p += len;
2057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002059 }
2060 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2062#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2063 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002064 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002065 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002066 size_t zlen;
2067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002069 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002070 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002073 return( ret );
2074 }
2075
2076 *(p++) = (unsigned char)( zlen >> 8 );
2077 *(p++) = (unsigned char)( zlen );
2078 p += zlen;
2079
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002080 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2081 MBEDTLS_DEBUG_ECDH_Z );
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_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002088 }
2089
2090 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002091 if( end - p < 2 )
2092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002093
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002094 *(p++) = (unsigned char)( psk_len >> 8 );
2095 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002096
2097 if( end < p || (size_t)( end - p ) < psk_len )
2098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2099
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002100 memcpy( p, psk, psk_len );
2101 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002102
2103 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2104
2105 return( 0 );
2106}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002110/*
2111 * SSLv3.0 MAC functions
2112 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002113#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002114static void ssl_mac( mbedtls_md_context_t *md_ctx,
2115 const unsigned char *secret,
2116 const unsigned char *buf, size_t len,
2117 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002118 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002119{
2120 unsigned char header[11];
2121 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002122 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2124 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002125
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002126 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002128 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002129 else
Paul Bakker68884e32013-01-07 18:20:04 +01002130 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
2132 memcpy( header, ctr, 8 );
2133 header[ 8] = (unsigned char) type;
2134 header[ 9] = (unsigned char)( len >> 8 );
2135 header[10] = (unsigned char)( len );
2136
Paul Bakker68884e32013-01-07 18:20:04 +01002137 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 mbedtls_md_starts( md_ctx );
2139 mbedtls_md_update( md_ctx, secret, md_size );
2140 mbedtls_md_update( md_ctx, padding, padlen );
2141 mbedtls_md_update( md_ctx, header, 11 );
2142 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002143 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002144
Paul Bakker68884e32013-01-07 18:20:04 +01002145 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 mbedtls_md_starts( md_ctx );
2147 mbedtls_md_update( md_ctx, secret, md_size );
2148 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002149 mbedtls_md_update( md_ctx, out, md_size );
2150 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002151}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002153
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002154/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002155 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002156#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002157 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2158 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2159 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2160/* This function makes sure every byte in the memory region is accessed
2161 * (in ascending addresses order) */
2162static void ssl_read_memory( unsigned char *p, size_t len )
2163{
2164 unsigned char acc = 0;
2165 volatile unsigned char force;
2166
2167 for( ; len != 0; p++, len-- )
2168 acc ^= *p;
2169
2170 force = acc;
2171 (void) force;
2172}
2173#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2174
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002175/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002177 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002178
Hanno Beckera0e20d02019-05-15 14:03:01 +01002179#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002180/* This functions transforms a DTLS plaintext fragment and a record content
2181 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002182 *
2183 * struct {
2184 * opaque content[DTLSPlaintext.length];
2185 * ContentType real_type;
2186 * uint8 zeros[length_of_padding];
2187 * } DTLSInnerPlaintext;
2188 *
2189 * Input:
2190 * - `content`: The beginning of the buffer holding the
2191 * plaintext to be wrapped.
2192 * - `*content_size`: The length of the plaintext in Bytes.
2193 * - `max_len`: The number of Bytes available starting from
2194 * `content`. This must be `>= *content_size`.
2195 * - `rec_type`: The desired record content type.
2196 *
2197 * Output:
2198 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2199 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2200 *
2201 * Returns:
2202 * - `0` on success.
2203 * - A negative error code if `max_len` didn't offer enough space
2204 * for the expansion.
2205 */
2206static int ssl_cid_build_inner_plaintext( unsigned char *content,
2207 size_t *content_size,
2208 size_t remaining,
2209 uint8_t rec_type )
2210{
2211 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002212 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2213 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2214 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002215
2216 /* Write real content type */
2217 if( remaining == 0 )
2218 return( -1 );
2219 content[ len ] = rec_type;
2220 len++;
2221 remaining--;
2222
2223 if( remaining < pad )
2224 return( -1 );
2225 memset( content + len, 0, pad );
2226 len += pad;
2227 remaining -= pad;
2228
2229 *content_size = len;
2230 return( 0 );
2231}
2232
Hanno Becker07dc97d2019-05-20 15:08:01 +01002233/* This function parses a DTLSInnerPlaintext structure.
2234 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002235static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2236 size_t *content_size,
2237 uint8_t *rec_type )
2238{
2239 size_t remaining = *content_size;
2240
2241 /* Determine length of padding by skipping zeroes from the back. */
2242 do
2243 {
2244 if( remaining == 0 )
2245 return( -1 );
2246 remaining--;
2247 } while( content[ remaining ] == 0 );
2248
2249 *content_size = remaining;
2250 *rec_type = content[ remaining ];
2251
2252 return( 0 );
2253}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002254#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002255
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002256/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002257 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002258static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002259 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002260 mbedtls_record *rec )
2261{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002262 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002263 *
2264 * additional_data = seq_num + TLSCompressed.type +
2265 * TLSCompressed.version + TLSCompressed.length;
2266 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002267 * For the CID extension, this is extended as follows
2268 * (quoting draft-ietf-tls-dtls-connection-id-05,
2269 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002270 *
2271 * additional_data = seq_num + DTLSPlaintext.type +
2272 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002273 * cid +
2274 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002275 * length_of_DTLSInnerPlaintext;
2276 */
2277
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002278 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2279 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002280 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002281
Hanno Beckera0e20d02019-05-15 14:03:01 +01002282#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002283 if( rec->cid_len != 0 )
2284 {
2285 memcpy( add_data + 11, rec->cid, rec->cid_len );
2286 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2287 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2288 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2289 *add_data_len = 13 + 1 + rec->cid_len;
2290 }
2291 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002292#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002293 {
2294 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2295 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2296 *add_data_len = 13;
2297 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002298}
2299
Hanno Beckera18d1322018-01-03 14:27:32 +00002300int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2301 mbedtls_ssl_transform *transform,
2302 mbedtls_record *rec,
2303 int (*f_rng)(void *, unsigned char *, size_t),
2304 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002305{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002307 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002308 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002309 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002310 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002311 size_t post_avail;
2312
2313 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002314#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002315 ((void) ssl);
2316#endif
2317
2318 /* The PRNG is used for dynamic IV generation that's used
2319 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2320#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2321 ( defined(MBEDTLS_AES_C) || \
2322 defined(MBEDTLS_ARIA_C) || \
2323 defined(MBEDTLS_CAMELLIA_C) ) && \
2324 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2325 ((void) f_rng);
2326 ((void) p_rng);
2327#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002331 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002332 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2334 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2335 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002336 if( rec == NULL
2337 || rec->buf == NULL
2338 || rec->buf_len < rec->data_offset
2339 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002340#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002341 || rec->cid_len != 0
2342#endif
2343 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002344 {
2345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002347 }
2348
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002349 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002350 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002352 data, rec->data_len );
2353
2354 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2355
2356 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2357 {
2358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2359 (unsigned) rec->data_len,
2360 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2362 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002363
Hanno Beckera0e20d02019-05-15 14:03:01 +01002364#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002365 /*
2366 * Add CID information
2367 */
2368 rec->cid_len = transform->out_cid_len;
2369 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2370 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002371
2372 if( rec->cid_len != 0 )
2373 {
2374 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002375 * Wrap plaintext into DTLSInnerPlaintext structure.
2376 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002377 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002378 * Note that this changes `rec->data_len`, and hence
2379 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002380 */
2381 if( ssl_cid_build_inner_plaintext( data,
2382 &rec->data_len,
2383 post_avail,
2384 rec->type ) != 0 )
2385 {
2386 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2387 }
2388
2389 rec->type = MBEDTLS_SSL_MSG_CID;
2390 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002391#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002392
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002393 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2394
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002396 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 */
Hanno Becker52344c22018-01-03 15:24:20 +00002398#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 if( mode == MBEDTLS_MODE_STREAM ||
2400 ( mode == MBEDTLS_MODE_CBC
2401#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002402 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002403#endif
2404 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002406 if( post_avail < transform->maclen )
2407 {
2408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2409 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2410 }
2411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002413 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002414 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002415 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002416 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2417 data, rec->data_len, rec->ctr, rec->type, mac );
2418 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002419 }
2420 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002421#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2423 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002424 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002425 {
Hanno Becker992b6872017-11-09 18:57:39 +00002426 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2427
Hanno Beckercab87e62019-04-29 13:52:53 +01002428 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002429
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002430 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002431 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002432 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2433 data, rec->data_len );
2434 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2435 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2436
2437 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002438 }
2439 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002440#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2443 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002444 }
2445
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002446 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2447 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002448
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002449 rec->data_len += transform->maclen;
2450 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002451 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002452 }
Hanno Becker52344c22018-01-03 15:24:20 +00002453#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002455 /*
2456 * Encrypt
2457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2459 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002461 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002462 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002464 "including %d bytes of padding",
2465 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002466
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002467 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2468 transform->iv_enc, transform->ivlen,
2469 data, rec->data_len,
2470 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002473 return( ret );
2474 }
2475
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002476 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2479 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002480 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 }
Paul Bakker68884e32013-01-07 18:20:04 +01002482 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002484
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002485#if defined(MBEDTLS_GCM_C) || \
2486 defined(MBEDTLS_CCM_C) || \
2487 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002489 mode == MBEDTLS_MODE_CCM ||
2490 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002491 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002492 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002493 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002494 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002495
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002496 /* Check that there's space for both the authentication tag
2497 * and the explicit IV before and after the record content. */
2498 if( post_avail < transform->taglen ||
2499 rec->data_offset < explicit_iv_len )
2500 {
2501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2502 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2503 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002504
Paul Bakker68884e32013-01-07 18:20:04 +01002505 /*
2506 * Generate IV
2507 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002508 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2509 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002510 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002511 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002512 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2513 explicit_iv_len );
2514 /* Prefix record content with explicit IV. */
2515 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002516 }
2517 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2518 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002519 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002520 unsigned char i;
2521
2522 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2523
2524 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002525 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002526 }
2527 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002528 {
2529 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2531 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002532 }
2533
Hanno Beckercab87e62019-04-29 13:52:53 +01002534 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002535
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002536 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2537 iv, transform->ivlen );
2538 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002539 data - explicit_iv_len, explicit_iv_len );
2540 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002541 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002543 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002544 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002545
Paul Bakker68884e32013-01-07 18:20:04 +01002546 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002547 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002548 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002549
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002550 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002551 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002552 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002553 data, rec->data_len, /* source */
2554 data, &rec->data_len, /* destination */
2555 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002558 return( ret );
2559 }
2560
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002561 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2562 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002563
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002564 rec->data_len += transform->taglen + explicit_iv_len;
2565 rec->data_offset -= explicit_iv_len;
2566 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002567 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002568 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2571#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002572 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002575 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002576 size_t padlen, i;
2577 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002578
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002579 /* Currently we're always using minimal padding
2580 * (up to 255 bytes would be allowed). */
2581 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2582 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 padlen = 0;
2584
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002585 /* Check there's enough space in the buffer for the padding. */
2586 if( post_avail < padlen + 1 )
2587 {
2588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2589 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2590 }
2591
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002593 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002595 rec->data_len += padlen + 1;
2596 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002599 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002600 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2601 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002602 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002603 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002604 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002605 if( f_rng == NULL )
2606 {
2607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2609 }
2610
2611 if( rec->data_offset < transform->ivlen )
2612 {
2613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2614 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2615 }
2616
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002617 /*
2618 * Generate IV
2619 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002620 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002621 if( ret != 0 )
2622 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002623
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002624 memcpy( data - transform->ivlen, transform->iv_enc,
2625 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002626
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002627 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002631 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002632 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002633 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002635 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2636 transform->iv_enc,
2637 transform->ivlen,
2638 data, rec->data_len,
2639 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002642 return( ret );
2643 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002644
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002645 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2648 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002649 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002652 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002653 {
2654 /*
2655 * Save IV in SSL3 and TLS1
2656 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002657 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2658 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002660 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002661#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002662 {
2663 data -= transform->ivlen;
2664 rec->data_offset -= transform->ivlen;
2665 rec->data_len += transform->ivlen;
2666 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002669 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002670 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002671 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2672
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002673 /*
2674 * MAC(MAC_write_key, seq_num +
2675 * TLSCipherText.type +
2676 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002677 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002678 * IV + // except for TLS 1.0
2679 * ENC(content + padding + padding_length));
2680 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002681
2682 if( post_avail < transform->maclen)
2683 {
2684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2685 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2686 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002687
Hanno Beckercab87e62019-04-29 13:52:53 +01002688 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002691 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002692 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002693
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002694 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002695 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002696 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2697 data, rec->data_len );
2698 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2699 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002700
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002701 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002702
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002703 rec->data_len += transform->maclen;
2704 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002705 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002706 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002709 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002711 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2714 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002715 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002717 /* Make extra sure authentication was performed, exactly once */
2718 if( auth_done != 1 )
2719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002722 }
2723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
2726 return( 0 );
2727}
2728
Hanno Becker605949f2019-07-12 08:23:59 +01002729int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002730 mbedtls_ssl_transform *transform,
2731 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002732{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002733 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002735 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002736#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002737 size_t padlen = 0, correct = 1;
2738#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002739 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002740 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002741 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002742
Hanno Beckera18d1322018-01-03 14:27:32 +00002743#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002744 ((void) ssl);
2745#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002748 if( rec == NULL ||
2749 rec->buf == NULL ||
2750 rec->buf_len < rec->data_offset ||
2751 rec->buf_len - rec->data_offset < rec->data_len )
2752 {
2753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002755 }
2756
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002757 data = rec->buf + rec->data_offset;
2758 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
Hanno Beckera0e20d02019-05-15 14:03:01 +01002760#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002761 /*
2762 * Match record's CID with incoming CID.
2763 */
Hanno Becker938489a2019-05-08 13:02:22 +01002764 if( rec->cid_len != transform->in_cid_len ||
2765 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2766 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002767 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002768 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002769#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2772 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002773 {
2774 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002775 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2776 transform->iv_dec,
2777 transform->ivlen,
2778 data, rec->data_len,
2779 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002782 return( ret );
2783 }
2784
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002785 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2788 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002789 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 }
Paul Bakker68884e32013-01-07 18:20:04 +01002791 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002793#if defined(MBEDTLS_GCM_C) || \
2794 defined(MBEDTLS_CCM_C) || \
2795 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002797 mode == MBEDTLS_MODE_CCM ||
2798 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002799 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002800 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002801 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002802
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002803 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002804 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002805 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002806
2807 /* Check that there's enough space for the explicit IV
2808 * (at the beginning of the record) and the MAC (at the
2809 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002810 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002813 "+ taglen (%d)", rec->data_len,
2814 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002816 }
Paul Bakker68884e32013-01-07 18:20:04 +01002817
Hanno Beckerd96a6522019-07-10 13:55:25 +01002818#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002819 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2820 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002821 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002822
Hanno Beckerd96a6522019-07-10 13:55:25 +01002823 /* Fixed */
2824 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2825 /* Explicit */
2826 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002827 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002828 else
2829#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2830#if defined(MBEDTLS_CHACHAPOLY_C)
2831 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002832 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002833 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002834 unsigned char i;
2835
2836 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2837
2838 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002839 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002840 }
2841 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002842#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002843 {
2844 /* Reminder if we ever add an AEAD mode with a different size */
2845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2846 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2847 }
2848
Hanno Beckerd96a6522019-07-10 13:55:25 +01002849 /* Group changes to data, data_len, and add_data, because
2850 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002851 data += explicit_iv_len;
2852 rec->data_offset += explicit_iv_len;
2853 rec->data_len -= explicit_iv_len + transform->taglen;
2854
Hanno Beckercab87e62019-04-29 13:52:53 +01002855 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002856 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002857 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002858
Hanno Beckerd96a6522019-07-10 13:55:25 +01002859 /* Because of the check above, we know that there are
2860 * explicit_iv_len Bytes preceeding data, and taglen
2861 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002862 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002863 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002864
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002865 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002866 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002867 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002868
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002869 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002870 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002871 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002872 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2873 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002874 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002875 data, rec->data_len,
2876 data, &olen,
2877 data + rec->data_len,
2878 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2883 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002884
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002885 return( ret );
2886 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002887 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002888
Hanno Beckerd96a6522019-07-10 13:55:25 +01002889 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002894 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002895 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002896 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2898#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002899 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002902 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002903
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 /*
Paul Bakker45829992013-01-03 14:52:21 +01002905 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002906 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002908 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2909 {
2910 /* The ciphertext is prefixed with the CBC IV. */
2911 minlen += transform->ivlen;
2912 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002913#endif
Paul Bakker45829992013-01-03 14:52:21 +01002914
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002915 /* Size considerations:
2916 *
2917 * - The CBC cipher text must not be empty and hence
2918 * at least of size transform->ivlen.
2919 *
2920 * Together with the potential IV-prefix, this explains
2921 * the first of the two checks below.
2922 *
2923 * - The record must contain a MAC, either in plain or
2924 * encrypted, depending on whether Encrypt-then-MAC
2925 * is used or not.
2926 * - If it is, the message contains the IV-prefix,
2927 * the CBC ciphertext, and the MAC.
2928 * - If it is not, the padded plaintext, and hence
2929 * the CBC ciphertext, has at least length maclen + 1
2930 * because there is at least the padding length byte.
2931 *
2932 * As the CBC ciphertext is not empty, both cases give the
2933 * lower bound minlen + maclen + 1 on the record size, which
2934 * we test for in the second check below.
2935 */
2936 if( rec->data_len < minlen + transform->ivlen ||
2937 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002940 "+ 1 ) ( + expl IV )", rec->data_len,
2941 transform->ivlen,
2942 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002944 }
2945
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002946 /*
2947 * Authenticate before decrypt if enabled
2948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002950 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002951 {
Hanno Becker992b6872017-11-09 18:57:39 +00002952 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002955
Hanno Beckerd96a6522019-07-10 13:55:25 +01002956 /* Update data_len in tandem with add_data.
2957 *
2958 * The subtraction is safe because of the previous check
2959 * data_len >= minlen + maclen + 1.
2960 *
2961 * Afterwards, we know that data + data_len is followed by at
2962 * least maclen Bytes, which justifies the call to
2963 * mbedtls_ssl_safer_memcmp() below.
2964 *
2965 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002966 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002967 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002968
Hanno Beckerd96a6522019-07-10 13:55:25 +01002969 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002970 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2971 add_data_len );
2972 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2973 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002974 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2975 data, rec->data_len );
2976 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2977 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002978
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002979 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2980 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002981 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002982 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002983
Hanno Beckerd96a6522019-07-10 13:55:25 +01002984 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002985 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2986 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002990 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002991 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002992 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002994
2995 /*
2996 * Check length sanity
2997 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002998
2999 /* We know from above that data_len > minlen >= 0,
3000 * so the following check in particular implies that
3001 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003002 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003005 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003007 }
3008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003010 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003011 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003012 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003013 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003014 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01003015 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003016 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003017
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003018 data += transform->ivlen;
3019 rec->data_offset += transform->ivlen;
3020 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003021 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003023
Hanno Beckerd96a6522019-07-10 13:55:25 +01003024 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3025
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003026 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3027 transform->iv_dec, transform->ivlen,
3028 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003031 return( ret );
3032 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003033
Hanno Beckerd96a6522019-07-10 13:55:25 +01003034 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003035 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003039 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003042 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003043 {
3044 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01003045 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3046 * records is equivalent to CBC decryption of the concatenation
3047 * of the records; in other words, IVs are maintained across
3048 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003049 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003050 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3051 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003054
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003055 /* Safe since data_len >= minlen + maclen + 1, so after having
3056 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003057 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3058 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003059 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003060
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003061 if( auth_done == 1 )
3062 {
3063 correct *= ( rec->data_len >= padlen + 1 );
3064 padlen *= ( rec->data_len >= padlen + 1 );
3065 }
3066 else
Paul Bakker45829992013-01-03 14:52:21 +01003067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003069 if( rec->data_len < transform->maclen + padlen + 1 )
3070 {
3071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3072 rec->data_len,
3073 transform->maclen,
3074 padlen + 1 ) );
3075 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003076#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003077
3078 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3079 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003080 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003081
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003082 padlen++;
3083
3084 /* Regardless of the validity of the padding,
3085 * we have data_len >= padlen here. */
3086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003088 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003090 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092#if defined(MBEDTLS_SSL_DEBUG_ALL)
3093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003094 "should be no more than %d",
3095 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003096#endif
Paul Bakker45829992013-01-03 14:52:21 +01003097 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 }
3099 }
3100 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3102#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3103 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003104 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003106 /* The padding check involves a series of up to 256
3107 * consecutive memory reads at the end of the record
3108 * plaintext buffer. In order to hide the length and
3109 * validity of the padding, always perform exactly
3110 * `min(256,plaintext_len)` reads (but take into account
3111 * only the last `padlen` bytes for the padding check). */
3112 size_t pad_count = 0;
3113 size_t real_count = 0;
3114 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003115
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003116 /* Index of first padding byte; it has been ensured above
3117 * that the subtraction is safe. */
3118 size_t const padding_idx = rec->data_len - padlen;
3119 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3120 size_t const start_idx = rec->data_len - num_checks;
3121 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003122
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003123 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003124 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003125 real_count |= ( idx >= padding_idx );
3126 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003127 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003128 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003131 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003133#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003134 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003135 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003136 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3138 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3141 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003142 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003143
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003144 /* If the padding was found to be invalid, padlen == 0
3145 * and the subtraction is safe. If the padding was found valid,
3146 * padlen hasn't been changed and the previous assertion
3147 * data_len >= padlen still holds. */
3148 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003149 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003150 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003152 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003157
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003158#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003160 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003161#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003162
3163 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003164 * Authenticate if not done yet.
3165 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003166 */
Hanno Becker52344c22018-01-03 15:24:20 +00003167#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003168 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003169 {
Hanno Becker992b6872017-11-09 18:57:39 +00003170 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003171
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003172 /* If the initial value of padlen was such that
3173 * data_len < maclen + padlen + 1, then padlen
3174 * got reset to 1, and the initial check
3175 * data_len >= minlen + maclen + 1
3176 * guarantees that at this point we still
3177 * have at least data_len >= maclen.
3178 *
3179 * If the initial value of padlen was such that
3180 * data_len >= maclen + padlen + 1, then we have
3181 * subtracted either padlen + 1 (if the padding was correct)
3182 * or 0 (if the padding was incorrect) since then,
3183 * hence data_len >= maclen in any case.
3184 */
3185 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003186 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003189 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003190 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003191 ssl_mac( &transform->md_ctx_dec,
3192 transform->mac_dec,
3193 data, rec->data_len,
3194 rec->ctr, rec->type,
3195 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003196 }
3197 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3199#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3200 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003201 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003202 {
3203 /*
3204 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003205 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003206 *
3207 * Known timing attacks:
3208 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3209 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003210 * To compensate for different timings for the MAC calculation
3211 * depending on how much padding was removed (which is determined
3212 * by padlen), process extra_run more blocks through the hash
3213 * function.
3214 *
3215 * The formula in the paper is
3216 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3217 * where L1 is the size of the header plus the decrypted message
3218 * plus CBC padding and L2 is the size of the header plus the
3219 * decrypted message. This is for an underlying hash function
3220 * with 64-byte blocks.
3221 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3222 * correctly. We round down instead of up, so -56 is the correct
3223 * value for our calculations instead of -55.
3224 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003225 * Repeat the formula rather than defining a block_size variable.
3226 * This avoids requiring division by a variable at runtime
3227 * (which would be marginally less efficient and would require
3228 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003229 */
3230 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003231 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003232
3233 /*
3234 * The next two sizes are the minimum and maximum values of
3235 * in_msglen over all padlen values.
3236 *
3237 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003238 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003239 *
3240 * Note that max_len + maclen is never more than the buffer
3241 * length, as we previously did in_msglen -= maclen too.
3242 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003243 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003244 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3245
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003246 memset( tmp, 0, sizeof( tmp ) );
3247
3248 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003249 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003250#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3251 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003252 case MBEDTLS_MD_MD5:
3253 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003254 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003255 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003256 extra_run =
3257 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3258 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003259 break;
3260#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003261#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003262 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003263 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003264 extra_run =
3265 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3266 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003267 break;
3268#endif
3269 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003271 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3272 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003273
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003274 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003275
Hanno Beckercab87e62019-04-29 13:52:53 +01003276 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3277 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003278 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3279 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003280 /* Make sure we access everything even when padlen > 0. This
3281 * makes the synchronisation requirements for just-in-time
3282 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003283 ssl_read_memory( data + rec->data_len, padlen );
3284 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003285
3286 /* Call mbedtls_md_process at least once due to cache attacks
3287 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003288 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003289 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003290
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003291 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003292
3293 /* Make sure we access all the memory that could contain the MAC,
3294 * before we check it in the next code block. This makes the
3295 * synchronisation requirements for just-in-time Prime+Probe
3296 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003297 ssl_read_memory( data + min_len,
3298 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003299 }
3300 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3302 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3305 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003306 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003307
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003308#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003309 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3310 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003311#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003313 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3314 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316#if defined(MBEDTLS_SSL_DEBUG_ALL)
3317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003318#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003319 correct = 0;
3320 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003321 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003322 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003323
3324 /*
3325 * Finally check the correct flag
3326 */
3327 if( correct == 0 )
3328 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003329#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003330
3331 /* Make extra sure authentication was performed, exactly once */
3332 if( auth_done != 1 )
3333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3335 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003336 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003337
Hanno Beckera0e20d02019-05-15 14:03:01 +01003338#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003339 if( rec->cid_len != 0 )
3340 {
3341 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3342 &rec->type );
3343 if( ret != 0 )
3344 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3345 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003346#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003349
3350 return( 0 );
3351}
3352
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003353#undef MAC_NONE
3354#undef MAC_PLAINTEXT
3355#undef MAC_CIPHERTEXT
3356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003358/*
3359 * Compression/decompression functions
3360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003362{
3363 int ret;
3364 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003365 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003366 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003367 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003370
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003371 if( len_pre == 0 )
3372 return( 0 );
3373
Paul Bakker2770fbd2012-07-03 13:30:23 +00003374 memcpy( msg_pre, ssl->out_msg, len_pre );
3375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003377 ssl->out_msglen ) );
3378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003380 ssl->out_msg, ssl->out_msglen );
3381
Paul Bakker48916f92012-09-16 19:57:18 +00003382 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3383 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3384 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003385 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003386
Paul Bakker48916f92012-09-16 19:57:18 +00003387 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003388 if( ret != Z_OK )
3389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3391 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003392 }
3393
Angus Grattond8213d02016-05-25 20:56:48 +10003394 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003395 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003398 ssl->out_msglen ) );
3399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003401 ssl->out_msg, ssl->out_msglen );
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003404
3405 return( 0 );
3406}
3407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003409{
3410 int ret;
3411 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003412 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003413 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003414 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003417
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003418 if( len_pre == 0 )
3419 return( 0 );
3420
Paul Bakker2770fbd2012-07-03 13:30:23 +00003421 memcpy( msg_pre, ssl->in_msg, len_pre );
3422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003424 ssl->in_msglen ) );
3425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003427 ssl->in_msg, ssl->in_msglen );
3428
Paul Bakker48916f92012-09-16 19:57:18 +00003429 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3430 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3431 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003432 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003433 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003434
Paul Bakker48916f92012-09-16 19:57:18 +00003435 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003436 if( ret != Z_OK )
3437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3439 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003440 }
3441
Angus Grattond8213d02016-05-25 20:56:48 +10003442 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003443 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003446 ssl->in_msglen ) );
3447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003449 ssl->in_msg, ssl->in_msglen );
3450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003452
3453 return( 0 );
3454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3458static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460#if defined(MBEDTLS_SSL_PROTO_DTLS)
3461static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003462{
3463 /* If renegotiation is not enforced, retransmit until we would reach max
3464 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003465 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003466 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003467 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003468 unsigned char doublings = 1;
3469
3470 while( ratio != 0 )
3471 {
3472 ++doublings;
3473 ratio >>= 1;
3474 }
3475
3476 if( ++ssl->renego_records_seen > doublings )
3477 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003479 return( 0 );
3480 }
3481 }
3482
3483 return( ssl_write_hello_request( ssl ) );
3484}
3485#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003487
Paul Bakker5121ce52009-01-03 21:22:43 +00003488/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003489 * Fill the input message buffer by appending data to it.
3490 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003491 *
3492 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3493 * available (from this read and/or a previous one). Otherwise, an error code
3494 * is returned (possibly EOF or WANT_READ).
3495 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003496 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3497 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3498 * since we always read a whole datagram at once.
3499 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003500 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003501 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003504{
Paul Bakker23986e52011-04-24 08:57:21 +00003505 int ret;
3506 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003510 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003513 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003515 }
3516
Angus Grattond8213d02016-05-25 20:56:48 +10003517 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003521 }
3522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003524 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003525 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003526 uint32_t timeout;
3527
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003528 /* Just to be sure */
3529 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3530 {
3531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3532 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3534 }
3535
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003536 /*
3537 * The point is, we need to always read a full datagram at once, so we
3538 * sometimes read more then requested, and handle the additional data.
3539 * It could be the rest of the current record (while fetching the
3540 * header) and/or some other records in the same datagram.
3541 */
3542
3543 /*
3544 * Move to the next record in the already read datagram if applicable
3545 */
3546 if( ssl->next_record_offset != 0 )
3547 {
3548 if( ssl->in_left < ssl->next_record_offset )
3549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003552 }
3553
3554 ssl->in_left -= ssl->next_record_offset;
3555
3556 if( ssl->in_left != 0 )
3557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003559 ssl->next_record_offset ) );
3560 memmove( ssl->in_hdr,
3561 ssl->in_hdr + ssl->next_record_offset,
3562 ssl->in_left );
3563 }
3564
3565 ssl->next_record_offset = 0;
3566 }
3567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003570
3571 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003572 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003573 */
3574 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003577 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003578 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003579
3580 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003581 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003582 * are not at the beginning of a new record, the caller did something
3583 * wrong.
3584 */
3585 if( ssl->in_left != 0 )
3586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003589 }
3590
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003591 /*
3592 * Don't even try to read if time's out already.
3593 * This avoids by-passing the timer when repeatedly receiving messages
3594 * that will end up being dropped.
3595 */
3596 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003597 {
3598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003599 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003600 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003601 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003602 {
Angus Grattond8213d02016-05-25 20:56:48 +10003603 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003606 timeout = ssl->handshake->retransmit_timeout;
3607 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003608 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003611
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003612 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003613 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3614 timeout );
3615 else
3616 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003619
3620 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003622 }
3623
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003624 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003627 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003630 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003631 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003634 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003635 }
3636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003640 return( ret );
3641 }
3642
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003643 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003644 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003646 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003648 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003649 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003652 return( ret );
3653 }
3654
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003655 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003656 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003658 }
3659
Paul Bakker5121ce52009-01-03 21:22:43 +00003660 if( ret < 0 )
3661 return( ret );
3662
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003663 ssl->in_left = ret;
3664 }
3665 else
3666#endif
3667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003669 ssl->in_left, nb_want ) );
3670
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003671 while( ssl->in_left < nb_want )
3672 {
3673 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003674
3675 if( ssl_check_timer( ssl ) != 0 )
3676 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3677 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003678 {
3679 if( ssl->f_recv_timeout != NULL )
3680 {
3681 ret = ssl->f_recv_timeout( ssl->p_bio,
3682 ssl->in_hdr + ssl->in_left, len,
3683 ssl->conf->read_timeout );
3684 }
3685 else
3686 {
3687 ret = ssl->f_recv( ssl->p_bio,
3688 ssl->in_hdr + ssl->in_left, len );
3689 }
3690 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003693 ssl->in_left, nb_want ) );
3694 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003695
3696 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003698
3699 if( ret < 0 )
3700 return( ret );
3701
mohammad160352aecb92018-03-28 23:41:40 -07003702 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003703 {
Darryl Green11999bb2018-03-13 15:22:58 +00003704 MBEDTLS_SSL_DEBUG_MSG( 1,
3705 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003706 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3708 }
3709
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003710 ssl->in_left += ret;
3711 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003712 }
3713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
3716 return( 0 );
3717}
3718
3719/*
3720 * Flush any data not yet written
3721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003723{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003724 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003725 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003728
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003729 if( ssl->f_send == NULL )
3730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003732 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003734 }
3735
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003736 /* Avoid incrementing counter if data is flushed */
3737 if( ssl->out_left == 0 )
3738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003740 return( 0 );
3741 }
3742
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 while( ssl->out_left > 0 )
3744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003746 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003747
Hanno Becker2b1e3542018-08-06 11:19:13 +01003748 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003749 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003752
3753 if( ret <= 0 )
3754 return( ret );
3755
mohammad160352aecb92018-03-28 23:41:40 -07003756 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003757 {
Darryl Green11999bb2018-03-13 15:22:58 +00003758 MBEDTLS_SSL_DEBUG_MSG( 1,
3759 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003760 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3762 }
3763
Paul Bakker5121ce52009-01-03 21:22:43 +00003764 ssl->out_left -= ret;
3765 }
3766
Hanno Becker2b1e3542018-08-06 11:19:13 +01003767#if defined(MBEDTLS_SSL_PROTO_DTLS)
3768 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003769 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003770 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003771 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003772 else
3773#endif
3774 {
3775 ssl->out_hdr = ssl->out_buf + 8;
3776 }
3777 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003780
3781 return( 0 );
3782}
3783
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003784/*
3785 * Functions to handle the DTLS retransmission state machine
3786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003788/*
3789 * Append current handshake message to current outgoing flight
3790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003792{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3795 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3796 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003797
3798 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003799 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003800 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003803 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003804 }
3805
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003806 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003807 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003809 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003810 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003811 }
3812
3813 /* Copy current handshake message with headers */
3814 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3815 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003816 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003817 msg->next = NULL;
3818
3819 /* Append to the current flight */
3820 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003821 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003822 else
3823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003825 while( cur->next != NULL )
3826 cur = cur->next;
3827 cur->next = msg;
3828 }
3829
Hanno Becker3b235902018-08-06 09:54:53 +01003830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003831 return( 0 );
3832}
3833
3834/*
3835 * Free the current flight of handshake messages
3836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 mbedtls_ssl_flight_item *cur = flight;
3840 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003841
3842 while( cur != NULL )
3843 {
3844 next = cur->next;
3845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 mbedtls_free( cur->p );
3847 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003848
3849 cur = next;
3850 }
3851}
3852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3854static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003855#endif
3856
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003857/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003858 * Swap transform_out and out_ctr with the alternative ones
3859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003861{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003863 unsigned char tmp_out_ctr[8];
3864
3865 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003868 return;
3869 }
3870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003872
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003873 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003874 tmp_transform = ssl->transform_out;
3875 ssl->transform_out = ssl->handshake->alt_transform_out;
3876 ssl->handshake->alt_transform_out = tmp_transform;
3877
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003878 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003879 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3880 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003881 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003882
3883 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003884 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3887 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3892 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003893 }
3894 }
3895#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003896}
3897
3898/*
3899 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003900 */
3901int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3902{
3903 int ret = 0;
3904
3905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3906
3907 ret = mbedtls_ssl_flight_transmit( ssl );
3908
3909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3910
3911 return( ret );
3912}
3913
3914/*
3915 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003916 *
3917 * Need to remember the current message in case flush_output returns
3918 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003919 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003920 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003921int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003922{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003923 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003927 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003929
3930 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003931 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003932 ssl_swap_epochs( ssl );
3933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003935 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003936
3937 while( ssl->handshake->cur_msg != NULL )
3938 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003939 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003940 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003941
Hanno Beckere1dcb032018-08-17 16:47:58 +01003942 int const is_finished =
3943 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3944 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3945
Hanno Becker04da1892018-08-14 13:22:10 +01003946 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3947 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3948
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003949 /* Swap epochs before sending Finished: we can't do it after
3950 * sending ChangeCipherSpec, in case write returns WANT_READ.
3951 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003952 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003953 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003955 ssl_swap_epochs( ssl );
3956 }
3957
Hanno Becker67bc7c32018-08-06 11:33:50 +01003958 ret = ssl_get_remaining_payload_in_datagram( ssl );
3959 if( ret < 0 )
3960 return( ret );
3961 max_frag_len = (size_t) ret;
3962
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003963 /* CCS is copied as is, while HS messages may need fragmentation */
3964 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3965 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003966 if( max_frag_len == 0 )
3967 {
3968 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3969 return( ret );
3970
3971 continue;
3972 }
3973
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003974 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003975 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003976 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003977
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003978 /* Update position inside current message */
3979 ssl->handshake->cur_msg_p += cur->len;
3980 }
3981 else
3982 {
3983 const unsigned char * const p = ssl->handshake->cur_msg_p;
3984 const size_t hs_len = cur->len - 12;
3985 const size_t frag_off = p - ( cur->p + 12 );
3986 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003987 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003988
Hanno Beckere1dcb032018-08-17 16:47:58 +01003989 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003990 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003991 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003992 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003993
Hanno Becker67bc7c32018-08-06 11:33:50 +01003994 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3995 return( ret );
3996
3997 continue;
3998 }
3999 max_hs_frag_len = max_frag_len - 12;
4000
4001 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4002 max_hs_frag_len : rem_len;
4003
4004 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004005 {
4006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004007 (unsigned) cur_hs_frag_len,
4008 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004009 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004010
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004011 /* Messages are stored with handshake headers as if not fragmented,
4012 * copy beginning of headers then fill fragmentation fields.
4013 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4014 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004015
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004016 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4017 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4018 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4019
Hanno Becker67bc7c32018-08-06 11:33:50 +01004020 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4021 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4022 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004023
4024 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4025
Hanno Becker3f7b9732018-08-28 09:53:25 +01004026 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004027 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4028 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004029 ssl->out_msgtype = cur->type;
4030
4031 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004032 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004033 }
4034
4035 /* If done with the current message move to the next one if any */
4036 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4037 {
4038 if( cur->next != NULL )
4039 {
4040 ssl->handshake->cur_msg = cur->next;
4041 ssl->handshake->cur_msg_p = cur->next->p + 12;
4042 }
4043 else
4044 {
4045 ssl->handshake->cur_msg = NULL;
4046 ssl->handshake->cur_msg_p = NULL;
4047 }
4048 }
4049
4050 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004051 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004054 return( ret );
4055 }
4056 }
4057
Hanno Becker67bc7c32018-08-06 11:33:50 +01004058 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4059 return( ret );
4060
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004061 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4063 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004064 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004067 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4068 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004069
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004071
4072 return( 0 );
4073}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004074
4075/*
4076 * To be called when the last message of an incoming flight is received.
4077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004079{
4080 /* We won't need to resend that one any more */
4081 ssl_flight_free( ssl->handshake->flight );
4082 ssl->handshake->flight = NULL;
4083 ssl->handshake->cur_msg = NULL;
4084
4085 /* The next incoming flight will start with this msg_seq */
4086 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4087
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004088 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004089 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004090
Hanno Becker0271f962018-08-16 13:23:47 +01004091 /* Clear future message buffering structure. */
4092 ssl_buffering_free( ssl );
4093
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004094 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004095 ssl_set_timer( ssl, 0 );
4096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4098 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004101 }
4102 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004104}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004105
4106/*
4107 * To be called when the last message of an outgoing flight is send.
4108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004110{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004111 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004112 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4115 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004118 }
4119 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004121}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004123
Paul Bakker5121ce52009-01-03 21:22:43 +00004124/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004125 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004126 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004127
4128/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004129 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004130 *
4131 * - fill in handshake headers
4132 * - update handshake checksum
4133 * - DTLS: save message for resending
4134 * - then pass to the record layer
4135 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004136 * DTLS: except for HelloRequest, messages are only queued, and will only be
4137 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004138 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004139 * Inputs:
4140 * - ssl->out_msglen: 4 + actual handshake message len
4141 * (4 is the size of handshake headers for TLS)
4142 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4143 * - ssl->out_msg + 4: the handshake message body
4144 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004145 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004146 * - ssl->out_msglen: the length of the record contents
4147 * (including handshake headers but excluding record headers)
4148 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004149 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004150int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004151{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004152 int ret;
4153 const size_t hs_len = ssl->out_msglen - 4;
4154 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004155
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4157
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004158 /*
4159 * Sanity checks
4160 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004161 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004162 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4163 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004164 /* In SSLv3, the client might send a NoCertificate alert. */
4165#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4166 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4167 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4168 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4169#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4170 {
4171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4173 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004174 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004175
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004176 /* Whenever we send anything different from a
4177 * HelloRequest we should be in a handshake - double check. */
4178 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4179 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004180 ssl->handshake == NULL )
4181 {
4182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4184 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004187 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004188 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004189 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004190 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4192 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004193 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004194#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004195
Hanno Beckerb50a2532018-08-06 11:52:54 +01004196 /* Double-check that we did not exceed the bounds
4197 * of the outgoing record buffer.
4198 * This should never fail as the various message
4199 * writing functions must obey the bounds of the
4200 * outgoing record buffer, but better be safe.
4201 *
4202 * Note: We deliberately do not check for the MTU or MFL here.
4203 */
4204 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4205 {
4206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4207 "size %u, maximum %u",
4208 (unsigned) ssl->out_msglen,
4209 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4210 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4211 }
4212
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004213 /*
4214 * Fill handshake headers
4215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004217 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004218 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4219 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4220 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004221
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004222 /*
4223 * DTLS has additional fields in the Handshake layer,
4224 * between the length field and the actual payload:
4225 * uint16 message_seq;
4226 * uint24 fragment_offset;
4227 * uint24 fragment_length;
4228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004230 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004231 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004232 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004233 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004234 {
4235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4236 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004237 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004238 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4240 }
4241
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004242 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004243 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004244
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004245 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004246 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004247 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004248 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4249 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4250 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004251 }
4252 else
4253 {
4254 ssl->out_msg[4] = 0;
4255 ssl->out_msg[5] = 0;
4256 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004257
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004258 /* Handshake hashes are computed without fragmentation,
4259 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004260 memset( ssl->out_msg + 6, 0x00, 3 );
4261 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004262 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004264
Hanno Becker0207e532018-08-28 10:28:28 +01004265 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004266 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4267 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004268 }
4269
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004270 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004272 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004273 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4274 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004275 {
4276 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004279 return( ret );
4280 }
4281 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004282 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004283#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004284 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004285 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004286 {
4287 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4288 return( ret );
4289 }
4290 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004291
4292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4293
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004294 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004295}
4296
4297/*
4298 * Record layer functions
4299 */
4300
4301/*
4302 * Write current record.
4303 *
4304 * Uses:
4305 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4306 * - ssl->out_msglen: length of the record content (excl headers)
4307 * - ssl->out_msg: record content
4308 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004309int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004310{
4311 int ret, done = 0;
4312 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004313 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004314
4315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004318 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004320 {
4321 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004324 return( ret );
4325 }
4326
4327 len = ssl->out_msglen;
4328 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4332 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336 ret = mbedtls_ssl_hw_record_write( ssl );
4337 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4340 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004341 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004342
4343 if( ret == 0 )
4344 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004345 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004347 if( !done )
4348 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004349 unsigned i;
4350 size_t protected_record_size;
4351
Hanno Becker6430faf2019-05-08 11:57:13 +01004352 /* Skip writing the record content type to after the encryption,
4353 * as it may change when using the CID extension. */
4354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004356 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004357
Hanno Becker19859472018-08-06 09:40:20 +01004358 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004359 ssl->out_len[0] = (unsigned char)( len >> 8 );
4360 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004361
Paul Bakker48916f92012-09-16 19:57:18 +00004362 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004363 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004364 mbedtls_record rec;
4365
4366 rec.buf = ssl->out_iv;
4367 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4368 ( ssl->out_iv - ssl->out_buf );
4369 rec.data_len = ssl->out_msglen;
4370 rec.data_offset = ssl->out_msg - rec.buf;
4371
4372 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4373 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4374 ssl->conf->transport, rec.ver );
4375 rec.type = ssl->out_msgtype;
4376
Hanno Beckera0e20d02019-05-15 14:03:01 +01004377#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004378 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004379 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004380#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004381
Hanno Beckera18d1322018-01-03 14:27:32 +00004382 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004383 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004386 return( ret );
4387 }
4388
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004389 if( rec.data_offset != 0 )
4390 {
4391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4393 }
4394
Hanno Becker6430faf2019-05-08 11:57:13 +01004395 /* Update the record content type and CID. */
4396 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004397#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004398 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004399#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004400 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004401 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4402 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004403 }
4404
Hanno Becker5903de42019-05-03 14:46:38 +01004405 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004406
4407#if defined(MBEDTLS_SSL_PROTO_DTLS)
4408 /* In case of DTLS, double-check that we don't exceed
4409 * the remaining space in the datagram. */
4410 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4411 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004412 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004413 if( ret < 0 )
4414 return( ret );
4415
4416 if( protected_record_size > (size_t) ret )
4417 {
4418 /* Should never happen */
4419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4420 }
4421 }
4422#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004423
Hanno Becker6430faf2019-05-08 11:57:13 +01004424 /* Now write the potentially updated record content type. */
4425 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004427 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004428 "version = [%d:%d], msglen = %d",
4429 ssl->out_hdr[0], ssl->out_hdr[1],
4430 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004433 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004434
4435 ssl->out_left += protected_record_size;
4436 ssl->out_hdr += protected_record_size;
4437 ssl_update_out_pointers( ssl, ssl->transform_out );
4438
Hanno Becker04484622018-08-06 09:49:38 +01004439 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4440 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4441 break;
4442
4443 /* The loop goes to its end iff the counter is wrapping */
4444 if( i == ssl_ep_len( ssl ) )
4445 {
4446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4447 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4448 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004449 }
4450
Hanno Becker67bc7c32018-08-06 11:33:50 +01004451#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004452 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4453 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004454 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004455 size_t remaining;
4456 ret = ssl_get_remaining_payload_in_datagram( ssl );
4457 if( ret < 0 )
4458 {
4459 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4460 ret );
4461 return( ret );
4462 }
4463
4464 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004465 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004466 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004467 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004468 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004469 else
4470 {
Hanno Becker513815a2018-08-20 11:56:09 +01004471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004472 }
4473 }
4474#endif /* MBEDTLS_SSL_PROTO_DTLS */
4475
4476 if( ( flush == SSL_FORCE_FLUSH ) &&
4477 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004480 return( ret );
4481 }
4482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004484
4485 return( 0 );
4486}
4487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004489
4490static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4491{
4492 if( ssl->in_msglen < ssl->in_hslen ||
4493 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4494 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4495 {
4496 return( 1 );
4497 }
4498 return( 0 );
4499}
Hanno Becker44650b72018-08-16 12:51:11 +01004500
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004501static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004502{
4503 return( ( ssl->in_msg[9] << 16 ) |
4504 ( ssl->in_msg[10] << 8 ) |
4505 ssl->in_msg[11] );
4506}
4507
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004508static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004509{
4510 return( ( ssl->in_msg[6] << 16 ) |
4511 ( ssl->in_msg[7] << 8 ) |
4512 ssl->in_msg[8] );
4513}
4514
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004515static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004516{
4517 uint32_t msg_len, frag_off, frag_len;
4518
4519 msg_len = ssl_get_hs_total_len( ssl );
4520 frag_off = ssl_get_hs_frag_off( ssl );
4521 frag_len = ssl_get_hs_frag_len( ssl );
4522
4523 if( frag_off > msg_len )
4524 return( -1 );
4525
4526 if( frag_len > msg_len - frag_off )
4527 return( -1 );
4528
4529 if( frag_len + 12 > ssl->in_msglen )
4530 return( -1 );
4531
4532 return( 0 );
4533}
4534
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004535/*
4536 * Mark bits in bitmask (used for DTLS HS reassembly)
4537 */
4538static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4539{
4540 unsigned int start_bits, end_bits;
4541
4542 start_bits = 8 - ( offset % 8 );
4543 if( start_bits != 8 )
4544 {
4545 size_t first_byte_idx = offset / 8;
4546
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004547 /* Special case */
4548 if( len <= start_bits )
4549 {
4550 for( ; len != 0; len-- )
4551 mask[first_byte_idx] |= 1 << ( start_bits - len );
4552
4553 /* Avoid potential issues with offset or len becoming invalid */
4554 return;
4555 }
4556
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004557 offset += start_bits; /* Now offset % 8 == 0 */
4558 len -= start_bits;
4559
4560 for( ; start_bits != 0; start_bits-- )
4561 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4562 }
4563
4564 end_bits = len % 8;
4565 if( end_bits != 0 )
4566 {
4567 size_t last_byte_idx = ( offset + len ) / 8;
4568
4569 len -= end_bits; /* Now len % 8 == 0 */
4570
4571 for( ; end_bits != 0; end_bits-- )
4572 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4573 }
4574
4575 memset( mask + offset / 8, 0xFF, len / 8 );
4576}
4577
4578/*
4579 * Check that bitmask is full
4580 */
4581static int ssl_bitmask_check( unsigned char *mask, size_t len )
4582{
4583 size_t i;
4584
4585 for( i = 0; i < len / 8; i++ )
4586 if( mask[i] != 0xFF )
4587 return( -1 );
4588
4589 for( i = 0; i < len % 8; i++ )
4590 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4591 return( -1 );
4592
4593 return( 0 );
4594}
4595
Hanno Becker56e205e2018-08-16 09:06:12 +01004596/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004597static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004598 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004599{
Hanno Becker56e205e2018-08-16 09:06:12 +01004600 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004601
Hanno Becker56e205e2018-08-16 09:06:12 +01004602 alloc_len = 12; /* Handshake header */
4603 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004604
Hanno Beckerd07df862018-08-16 09:14:58 +01004605 if( add_bitmap )
4606 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004607
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004608 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004609}
Hanno Becker56e205e2018-08-16 09:06:12 +01004610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004611#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004612
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004613static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004614{
4615 return( ( ssl->in_msg[1] << 16 ) |
4616 ( ssl->in_msg[2] << 8 ) |
4617 ssl->in_msg[3] );
4618}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004619
Simon Butcher99000142016-10-13 17:21:01 +01004620int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004621{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004622 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004625 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004627 }
4628
Hanno Becker12555c62018-08-16 12:47:53 +01004629 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004632 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004633 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004636 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004637 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004638 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004639 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004640
Hanno Becker44650b72018-08-16 12:51:11 +01004641 if( ssl_check_hs_header( ssl ) != 0 )
4642 {
4643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4644 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4645 }
4646
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004647 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004648 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4649 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4650 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4651 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004652 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004653 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4654 {
4655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4656 recv_msg_seq,
4657 ssl->handshake->in_msg_seq ) );
4658 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4659 }
4660
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004661 /* Retransmit only on last message from previous flight, to avoid
4662 * too many retransmissions.
4663 * Besides, No sane server ever retransmits HelloVerifyRequest */
4664 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004668 "message_seq = %d, start_of_flight = %d",
4669 recv_msg_seq,
4670 ssl->handshake->in_flight_start_seq ) );
4671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004675 return( ret );
4676 }
4677 }
4678 else
4679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004681 "message_seq = %d, expected = %d",
4682 recv_msg_seq,
4683 ssl->handshake->in_msg_seq ) );
4684 }
4685
Hanno Becker90333da2017-10-10 11:27:13 +01004686 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004687 }
4688 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004689
Hanno Becker6d97ef52018-08-16 13:09:04 +01004690 /* Message reassembly is handled alongside buffering of future
4691 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004692 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004693 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004694 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004697 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004698 }
4699 }
4700 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004702 /* With TLS we don't handle fragmentation (for now) */
4703 if( ssl->in_msglen < ssl->in_hslen )
4704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4706 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004707 }
4708
Simon Butcher99000142016-10-13 17:21:01 +01004709 return( 0 );
4710}
4711
4712void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4713{
Hanno Becker0271f962018-08-16 13:23:47 +01004714 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004715
Hanno Becker0271f962018-08-16 13:23:47 +01004716 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004717 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004718 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004719 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004720
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004721 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004722#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004723 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004724 ssl->handshake != NULL )
4725 {
Hanno Becker0271f962018-08-16 13:23:47 +01004726 unsigned offset;
4727 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004728
Hanno Becker0271f962018-08-16 13:23:47 +01004729 /* Increment handshake sequence number */
4730 hs->in_msg_seq++;
4731
4732 /*
4733 * Clear up handshake buffering and reassembly structure.
4734 */
4735
4736 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004737 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004738
4739 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004740 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4741 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004742 offset++, hs_buf++ )
4743 {
4744 *hs_buf = *(hs_buf + 1);
4745 }
4746
4747 /* Create a fresh last entry */
4748 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004749 }
4750#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004751}
4752
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004753/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004754 * DTLS anti-replay: RFC 6347 4.1.2.6
4755 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004756 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4757 * Bit n is set iff record number in_window_top - n has been seen.
4758 *
4759 * Usually, in_window_top is the last record number seen and the lsb of
4760 * in_window is set. The only exception is the initial state (record number 0
4761 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4764static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004765{
4766 ssl->in_window_top = 0;
4767 ssl->in_window = 0;
4768}
4769
4770static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4771{
4772 return( ( (uint64_t) buf[0] << 40 ) |
4773 ( (uint64_t) buf[1] << 32 ) |
4774 ( (uint64_t) buf[2] << 24 ) |
4775 ( (uint64_t) buf[3] << 16 ) |
4776 ( (uint64_t) buf[4] << 8 ) |
4777 ( (uint64_t) buf[5] ) );
4778}
4779
4780/*
4781 * Return 0 if sequence number is acceptable, -1 otherwise
4782 */
Hanno Becker0183d692019-07-12 08:50:37 +01004783int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004784{
4785 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4786 uint64_t bit;
4787
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004788 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004789 return( 0 );
4790
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004791 if( rec_seqnum > ssl->in_window_top )
4792 return( 0 );
4793
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004794 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004795
4796 if( bit >= 64 )
4797 return( -1 );
4798
4799 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4800 return( -1 );
4801
4802 return( 0 );
4803}
4804
4805/*
4806 * Update replay window on new validated record
4807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *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
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004812 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004813 return;
4814
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004815 if( rec_seqnum > ssl->in_window_top )
4816 {
4817 /* Update window_top and the contents of the window */
4818 uint64_t shift = rec_seqnum - ssl->in_window_top;
4819
4820 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004821 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004822 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004823 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004824 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004825 ssl->in_window |= 1;
4826 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004827
4828 ssl->in_window_top = rec_seqnum;
4829 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004830 else
4831 {
4832 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004833 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004834
4835 if( bit < 64 ) /* Always true, but be extra sure */
4836 ssl->in_window |= (uint64_t) 1 << bit;
4837 }
4838}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004840
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004841#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004842/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004843static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4844
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004845/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004846 * Without any SSL context, check if a datagram looks like a ClientHello with
4847 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004848 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004849 *
4850 * - if cookie is valid, return 0
4851 * - if ClientHello looks superficially valid but cookie is not,
4852 * fill obuf and set olen, then
4853 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4854 * - otherwise return a specific error code
4855 */
4856static int ssl_check_dtls_clihlo_cookie(
4857 mbedtls_ssl_cookie_write_t *f_cookie_write,
4858 mbedtls_ssl_cookie_check_t *f_cookie_check,
4859 void *p_cookie,
4860 const unsigned char *cli_id, size_t cli_id_len,
4861 const unsigned char *in, size_t in_len,
4862 unsigned char *obuf, size_t buf_len, size_t *olen )
4863{
4864 size_t sid_len, cookie_len;
4865 unsigned char *p;
4866
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004867 /*
4868 * Structure of ClientHello with record and handshake headers,
4869 * and expected values. We don't need to check a lot, more checks will be
4870 * done when actually parsing the ClientHello - skipping those checks
4871 * avoids code duplication and does not make cookie forging any easier.
4872 *
4873 * 0-0 ContentType type; copied, must be handshake
4874 * 1-2 ProtocolVersion version; copied
4875 * 3-4 uint16 epoch; copied, must be 0
4876 * 5-10 uint48 sequence_number; copied
4877 * 11-12 uint16 length; (ignored)
4878 *
4879 * 13-13 HandshakeType msg_type; (ignored)
4880 * 14-16 uint24 length; (ignored)
4881 * 17-18 uint16 message_seq; copied
4882 * 19-21 uint24 fragment_offset; copied, must be 0
4883 * 22-24 uint24 fragment_length; (ignored)
4884 *
4885 * 25-26 ProtocolVersion client_version; (ignored)
4886 * 27-58 Random random; (ignored)
4887 * 59-xx SessionID session_id; 1 byte len + sid_len content
4888 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4889 * ...
4890 *
4891 * Minimum length is 61 bytes.
4892 */
4893 if( in_len < 61 ||
4894 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4895 in[3] != 0 || in[4] != 0 ||
4896 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4897 {
4898 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4899 }
4900
4901 sid_len = in[59];
4902 if( sid_len > in_len - 61 )
4903 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4904
4905 cookie_len = in[60 + sid_len];
4906 if( cookie_len > in_len - 60 )
4907 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4908
4909 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4910 cli_id, cli_id_len ) == 0 )
4911 {
4912 /* Valid cookie */
4913 return( 0 );
4914 }
4915
4916 /*
4917 * If we get here, we've got an invalid cookie, let's prepare HVR.
4918 *
4919 * 0-0 ContentType type; copied
4920 * 1-2 ProtocolVersion version; copied
4921 * 3-4 uint16 epoch; copied
4922 * 5-10 uint48 sequence_number; copied
4923 * 11-12 uint16 length; olen - 13
4924 *
4925 * 13-13 HandshakeType msg_type; hello_verify_request
4926 * 14-16 uint24 length; olen - 25
4927 * 17-18 uint16 message_seq; copied
4928 * 19-21 uint24 fragment_offset; copied
4929 * 22-24 uint24 fragment_length; olen - 25
4930 *
4931 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4932 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4933 *
4934 * Minimum length is 28.
4935 */
4936 if( buf_len < 28 )
4937 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4938
4939 /* Copy most fields and adapt others */
4940 memcpy( obuf, in, 25 );
4941 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4942 obuf[25] = 0xfe;
4943 obuf[26] = 0xff;
4944
4945 /* Generate and write actual cookie */
4946 p = obuf + 28;
4947 if( f_cookie_write( p_cookie,
4948 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4949 {
4950 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4951 }
4952
4953 *olen = p - obuf;
4954
4955 /* Go back and fill length fields */
4956 obuf[27] = (unsigned char)( *olen - 28 );
4957
4958 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4959 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4960 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4961
4962 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4963 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4964
4965 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4966}
4967
4968/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004969 * Handle possible client reconnect with the same UDP quadruplet
4970 * (RFC 6347 Section 4.2.8).
4971 *
4972 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4973 * that looks like a ClientHello.
4974 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004975 * - if the input looks like a ClientHello without cookies,
4976 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004977 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004978 * - if the input looks like a ClientHello with a valid cookie,
4979 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004980 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004981 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004982 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004983 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004984 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4985 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004986 */
4987static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4988{
4989 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004990 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004991
Hanno Becker2fddd372019-07-10 14:37:41 +01004992 if( ssl->conf->f_cookie_write == NULL ||
4993 ssl->conf->f_cookie_check == NULL )
4994 {
4995 /* If we can't use cookies to verify reachability of the peer,
4996 * drop the record. */
4997 return( 0 );
4998 }
4999
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005000 ret = ssl_check_dtls_clihlo_cookie(
5001 ssl->conf->f_cookie_write,
5002 ssl->conf->f_cookie_check,
5003 ssl->conf->p_cookie,
5004 ssl->cli_id, ssl->cli_id_len,
5005 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005006 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005007
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005008 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5009
5010 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005011 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005012 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005013 * If the error is permanent we'll catch it later,
5014 * if it's not, then hopefully it'll work next time. */
5015 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01005016 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005017 }
5018
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005019 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005020 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005021 /* Got a valid cookie, partially reset context */
5022 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5023 {
5024 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5025 return( ret );
5026 }
5027
5028 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005029 }
5030
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005031 return( ret );
5032}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005033#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005034
Hanno Beckerf661c9c2019-05-03 13:25:54 +01005035static int ssl_check_record_type( uint8_t record_type )
5036{
5037 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5038 record_type != MBEDTLS_SSL_MSG_ALERT &&
5039 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5040 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5041 {
5042 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5043 }
5044
5045 return( 0 );
5046}
5047
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005048/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005049 * ContentType type;
5050 * ProtocolVersion version;
5051 * uint16 epoch; // DTLS only
5052 * uint48 sequence_number; // DTLS only
5053 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005054 *
5055 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005056 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005057 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5058 *
5059 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005060 * 1. proceed with the record if this function returns 0
5061 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5062 * 3. return CLIENT_RECONNECT if this function return that value
5063 * 4. drop the whole datagram if this function returns anything else.
5064 * Point 2 is needed when the peer is resending, and we have already received
5065 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005066 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005067static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005068 unsigned char *buf,
5069 size_t len,
5070 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005071{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005072 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005073
Hanno Beckere5e7e782019-07-11 12:29:35 +01005074 size_t const rec_hdr_type_offset = 0;
5075 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005076
Hanno Beckere5e7e782019-07-11 12:29:35 +01005077 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5078 rec_hdr_type_len;
5079 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005080
Hanno Beckere5e7e782019-07-11 12:29:35 +01005081 size_t const rec_hdr_ctr_len = 8;
5082#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005083 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005084 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5085 rec_hdr_version_len;
5086
Hanno Beckera0e20d02019-05-15 14:03:01 +01005087#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005088 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5089 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005090 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005091#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5092#endif /* MBEDTLS_SSL_PROTO_DTLS */
5093
5094 size_t rec_hdr_len_offset; /* To be determined */
5095 size_t const rec_hdr_len_len = 2;
5096
5097 /*
5098 * Check minimum lengths for record header.
5099 */
5100
5101#if defined(MBEDTLS_SSL_PROTO_DTLS)
5102 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5103 {
5104 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5105 }
5106 else
5107#endif /* MBEDTLS_SSL_PROTO_DTLS */
5108 {
5109 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5110 }
5111
5112 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5113 {
5114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5115 (unsigned) len,
5116 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5117 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5118 }
5119
5120 /*
5121 * Parse and validate record content type
5122 */
5123
5124 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005125
5126 /* Check record content type */
5127#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5128 rec->cid_len = 0;
5129
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005130 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005131 ssl->conf->cid_len != 0 &&
5132 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005133 {
5134 /* Shift pointers to account for record header including CID
5135 * struct {
5136 * ContentType special_type = tls12_cid;
5137 * ProtocolVersion version;
5138 * uint16 epoch;
5139 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005140 * opaque cid[cid_length]; // Additional field compared to
5141 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005142 * uint16 length;
5143 * opaque enc_content[DTLSCiphertext.length];
5144 * } DTLSCiphertext;
5145 */
5146
5147 /* So far, we only support static CID lengths
5148 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005149 rec_hdr_cid_len = ssl->conf->cid_len;
5150 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005151
Hanno Beckere5e7e782019-07-11 12:29:35 +01005152 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005153 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5155 (unsigned) len,
5156 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005157 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005158 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005159
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005160 /* configured CID len is guaranteed at most 255, see
5161 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5162 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005163 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005164 }
5165 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005166#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005167 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005168 if( ssl_check_record_type( rec->type ) )
5169 {
Hanno Becker54229812019-07-12 14:40:00 +01005170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5171 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005172 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5173 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005174 }
5175
Hanno Beckere5e7e782019-07-11 12:29:35 +01005176 /*
5177 * Parse and validate record version
5178 */
5179
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005180 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5181 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005182 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5183 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005184 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005185
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005186 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5189 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005190 }
5191
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005192 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5195 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005196 }
5197
Hanno Beckere5e7e782019-07-11 12:29:35 +01005198 /*
5199 * Parse/Copy record sequence number.
5200 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005201
Hanno Beckere5e7e782019-07-11 12:29:35 +01005202#if defined(MBEDTLS_SSL_PROTO_DTLS)
5203 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005204 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005205 /* Copy explicit record sequence number from input buffer. */
5206 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5207 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005208 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005209 else
5210#endif /* MBEDTLS_SSL_PROTO_DTLS */
5211 {
5212 /* Copy implicit record sequence number from SSL context structure. */
5213 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5214 }
Paul Bakker40e46942009-01-03 21:51:57 +00005215
Hanno Beckere5e7e782019-07-11 12:29:35 +01005216 /*
5217 * Parse record length.
5218 */
5219
Hanno Beckere5e7e782019-07-11 12:29:35 +01005220 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005221 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5222 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005223 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005224
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005226 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005227 rec->type,
5228 major_ver, minor_ver, rec->data_len ) );
5229
5230 rec->buf = buf;
5231 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005232
Hanno Beckerd417cc92019-07-26 08:20:27 +01005233 if( rec->data_len == 0 )
5234 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005235
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005236 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005237 * DTLS-related tests.
5238 * Check epoch before checking length constraint because
5239 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5240 * message gets duplicated before the corresponding Finished message,
5241 * the second ChangeCipherSpec should be discarded because it belongs
5242 * to an old epoch, but not because its length is shorter than
5243 * the minimum record length for packets using the new record transform.
5244 * Note that these two kinds of failures are handled differently,
5245 * as an unexpected record is silently skipped but an invalid
5246 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005247 */
5248#if defined(MBEDTLS_SSL_PROTO_DTLS)
5249 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5250 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005251 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005252
Hanno Becker955a5c92019-07-10 17:12:07 +01005253 /* Check that the datagram is large enough to contain a record
5254 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005255 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005256 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5258 (unsigned) len,
5259 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005260 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5261 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005262
Hanno Becker37cfe732019-07-10 17:20:01 +01005263 /* Records from other, non-matching epochs are silently discarded.
5264 * (The case of same-port Client reconnects must be considered in
5265 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005266 if( rec_epoch != ssl->in_epoch )
5267 {
5268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5269 "expected %d, received %d",
5270 ssl->in_epoch, rec_epoch ) );
5271
Hanno Becker552f7472019-07-19 10:59:12 +01005272 /* Records from the next epoch are considered for buffering
5273 * (concretely: early Finished messages). */
5274 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005275 {
Hanno Becker552f7472019-07-19 10:59:12 +01005276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5277 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005278 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005279
Hanno Becker2fddd372019-07-10 14:37:41 +01005280 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005281 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005282#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005283 /* For records from the correct epoch, check whether their
5284 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005285 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005286 {
5287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5288 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5289 }
5290#endif
5291 }
5292#endif /* MBEDTLS_SSL_PROTO_DTLS */
5293
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005294 return( 0 );
5295}
Paul Bakker5121ce52009-01-03 21:22:43 +00005296
Paul Bakker5121ce52009-01-03 21:22:43 +00005297
Hanno Becker2fddd372019-07-10 14:37:41 +01005298#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5299static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5300{
5301 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5302
5303 /*
5304 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5305 * access the first byte of record content (handshake type), as we
5306 * have an active transform (possibly iv_len != 0), so use the
5307 * fact that the record header len is 13 instead.
5308 */
5309 if( rec_epoch == 0 &&
5310 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5311 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5312 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5313 ssl->in_left > 13 &&
5314 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5315 {
5316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5317 "from the same port" ) );
5318 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005319 }
5320
5321 return( 0 );
5322}
Hanno Becker2fddd372019-07-10 14:37:41 +01005323#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005324
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005325/*
5326 * If applicable, decrypt (and decompress) record content
5327 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005328static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5329 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005330{
5331 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005334 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5337 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 ret = mbedtls_ssl_hw_record_read( ssl );
5342 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5345 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005346 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005347
5348 if( ret == 0 )
5349 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005352 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005353 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005354 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005355
Hanno Beckera18d1322018-01-03 14:27:32 +00005356 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005357 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005360
Hanno Beckera0e20d02019-05-15 14:03:01 +01005361#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005362 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5363 ssl->conf->ignore_unexpected_cid
5364 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5365 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005367 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005368 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005369#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005370
Paul Bakker5121ce52009-01-03 21:22:43 +00005371 return( ret );
5372 }
5373
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005374 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005375 {
5376 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005377 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005378 }
5379
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005380 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005381 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005382
Hanno Beckera0e20d02019-05-15 14:03:01 +01005383#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005384 /* We have already checked the record content type
5385 * in ssl_parse_record_header(), failing or silently
5386 * dropping the record in the case of an unknown type.
5387 *
5388 * Since with the use of CIDs, the record content type
5389 * might change during decryption, re-check the record
5390 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005391 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005392 {
5393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5394 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5395 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005396#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005397
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005398 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005399 {
5400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5401 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005402 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005403 {
5404 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5406 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5407 }
5408#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5409
5410 ssl->nb_zero++;
5411
5412 /*
5413 * Three or more empty messages may be a DoS attack
5414 * (excessive CPU consumption).
5415 */
5416 if( ssl->nb_zero > 3 )
5417 {
5418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005419 "messages, possible DoS attack" ) );
5420 /* Treat the records as if they were not properly authenticated,
5421 * thereby failing the connection if we see more than allowed
5422 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005423 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5424 }
5425 }
5426 else
5427 ssl->nb_zero = 0;
5428
5429#if defined(MBEDTLS_SSL_PROTO_DTLS)
5430 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5431 {
5432 ; /* in_ctr read from peer, not maintained internally */
5433 }
5434 else
5435#endif
5436 {
5437 unsigned i;
5438 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5439 if( ++ssl->in_ctr[i - 1] != 0 )
5440 break;
5441
5442 /* The loop goes to its end iff the counter is wrapping */
5443 if( i == ssl_ep_len( ssl ) )
5444 {
5445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5446 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5447 }
5448 }
5449
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 }
5451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005453 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005455 {
5456 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005459 return( ret );
5460 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005461 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005465 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005468 }
5469#endif
5470
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005471 /* Check actual (decrypted) record content length against
5472 * configured maximum. */
5473 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5474 {
5475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5476 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5477 }
5478
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005479 return( 0 );
5480}
5481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005483
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005484/*
5485 * Read a record.
5486 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005487 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5488 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5489 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005490 */
Hanno Becker1097b342018-08-15 14:09:41 +01005491
5492/* Helper functions for mbedtls_ssl_read_record(). */
5493static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005494static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5495static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005496
Hanno Becker327c93b2018-08-15 13:56:18 +01005497int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005498 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005499{
5500 int ret;
5501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005503
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005504 if( ssl->keep_current_message == 0 )
5505 {
5506 do {
Simon Butcher99000142016-10-13 17:21:01 +01005507
Hanno Becker26994592018-08-15 14:14:59 +01005508 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005509 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005510 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005511
Hanno Beckere74d5562018-08-15 14:26:08 +01005512 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005513 {
Hanno Becker40f50842018-08-15 14:48:01 +01005514#if defined(MBEDTLS_SSL_PROTO_DTLS)
5515 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005516
Hanno Becker40f50842018-08-15 14:48:01 +01005517 /* We only check for buffered messages if the
5518 * current datagram is fully consumed. */
5519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005520 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005521 {
Hanno Becker40f50842018-08-15 14:48:01 +01005522 if( ssl_load_buffered_message( ssl ) == 0 )
5523 have_buffered = 1;
5524 }
5525
5526 if( have_buffered == 0 )
5527#endif /* MBEDTLS_SSL_PROTO_DTLS */
5528 {
5529 ret = ssl_get_next_record( ssl );
5530 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5531 continue;
5532
5533 if( ret != 0 )
5534 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005535 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005536 return( ret );
5537 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005538 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005539 }
5540
5541 ret = mbedtls_ssl_handle_message_type( ssl );
5542
Hanno Becker40f50842018-08-15 14:48:01 +01005543#if defined(MBEDTLS_SSL_PROTO_DTLS)
5544 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5545 {
5546 /* Buffer future message */
5547 ret = ssl_buffer_message( ssl );
5548 if( ret != 0 )
5549 return( ret );
5550
5551 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5552 }
5553#endif /* MBEDTLS_SSL_PROTO_DTLS */
5554
Hanno Becker90333da2017-10-10 11:27:13 +01005555 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5556 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005557
5558 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005559 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005560 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005561 return( ret );
5562 }
5563
Hanno Becker327c93b2018-08-15 13:56:18 +01005564 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005565 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005566 {
5567 mbedtls_ssl_update_handshake_status( ssl );
5568 }
Simon Butcher99000142016-10-13 17:21:01 +01005569 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005570 else
Simon Butcher99000142016-10-13 17:21:01 +01005571 {
Hanno Becker02f59072018-08-15 14:00:24 +01005572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005573 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005574 }
5575
5576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5577
5578 return( 0 );
5579}
5580
Hanno Becker40f50842018-08-15 14:48:01 +01005581#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005582static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005583{
Hanno Becker40f50842018-08-15 14:48:01 +01005584 if( ssl->in_left > ssl->next_record_offset )
5585 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005586
Hanno Becker40f50842018-08-15 14:48:01 +01005587 return( 0 );
5588}
5589
5590static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5591{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005592 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005593 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005594 int ret = 0;
5595
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005596 if( hs == NULL )
5597 return( -1 );
5598
Hanno Beckere00ae372018-08-20 09:39:42 +01005599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5600
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005601 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5602 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5603 {
5604 /* Check if we have seen a ChangeCipherSpec before.
5605 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005606 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005607 {
5608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5609 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005610 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005611 }
5612
Hanno Becker39b8bc92018-08-28 17:17:13 +01005613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005614 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5615 ssl->in_msglen = 1;
5616 ssl->in_msg[0] = 1;
5617
5618 /* As long as they are equal, the exact value doesn't matter. */
5619 ssl->in_left = 0;
5620 ssl->next_record_offset = 0;
5621
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005622 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005623 goto exit;
5624 }
Hanno Becker37f95322018-08-16 13:55:32 +01005625
Hanno Beckerb8f50142018-08-28 10:01:34 +01005626#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005627 /* Debug only */
5628 {
5629 unsigned offset;
5630 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5631 {
5632 hs_buf = &hs->buffering.hs[offset];
5633 if( hs_buf->is_valid == 1 )
5634 {
5635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5636 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005637 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005638 }
5639 }
5640 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005641#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005642
5643 /* Check if we have buffered and/or fully reassembled the
5644 * next handshake message. */
5645 hs_buf = &hs->buffering.hs[0];
5646 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5647 {
5648 /* Synthesize a record containing the buffered HS message. */
5649 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5650 ( hs_buf->data[2] << 8 ) |
5651 hs_buf->data[3];
5652
5653 /* Double-check that we haven't accidentally buffered
5654 * a message that doesn't fit into the input buffer. */
5655 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5656 {
5657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5658 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5659 }
5660
5661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5662 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5663 hs_buf->data, msg_len + 12 );
5664
5665 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5666 ssl->in_hslen = msg_len + 12;
5667 ssl->in_msglen = msg_len + 12;
5668 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5669
5670 ret = 0;
5671 goto exit;
5672 }
5673 else
5674 {
5675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5676 hs->in_msg_seq ) );
5677 }
5678
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005679 ret = -1;
5680
5681exit:
5682
5683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5684 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005685}
5686
Hanno Beckera02b0b42018-08-21 17:20:27 +01005687static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5688 size_t desired )
5689{
5690 int offset;
5691 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5693 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005694
Hanno Becker01315ea2018-08-21 17:22:17 +01005695 /* Get rid of future records epoch first, if such exist. */
5696 ssl_free_buffered_record( ssl );
5697
5698 /* Check if we have enough space available now. */
5699 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5700 hs->buffering.total_bytes_buffered ) )
5701 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005703 return( 0 );
5704 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005705
Hanno Becker4f432ad2018-08-28 10:02:32 +01005706 /* We don't have enough space to buffer the next expected handshake
5707 * message. Remove buffers used for future messages to gain space,
5708 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005709 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5710 offset >= 0; offset-- )
5711 {
5712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5713 offset ) );
5714
Hanno Beckerb309b922018-08-23 13:18:05 +01005715 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005716
5717 /* Check if we have enough space available now. */
5718 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5719 hs->buffering.total_bytes_buffered ) )
5720 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005722 return( 0 );
5723 }
5724 }
5725
5726 return( -1 );
5727}
5728
Hanno Becker40f50842018-08-15 14:48:01 +01005729static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5730{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005731 int ret = 0;
5732 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5733
5734 if( hs == NULL )
5735 return( 0 );
5736
5737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5738
5739 switch( ssl->in_msgtype )
5740 {
5741 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005743
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005744 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005745 break;
5746
5747 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005748 {
5749 unsigned recv_msg_seq_offset;
5750 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5751 mbedtls_ssl_hs_buffer *hs_buf;
5752 size_t msg_len = ssl->in_hslen - 12;
5753
5754 /* We should never receive an old handshake
5755 * message - double-check nonetheless. */
5756 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5757 {
5758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5759 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5760 }
5761
5762 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5763 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5764 {
5765 /* Silently ignore -- message too far in the future */
5766 MBEDTLS_SSL_DEBUG_MSG( 2,
5767 ( "Ignore future HS message with sequence number %u, "
5768 "buffering window %u - %u",
5769 recv_msg_seq, ssl->handshake->in_msg_seq,
5770 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5771
5772 goto exit;
5773 }
5774
5775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5776 recv_msg_seq, recv_msg_seq_offset ) );
5777
5778 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5779
5780 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005781 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005782 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005783 size_t reassembly_buf_sz;
5784
Hanno Becker37f95322018-08-16 13:55:32 +01005785 hs_buf->is_fragmented =
5786 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5787
5788 /* We copy the message back into the input buffer
5789 * after reassembly, so check that it's not too large.
5790 * This is an implementation-specific limitation
5791 * and not one from the standard, hence it is not
5792 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005793 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005794 {
5795 /* Ignore message */
5796 goto exit;
5797 }
5798
Hanno Beckere0b150f2018-08-21 15:51:03 +01005799 /* Check if we have enough space to buffer the message. */
5800 if( hs->buffering.total_bytes_buffered >
5801 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5802 {
5803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5804 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5805 }
5806
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005807 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5808 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005809
5810 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5811 hs->buffering.total_bytes_buffered ) )
5812 {
5813 if( recv_msg_seq_offset > 0 )
5814 {
5815 /* If we can't buffer a future message because
5816 * of space limitations -- ignore. */
5817 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",
5818 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5819 (unsigned) hs->buffering.total_bytes_buffered ) );
5820 goto exit;
5821 }
Hanno Beckere1801392018-08-21 16:51:05 +01005822 else
5823 {
5824 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",
5825 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5826 (unsigned) hs->buffering.total_bytes_buffered ) );
5827 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005828
Hanno Beckera02b0b42018-08-21 17:20:27 +01005829 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005830 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005831 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",
5832 (unsigned) msg_len,
5833 (unsigned) reassembly_buf_sz,
5834 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005835 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005836 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5837 goto exit;
5838 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005839 }
5840
5841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5842 msg_len ) );
5843
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005844 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5845 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005846 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005847 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005848 goto exit;
5849 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005850 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005851
5852 /* Prepare final header: copy msg_type, length and message_seq,
5853 * then add standardised fragment_offset and fragment_length */
5854 memcpy( hs_buf->data, ssl->in_msg, 6 );
5855 memset( hs_buf->data + 6, 0, 3 );
5856 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5857
5858 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005859
5860 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005861 }
5862 else
5863 {
5864 /* Make sure msg_type and length are consistent */
5865 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5866 {
5867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5868 /* Ignore */
5869 goto exit;
5870 }
5871 }
5872
Hanno Becker4422bbb2018-08-20 09:40:19 +01005873 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005874 {
5875 size_t frag_len, frag_off;
5876 unsigned char * const msg = hs_buf->data + 12;
5877
5878 /*
5879 * Check and copy current fragment
5880 */
5881
5882 /* Validation of header fields already done in
5883 * mbedtls_ssl_prepare_handshake_record(). */
5884 frag_off = ssl_get_hs_frag_off( ssl );
5885 frag_len = ssl_get_hs_frag_len( ssl );
5886
5887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5888 frag_off, frag_len ) );
5889 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5890
5891 if( hs_buf->is_fragmented )
5892 {
5893 unsigned char * const bitmask = msg + msg_len;
5894 ssl_bitmask_set( bitmask, frag_off, frag_len );
5895 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5896 msg_len ) == 0 );
5897 }
5898 else
5899 {
5900 hs_buf->is_complete = 1;
5901 }
5902
5903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5904 hs_buf->is_complete ? "" : "not yet " ) );
5905 }
5906
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005907 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005908 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005909
5910 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005911 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005912 break;
5913 }
5914
5915exit:
5916
5917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5918 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005919}
5920#endif /* MBEDTLS_SSL_PROTO_DTLS */
5921
Hanno Becker1097b342018-08-15 14:09:41 +01005922static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005923{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005924 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005925 * Consume last content-layer message and potentially
5926 * update in_msglen which keeps track of the contents'
5927 * consumption state.
5928 *
5929 * (1) Handshake messages:
5930 * Remove last handshake message, move content
5931 * and adapt in_msglen.
5932 *
5933 * (2) Alert messages:
5934 * Consume whole record content, in_msglen = 0.
5935 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005936 * (3) Change cipher spec:
5937 * Consume whole record content, in_msglen = 0.
5938 *
5939 * (4) Application data:
5940 * Don't do anything - the record layer provides
5941 * the application data as a stream transport
5942 * and consumes through mbedtls_ssl_read only.
5943 *
5944 */
5945
5946 /* Case (1): Handshake messages */
5947 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005948 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005949 /* Hard assertion to be sure that no application data
5950 * is in flight, as corrupting ssl->in_msglen during
5951 * ssl->in_offt != NULL is fatal. */
5952 if( ssl->in_offt != NULL )
5953 {
5954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5955 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5956 }
5957
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005958 /*
5959 * Get next Handshake message in the current record
5960 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005961
Hanno Becker4a810fb2017-05-24 16:27:30 +01005962 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005963 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005964 * current handshake content: If DTLS handshake
5965 * fragmentation is used, that's the fragment
5966 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005967 * size here is faulty and should be changed at
5968 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005969 * (2) While it doesn't seem to cause problems, one
5970 * has to be very careful not to assume that in_hslen
5971 * is always <= in_msglen in a sensible communication.
5972 * Again, it's wrong for DTLS handshake fragmentation.
5973 * The following check is therefore mandatory, and
5974 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005975 * Additionally, ssl->in_hslen might be arbitrarily out of
5976 * bounds after handling a DTLS message with an unexpected
5977 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005978 */
5979 if( ssl->in_hslen < ssl->in_msglen )
5980 {
5981 ssl->in_msglen -= ssl->in_hslen;
5982 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5983 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005984
Hanno Becker4a810fb2017-05-24 16:27:30 +01005985 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5986 ssl->in_msg, ssl->in_msglen );
5987 }
5988 else
5989 {
5990 ssl->in_msglen = 0;
5991 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005992
Hanno Becker4a810fb2017-05-24 16:27:30 +01005993 ssl->in_hslen = 0;
5994 }
5995 /* Case (4): Application data */
5996 else if( ssl->in_offt != NULL )
5997 {
5998 return( 0 );
5999 }
6000 /* Everything else (CCS & Alerts) */
6001 else
6002 {
6003 ssl->in_msglen = 0;
6004 }
6005
Hanno Becker1097b342018-08-15 14:09:41 +01006006 return( 0 );
6007}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006008
Hanno Beckere74d5562018-08-15 14:26:08 +01006009static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6010{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006011 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006012 return( 1 );
6013
6014 return( 0 );
6015}
6016
Hanno Becker5f066e72018-08-16 14:56:31 +01006017#if defined(MBEDTLS_SSL_PROTO_DTLS)
6018
6019static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6020{
6021 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6022 if( hs == NULL )
6023 return;
6024
Hanno Becker01315ea2018-08-21 17:22:17 +01006025 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006026 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006027 hs->buffering.total_bytes_buffered -=
6028 hs->buffering.future_record.len;
6029
6030 mbedtls_free( hs->buffering.future_record.data );
6031 hs->buffering.future_record.data = NULL;
6032 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006033}
6034
6035static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6036{
6037 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6038 unsigned char * rec;
6039 size_t rec_len;
6040 unsigned rec_epoch;
6041
6042 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6043 return( 0 );
6044
6045 if( hs == NULL )
6046 return( 0 );
6047
Hanno Becker5f066e72018-08-16 14:56:31 +01006048 rec = hs->buffering.future_record.data;
6049 rec_len = hs->buffering.future_record.len;
6050 rec_epoch = hs->buffering.future_record.epoch;
6051
6052 if( rec == NULL )
6053 return( 0 );
6054
Hanno Becker4cb782d2018-08-20 11:19:05 +01006055 /* Only consider loading future records if the
6056 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006057 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006058 return( 0 );
6059
Hanno Becker5f066e72018-08-16 14:56:31 +01006060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6061
6062 if( rec_epoch != ssl->in_epoch )
6063 {
6064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6065 goto exit;
6066 }
6067
6068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6069
6070 /* Double-check that the record is not too large */
6071 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6072 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6073 {
6074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6076 }
6077
6078 memcpy( ssl->in_hdr, rec, rec_len );
6079 ssl->in_left = rec_len;
6080 ssl->next_record_offset = 0;
6081
6082 ssl_free_buffered_record( ssl );
6083
6084exit:
6085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6086 return( 0 );
6087}
6088
Hanno Becker519f15d2019-07-11 12:43:20 +01006089static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6090 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006091{
6092 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006093
6094 /* Don't buffer future records outside handshakes. */
6095 if( hs == NULL )
6096 return( 0 );
6097
6098 /* Only buffer handshake records (we are only interested
6099 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006100 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006101 return( 0 );
6102
6103 /* Don't buffer more than one future epoch record. */
6104 if( hs->buffering.future_record.data != NULL )
6105 return( 0 );
6106
Hanno Becker01315ea2018-08-21 17:22:17 +01006107 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006108 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006109 hs->buffering.total_bytes_buffered ) )
6110 {
6111 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 +01006112 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006113 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006114 return( 0 );
6115 }
6116
Hanno Becker5f066e72018-08-16 14:56:31 +01006117 /* Buffer record */
6118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6119 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006120 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006121
6122 /* ssl_parse_record_header() only considers records
6123 * of the next epoch as candidates for buffering. */
6124 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006125 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006126
6127 hs->buffering.future_record.data =
6128 mbedtls_calloc( 1, hs->buffering.future_record.len );
6129 if( hs->buffering.future_record.data == NULL )
6130 {
6131 /* If we run out of RAM trying to buffer a
6132 * record from the next epoch, just ignore. */
6133 return( 0 );
6134 }
6135
Hanno Becker519f15d2019-07-11 12:43:20 +01006136 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006137
Hanno Becker519f15d2019-07-11 12:43:20 +01006138 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006139 return( 0 );
6140}
6141
6142#endif /* MBEDTLS_SSL_PROTO_DTLS */
6143
Hanno Beckere74d5562018-08-15 14:26:08 +01006144static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006145{
6146 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006147 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006148
Hanno Becker5f066e72018-08-16 14:56:31 +01006149#if defined(MBEDTLS_SSL_PROTO_DTLS)
6150 /* We might have buffered a future record; if so,
6151 * and if the epoch matches now, load it.
6152 * On success, this call will set ssl->in_left to
6153 * the length of the buffered record, so that
6154 * the calls to ssl_fetch_input() below will
6155 * essentially be no-ops. */
6156 ret = ssl_load_buffered_record( ssl );
6157 if( ret != 0 )
6158 return( ret );
6159#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006160
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006161 /* Ensure that we have enough space available for the default form
6162 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6163 * with no space for CIDs counted in). */
6164 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6165 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006168 return( ret );
6169 }
6170
Hanno Beckere5e7e782019-07-11 12:29:35 +01006171 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6172 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006175 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006176 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006177 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6178 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006179 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006180 if( ret != 0 )
6181 return( ret );
6182
6183 /* Fall through to handling of unexpected records */
6184 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6185 }
6186
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006187 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6188 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006189#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006190 /* Reset in pointers to default state for TLS/DTLS records,
6191 * assuming no CID and no offset between record content and
6192 * record plaintext. */
6193 ssl_update_in_pointers( ssl );
6194
Hanno Becker7ae20e02019-07-12 08:33:49 +01006195 /* Setup internal message pointers from record structure. */
6196 ssl->in_msgtype = rec.type;
6197#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6198 ssl->in_len = ssl->in_cid + rec.cid_len;
6199#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6200 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6201 ssl->in_msglen = rec.data_len;
6202
Hanno Becker2fddd372019-07-10 14:37:41 +01006203 ret = ssl_check_client_reconnect( ssl );
6204 if( ret != 0 )
6205 return( ret );
6206#endif
6207
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006208 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006209 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006210
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6212 "(header)" ) );
6213 }
6214 else
6215 {
6216 /* Skip invalid record and the rest of the datagram */
6217 ssl->next_record_offset = 0;
6218 ssl->in_left = 0;
6219
6220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6221 "(header)" ) );
6222 }
6223
6224 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006225 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006226 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006227 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006228#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006229 {
6230 return( ret );
6231 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006232 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006235 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006236 {
Hanno Beckera8814792019-07-10 15:01:45 +01006237 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006238 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006239 if( ssl->next_record_offset < ssl->in_left )
6240 {
6241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6242 }
6243 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006244 else
6245#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006246 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006247 /*
6248 * Fetch record contents from underlying transport.
6249 */
Hanno Beckera3175662019-07-11 12:50:29 +01006250 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006251 if( ret != 0 )
6252 {
6253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6254 return( ret );
6255 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006256
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006257 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006258 }
6259
6260 /*
6261 * Decrypt record contents.
6262 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006263
Hanno Beckerfdf66042019-07-11 13:07:45 +01006264 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006267 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006268 {
6269 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006270 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006271 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006272 /* Except when waiting for Finished as a bad mac here
6273 * probably means something went wrong in the handshake
6274 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6275 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6276 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6277 {
6278#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6279 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6280 {
6281 mbedtls_ssl_send_alert_message( ssl,
6282 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6283 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6284 }
6285#endif
6286 return( ret );
6287 }
6288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006290 if( ssl->conf->badmac_limit != 0 &&
6291 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6294 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006295 }
6296#endif
6297
Hanno Becker4a810fb2017-05-24 16:27:30 +01006298 /* As above, invalid records cause
6299 * dismissal of the whole datagram. */
6300
6301 ssl->next_record_offset = 0;
6302 ssl->in_left = 0;
6303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006305 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006306 }
6307
6308 return( ret );
6309 }
6310 else
6311#endif
6312 {
6313 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6315 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 mbedtls_ssl_send_alert_message( ssl,
6318 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6319 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006320 }
6321#endif
6322 return( ret );
6323 }
6324 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006325
Hanno Becker44d89b22019-07-12 09:40:44 +01006326
6327 /* Reset in pointers to default state for TLS/DTLS records,
6328 * assuming no CID and no offset between record content and
6329 * record plaintext. */
6330 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006331#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6332 ssl->in_len = ssl->in_cid + rec.cid_len;
6333#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6334 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006335
Hanno Becker8685c822019-07-12 09:37:30 +01006336 /* The record content type may change during decryption,
6337 * so re-read it. */
6338 ssl->in_msgtype = rec.type;
6339 /* Also update the input buffer, because unfortunately
6340 * the server-side ssl_parse_client_hello() reparses the
6341 * record header when receiving a ClientHello initiating
6342 * a renegotiation. */
6343 ssl->in_hdr[0] = rec.type;
6344 ssl->in_msg = rec.buf + rec.data_offset;
6345 ssl->in_msglen = rec.data_len;
6346 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6347 ssl->in_len[1] = (unsigned char)( rec.data_len );
6348
Simon Butcher99000142016-10-13 17:21:01 +01006349 return( 0 );
6350}
6351
6352int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6353{
6354 int ret;
6355
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006356 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006357 * Handle particular types of records
6358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006360 {
Simon Butcher99000142016-10-13 17:21:01 +01006361 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6362 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006363 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006365 }
6366
Hanno Beckere678eaa2018-08-21 14:57:46 +01006367 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006368 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006369 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006370 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6372 ssl->in_msglen ) );
6373 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006374 }
6375
Hanno Beckere678eaa2018-08-21 14:57:46 +01006376 if( ssl->in_msg[0] != 1 )
6377 {
6378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6379 ssl->in_msg[0] ) );
6380 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6381 }
6382
6383#if defined(MBEDTLS_SSL_PROTO_DTLS)
6384 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6385 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6386 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6387 {
6388 if( ssl->handshake == NULL )
6389 {
6390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6391 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6392 }
6393
6394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6395 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6396 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006397#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006398 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006401 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006402 if( ssl->in_msglen != 2 )
6403 {
6404 /* Note: Standard allows for more than one 2 byte alert
6405 to be packed in a single message, but Mbed TLS doesn't
6406 currently support this. */
6407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6408 ssl->in_msglen ) );
6409 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6410 }
6411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006413 ssl->in_msg[0], ssl->in_msg[1] ) );
6414
6415 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006416 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006418 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006421 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006423 }
6424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6426 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6429 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006430 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006431
6432#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6433 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6434 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6435 {
Hanno Becker90333da2017-10-10 11:27:13 +01006436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006437 /* Will be handled when trying to parse ServerHello */
6438 return( 0 );
6439 }
6440#endif
6441
6442#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6443 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6444 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6445 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6446 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6447 {
6448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6449 /* Will be handled in mbedtls_ssl_parse_certificate() */
6450 return( 0 );
6451 }
6452#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6453
6454 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006455 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006456 }
6457
Hanno Beckerc76c6192017-06-06 10:03:17 +01006458#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006459 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006460 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006461 /* Drop unexpected ApplicationData records,
6462 * except at the beginning of renegotiations */
6463 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6464 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6465#if defined(MBEDTLS_SSL_RENEGOTIATION)
6466 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6467 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006468#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006469 )
6470 {
6471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6472 return( MBEDTLS_ERR_SSL_NON_FATAL );
6473 }
6474
6475 if( ssl->handshake != NULL &&
6476 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6477 {
6478 ssl_handshake_wrapup_free_hs_transform( ssl );
6479 }
6480 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006481#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006482
Paul Bakker5121ce52009-01-03 21:22:43 +00006483 return( 0 );
6484}
6485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006487{
6488 int ret;
6489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6491 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6492 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006493 {
6494 return( ret );
6495 }
6496
6497 return( 0 );
6498}
6499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006501 unsigned char level,
6502 unsigned char message )
6503{
6504 int ret;
6505
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006506 if( ssl == NULL || ssl->conf == NULL )
6507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006513 ssl->out_msglen = 2;
6514 ssl->out_msg[0] = level;
6515 ssl->out_msg[1] = message;
6516
Hanno Becker67bc7c32018-08-06 11:33:50 +01006517 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006520 return( ret );
6521 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006523
6524 return( 0 );
6525}
6526
Hanno Beckerb9d44792019-02-08 07:19:04 +00006527#if defined(MBEDTLS_X509_CRT_PARSE_C)
6528static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6529{
6530#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6531 if( session->peer_cert != NULL )
6532 {
6533 mbedtls_x509_crt_free( session->peer_cert );
6534 mbedtls_free( session->peer_cert );
6535 session->peer_cert = NULL;
6536 }
6537#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6538 if( session->peer_cert_digest != NULL )
6539 {
6540 /* Zeroization is not necessary. */
6541 mbedtls_free( session->peer_cert_digest );
6542 session->peer_cert_digest = NULL;
6543 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6544 session->peer_cert_digest_len = 0;
6545 }
6546#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6547}
6548#endif /* MBEDTLS_X509_CRT_PARSE_C */
6549
Paul Bakker5121ce52009-01-03 21:22:43 +00006550/*
6551 * Handshake functions
6552 */
Hanno Becker21489932019-02-05 13:20:55 +00006553#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006554/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006556{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006557 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6558 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006561
Hanno Becker7177a882019-02-05 13:36:46 +00006562 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006565 ssl->state++;
6566 return( 0 );
6567 }
6568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6570 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006571}
6572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006574{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006575 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6576 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006579
Hanno Becker7177a882019-02-05 13:36:46 +00006580 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006583 ssl->state++;
6584 return( 0 );
6585 }
6586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006589}
Gilles Peskinef9828522017-05-03 12:28:43 +02006590
Hanno Becker21489932019-02-05 13:20:55 +00006591#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006592/* Some certificate support -> implement write and parse */
6593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006597 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006599 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6600 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006603
Hanno Becker7177a882019-02-05 13:36:46 +00006604 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006607 ssl->state++;
6608 return( 0 );
6609 }
6610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006612 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006613 {
6614 if( ssl->client_auth == 0 )
6615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006617 ssl->state++;
6618 return( 0 );
6619 }
6620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006622 /*
6623 * If using SSLv3 and got no cert, send an Alert message
6624 * (otherwise an empty Certificate message will be sent).
6625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6627 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006628 {
6629 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6631 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6632 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006635 goto write_msg;
6636 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006638 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639#endif /* MBEDTLS_SSL_CLI_C */
6640#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006641 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6646 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006647 }
6648 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006649#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006652
6653 /*
6654 * 0 . 0 handshake type
6655 * 1 . 3 handshake length
6656 * 4 . 6 length of all certs
6657 * 7 . 9 length of cert. 1
6658 * 10 . n-1 peer certificate
6659 * n . n+2 length of cert. 2
6660 * n+3 . ... upper level cert, etc.
6661 */
6662 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006664
Paul Bakker29087132010-03-21 21:03:34 +00006665 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006666 {
6667 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006668 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006671 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006673 }
6674
6675 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6676 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6677 ssl->out_msg[i + 2] = (unsigned char)( n );
6678
6679 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6680 i += n; crt = crt->next;
6681 }
6682
6683 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6684 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6685 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6686
6687 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6689 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006690
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006691#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006692write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006693#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006694
6695 ssl->state++;
6696
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006697 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006698 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006699 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006700 return( ret );
6701 }
6702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006704
Paul Bakkered27a042013-04-18 22:46:23 +02006705 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006706}
6707
Hanno Becker84879e32019-01-31 07:44:03 +00006708#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006709
6710#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006711static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6712 unsigned char *crt_buf,
6713 size_t crt_buf_len )
6714{
6715 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6716
6717 if( peer_crt == NULL )
6718 return( -1 );
6719
6720 if( peer_crt->raw.len != crt_buf_len )
6721 return( -1 );
6722
Hanno Becker46f34d02019-02-08 14:00:04 +00006723 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006724}
Hanno Becker177475a2019-02-05 17:02:46 +00006725#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6726static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6727 unsigned char *crt_buf,
6728 size_t crt_buf_len )
6729{
6730 int ret;
6731 unsigned char const * const peer_cert_digest =
6732 ssl->session->peer_cert_digest;
6733 mbedtls_md_type_t const peer_cert_digest_type =
6734 ssl->session->peer_cert_digest_type;
6735 mbedtls_md_info_t const * const digest_info =
6736 mbedtls_md_info_from_type( peer_cert_digest_type );
6737 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6738 size_t digest_len;
6739
6740 if( peer_cert_digest == NULL || digest_info == NULL )
6741 return( -1 );
6742
6743 digest_len = mbedtls_md_get_size( digest_info );
6744 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6745 return( -1 );
6746
6747 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6748 if( ret != 0 )
6749 return( -1 );
6750
6751 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6752}
6753#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006754#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006755
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006756/*
6757 * Once the certificate message is read, parse it into a cert chain and
6758 * perform basic checks, but leave actual verification to the caller
6759 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006760static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6761 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006762{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006763 int ret;
6764#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6765 int crt_cnt=0;
6766#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006767 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006768 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006773 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6774 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006776 }
6777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6779 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006782 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6783 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006785 }
6786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006788
Paul Bakker5121ce52009-01-03 21:22:43 +00006789 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006791 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006792 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006793
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006794 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
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_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006801 }
6802
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006803 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6804 i += 3;
6805
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006806 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006807 while( i < ssl->in_hslen )
6808 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006809 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006810 if ( i + 3 > ssl->in_hslen ) {
6811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006812 mbedtls_ssl_send_alert_message( ssl,
6813 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6814 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006815 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6816 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006817 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6818 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006819 if( ssl->in_msg[i] != 0 )
6820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006822 mbedtls_ssl_send_alert_message( ssl,
6823 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 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006829 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6830 | (unsigned int) ssl->in_msg[i + 2];
6831 i += 3;
6832
6833 if( n < 128 || i + n > ssl->in_hslen )
6834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006836 mbedtls_ssl_send_alert_message( ssl,
6837 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6838 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006840 }
6841
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006842 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006843#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6844 if( crt_cnt++ == 0 &&
6845 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6846 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006847 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006848 /* During client-side renegotiation, check that the server's
6849 * end-CRTs hasn't changed compared to the initial handshake,
6850 * mitigating the triple handshake attack. On success, reuse
6851 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6853 if( ssl_check_peer_crt_unchanged( ssl,
6854 &ssl->in_msg[i],
6855 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006856 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6858 mbedtls_ssl_send_alert_message( ssl,
6859 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6860 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6861 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006862 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006863
6864 /* Now we can safely free the original chain. */
6865 ssl_clear_peer_cert( ssl->session );
6866 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006867#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6868
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006869 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006870#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006871 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006872#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006873 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006874 * it in-place from the input buffer instead of making a copy. */
6875 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6876#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006877 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006878 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006879 case 0: /*ok*/
6880 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6881 /* Ignore certificate with an unknown algorithm: maybe a
6882 prior certificate was already trusted. */
6883 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006884
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006885 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6886 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6887 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006888
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006889 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6890 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6891 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006892
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006893 default:
6894 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6895 crt_parse_der_failed:
6896 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6897 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6898 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006899 }
6900
6901 i += n;
6902 }
6903
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006904 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006905 return( 0 );
6906}
6907
Hanno Becker4a55f632019-02-05 12:49:06 +00006908#if defined(MBEDTLS_SSL_SRV_C)
6909static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6910{
6911 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6912 return( -1 );
6913
6914#if defined(MBEDTLS_SSL_PROTO_SSL3)
6915 /*
6916 * Check if the client sent an empty certificate
6917 */
6918 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6919 {
6920 if( ssl->in_msglen == 2 &&
6921 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6922 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6923 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6924 {
6925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6926 return( 0 );
6927 }
6928
6929 return( -1 );
6930 }
6931#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6932
6933#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6934 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6935 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6936 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6937 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6938 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6939 {
6940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6941 return( 0 );
6942 }
6943
6944 return( -1 );
6945#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6946 MBEDTLS_SSL_PROTO_TLS1_2 */
6947}
6948#endif /* MBEDTLS_SSL_SRV_C */
6949
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006950/* Check if a certificate message is expected.
6951 * Return either
6952 * - SSL_CERTIFICATE_EXPECTED, or
6953 * - SSL_CERTIFICATE_SKIP
6954 * indicating whether a Certificate message is expected or not.
6955 */
6956#define SSL_CERTIFICATE_EXPECTED 0
6957#define SSL_CERTIFICATE_SKIP 1
6958static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6959 int authmode )
6960{
6961 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006962 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006963
6964 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6965 return( SSL_CERTIFICATE_SKIP );
6966
6967#if defined(MBEDTLS_SSL_SRV_C)
6968 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6969 {
6970 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6971 return( SSL_CERTIFICATE_SKIP );
6972
6973 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6974 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006975 ssl->session_negotiate->verify_result =
6976 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6977 return( SSL_CERTIFICATE_SKIP );
6978 }
6979 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006980#else
6981 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006982#endif /* MBEDTLS_SSL_SRV_C */
6983
6984 return( SSL_CERTIFICATE_EXPECTED );
6985}
6986
Hanno Becker68636192019-02-05 14:36:34 +00006987static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6988 int authmode,
6989 mbedtls_x509_crt *chain,
6990 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006991{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006992 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006993 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006994 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006995 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006996
Hanno Becker8927c832019-04-03 12:52:50 +01006997 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6998 void *p_vrfy;
6999
Hanno Becker68636192019-02-05 14:36:34 +00007000 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7001 return( 0 );
7002
Hanno Becker8927c832019-04-03 12:52:50 +01007003 if( ssl->f_vrfy != NULL )
7004 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01007005 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01007006 f_vrfy = ssl->f_vrfy;
7007 p_vrfy = ssl->p_vrfy;
7008 }
7009 else
7010 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01007011 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01007012 f_vrfy = ssl->conf->f_vrfy;
7013 p_vrfy = ssl->conf->p_vrfy;
7014 }
7015
Hanno Becker68636192019-02-05 14:36:34 +00007016 /*
7017 * Main check: verify certificate
7018 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007019#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7020 if( ssl->conf->f_ca_cb != NULL )
7021 {
7022 ((void) rs_ctx);
7023 have_ca_chain = 1;
7024
7025 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03007026 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007027 chain,
7028 ssl->conf->f_ca_cb,
7029 ssl->conf->p_ca_cb,
7030 ssl->conf->cert_profile,
7031 ssl->hostname,
7032 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007033 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007034 }
7035 else
7036#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7037 {
7038 mbedtls_x509_crt *ca_chain;
7039 mbedtls_x509_crl *ca_crl;
7040
7041#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7042 if( ssl->handshake->sni_ca_chain != NULL )
7043 {
7044 ca_chain = ssl->handshake->sni_ca_chain;
7045 ca_crl = ssl->handshake->sni_ca_crl;
7046 }
7047 else
7048#endif
7049 {
7050 ca_chain = ssl->conf->ca_chain;
7051 ca_crl = ssl->conf->ca_crl;
7052 }
7053
7054 if( ca_chain != NULL )
7055 have_ca_chain = 1;
7056
7057 ret = mbedtls_x509_crt_verify_restartable(
7058 chain,
7059 ca_chain, ca_crl,
7060 ssl->conf->cert_profile,
7061 ssl->hostname,
7062 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007063 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007064 }
Hanno Becker68636192019-02-05 14:36:34 +00007065
7066 if( ret != 0 )
7067 {
7068 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7069 }
7070
7071#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7072 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7073 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7074#endif
7075
7076 /*
7077 * Secondary checks: always done, but change 'ret' only if it was 0
7078 */
7079
7080#if defined(MBEDTLS_ECP_C)
7081 {
7082 const mbedtls_pk_context *pk = &chain->pk;
7083
7084 /* If certificate uses an EC key, make sure the curve is OK */
7085 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7086 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7087 {
7088 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7089
7090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7091 if( ret == 0 )
7092 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7093 }
7094 }
7095#endif /* MBEDTLS_ECP_C */
7096
7097 if( mbedtls_ssl_check_cert_usage( chain,
7098 ciphersuite_info,
7099 ! ssl->conf->endpoint,
7100 &ssl->session_negotiate->verify_result ) != 0 )
7101 {
7102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7103 if( ret == 0 )
7104 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7105 }
7106
7107 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7108 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7109 * with details encoded in the verification flags. All other kinds
7110 * of error codes, including those from the user provided f_vrfy
7111 * functions, are treated as fatal and lead to a failure of
7112 * ssl_parse_certificate even if verification was optional. */
7113 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7114 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7115 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7116 {
7117 ret = 0;
7118 }
7119
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007120 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007121 {
7122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7123 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7124 }
7125
7126 if( ret != 0 )
7127 {
7128 uint8_t alert;
7129
7130 /* The certificate may have been rejected for several reasons.
7131 Pick one and send the corresponding alert. Which alert to send
7132 may be a subject of debate in some cases. */
7133 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7134 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7135 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7136 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7137 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7138 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7139 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7140 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7141 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7142 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7143 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7144 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7145 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7146 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7147 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7148 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7149 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7150 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7151 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7152 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7153 else
7154 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7155 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7156 alert );
7157 }
7158
7159#if defined(MBEDTLS_DEBUG_C)
7160 if( ssl->session_negotiate->verify_result != 0 )
7161 {
7162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7163 ssl->session_negotiate->verify_result ) );
7164 }
7165 else
7166 {
7167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7168 }
7169#endif /* MBEDTLS_DEBUG_C */
7170
7171 return( ret );
7172}
7173
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007174#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7175static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7176 unsigned char *start, size_t len )
7177{
7178 int ret;
7179 /* Remember digest of the peer's end-CRT. */
7180 ssl->session_negotiate->peer_cert_digest =
7181 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7182 if( ssl->session_negotiate->peer_cert_digest == NULL )
7183 {
7184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7185 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7186 mbedtls_ssl_send_alert_message( ssl,
7187 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7188 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7189
7190 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7191 }
7192
7193 ret = mbedtls_md( mbedtls_md_info_from_type(
7194 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7195 start, len,
7196 ssl->session_negotiate->peer_cert_digest );
7197
7198 ssl->session_negotiate->peer_cert_digest_type =
7199 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7200 ssl->session_negotiate->peer_cert_digest_len =
7201 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7202
7203 return( ret );
7204}
7205
7206static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7207 unsigned char *start, size_t len )
7208{
7209 unsigned char *end = start + len;
7210 int ret;
7211
7212 /* Make a copy of the peer's raw public key. */
7213 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7214 ret = mbedtls_pk_parse_subpubkey( &start, end,
7215 &ssl->handshake->peer_pubkey );
7216 if( ret != 0 )
7217 {
7218 /* We should have parsed the public key before. */
7219 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7220 }
7221
7222 return( 0 );
7223}
7224#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7225
Hanno Becker68636192019-02-05 14:36:34 +00007226int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7227{
7228 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007229 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007230#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7231 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7232 ? ssl->handshake->sni_authmode
7233 : ssl->conf->authmode;
7234#else
7235 const int authmode = ssl->conf->authmode;
7236#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007237 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007238 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007239
7240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7241
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007242 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7243 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007244 {
7245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007246 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007247 }
7248
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007249#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7250 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007251 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007252 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007253 chain = ssl->handshake->ecrs_peer_cert;
7254 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007255 goto crt_verify;
7256 }
7257#endif
7258
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007259 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007260 {
7261 /* mbedtls_ssl_read_record may have sent an alert already. We
7262 let it decide whether to alert. */
7263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007264 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007265 }
7266
Hanno Becker4a55f632019-02-05 12:49:06 +00007267#if defined(MBEDTLS_SSL_SRV_C)
7268 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7269 {
7270 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007271
7272 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007273 ret = 0;
7274 else
7275 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007276
Hanno Becker6bdfab22019-02-05 13:11:17 +00007277 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007278 }
7279#endif /* MBEDTLS_SSL_SRV_C */
7280
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007281 /* Clear existing peer CRT structure in case we tried to
7282 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007283 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007284
7285 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7286 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007287 {
7288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7289 sizeof( mbedtls_x509_crt ) ) );
7290 mbedtls_ssl_send_alert_message( ssl,
7291 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7292 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007293
Hanno Becker3dad3112019-02-05 17:19:52 +00007294 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7295 goto exit;
7296 }
7297 mbedtls_x509_crt_init( chain );
7298
7299 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007300 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007301 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007302
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007303#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7304 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007305 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007306
7307crt_verify:
7308 if( ssl->handshake->ecrs_enabled)
7309 rs_ctx = &ssl->handshake->ecrs_ctx;
7310#endif
7311
Hanno Becker68636192019-02-05 14:36:34 +00007312 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007313 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007314 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007315 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007316
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007317#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007318 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007319 unsigned char *crt_start, *pk_start;
7320 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007321
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007322 /* We parse the CRT chain without copying, so
7323 * these pointers point into the input buffer,
7324 * and are hence still valid after freeing the
7325 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007326
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007327 crt_start = chain->raw.p;
7328 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007329
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007330 pk_start = chain->pk_raw.p;
7331 pk_len = chain->pk_raw.len;
7332
7333 /* Free the CRT structures before computing
7334 * digest and copying the peer's public key. */
7335 mbedtls_x509_crt_free( chain );
7336 mbedtls_free( chain );
7337 chain = NULL;
7338
7339 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007340 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007341 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007342
7343 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7344 if( ret != 0 )
7345 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007346 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007347#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7348 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007349 ssl->session_negotiate->peer_cert = chain;
7350 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007351#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007354
Hanno Becker6bdfab22019-02-05 13:11:17 +00007355exit:
7356
Hanno Becker3dad3112019-02-05 17:19:52 +00007357 if( ret == 0 )
7358 ssl->state++;
7359
7360#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7361 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7362 {
7363 ssl->handshake->ecrs_peer_cert = chain;
7364 chain = NULL;
7365 }
7366#endif
7367
7368 if( chain != NULL )
7369 {
7370 mbedtls_x509_crt_free( chain );
7371 mbedtls_free( chain );
7372 }
7373
Paul Bakker5121ce52009-01-03 21:22:43 +00007374 return( ret );
7375}
Hanno Becker21489932019-02-05 13:20:55 +00007376#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007379{
7380 int ret;
7381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007385 ssl->out_msglen = 1;
7386 ssl->out_msg[0] = 1;
7387
Paul Bakker5121ce52009-01-03 21:22:43 +00007388 ssl->state++;
7389
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007390 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007391 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007392 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007393 return( ret );
7394 }
7395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007397
7398 return( 0 );
7399}
7400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007402{
7403 int ret;
7404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007406
Hanno Becker327c93b2018-08-15 13:56:18 +01007407 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007410 return( ret );
7411 }
7412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007416 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7417 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007419 }
7420
Hanno Beckere678eaa2018-08-21 14:57:46 +01007421 /* CCS records are only accepted if they have length 1 and content '1',
7422 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007423
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007424 /*
7425 * Switch to our negotiated transform and session parameters for inbound
7426 * data.
7427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007429 ssl->transform_in = ssl->transform_negotiate;
7430 ssl->session_in = ssl->session_negotiate;
7431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007433 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007436 ssl_dtls_replay_reset( ssl );
7437#endif
7438
7439 /* Increment epoch */
7440 if( ++ssl->in_epoch == 0 )
7441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007443 /* This is highly unlikely to happen for legitimate reasons, so
7444 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007446 }
7447 }
7448 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007450 memset( ssl->in_ctr, 0, 8 );
7451
Hanno Becker79594fd2019-05-08 09:38:41 +01007452 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7455 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007460 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7461 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007463 }
7464 }
7465#endif
7466
Paul Bakker5121ce52009-01-03 21:22:43 +00007467 ssl->state++;
7468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007470
7471 return( 0 );
7472}
7473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7475 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007476{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007477 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7480 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7481 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007482 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007483 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007484#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007485#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7486#if defined(MBEDTLS_SHA512_C)
7487 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007488 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7489 else
7490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491#if defined(MBEDTLS_SHA256_C)
7492 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007493 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007494 else
7495#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007499 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007500 }
Paul Bakker380da532012-04-18 16:10:25 +00007501}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007504{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7506 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007507 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7508 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007509#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7511#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007512#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007513 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007514 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7515#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007516 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007517#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007518#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007520#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007521 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007522 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007523#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007524 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007525#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007526#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007528}
7529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007531 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007532{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7534 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007535 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7536 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007537#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7539#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007540#if defined(MBEDTLS_USE_PSA_CRYPTO)
7541 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7542#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007543 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007544#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007545#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007547#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007548 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007549#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007550 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007551#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007552#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007554}
7555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7557 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7558static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007559 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007560{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007561 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7562 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007563}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007564#endif
Paul Bakker380da532012-04-18 16:10:25 +00007565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7567#if defined(MBEDTLS_SHA256_C)
7568static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007569 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007570{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007571#if defined(MBEDTLS_USE_PSA_CRYPTO)
7572 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7573#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007574 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007575#endif
Paul Bakker380da532012-04-18 16:10:25 +00007576}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007577#endif
Paul Bakker380da532012-04-18 16:10:25 +00007578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579#if defined(MBEDTLS_SHA512_C)
7580static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007581 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007582{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007583#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007584 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007585#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007586 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007587#endif
Paul Bakker380da532012-04-18 16:10:25 +00007588}
Paul Bakker769075d2012-11-24 11:26:46 +01007589#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007593static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007595{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007596 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007597 mbedtls_md5_context md5;
7598 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007599
Paul Bakker5121ce52009-01-03 21:22:43 +00007600 unsigned char padbuf[48];
7601 unsigned char md5sum[16];
7602 unsigned char sha1sum[20];
7603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007605 if( !session )
7606 session = ssl->session;
7607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007609
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007610 mbedtls_md5_init( &md5 );
7611 mbedtls_sha1_init( &sha1 );
7612
7613 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7614 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007615
7616 /*
7617 * SSLv3:
7618 * hash =
7619 * MD5( master + pad2 +
7620 * MD5( handshake + sender + master + pad1 ) )
7621 * + SHA1( master + pad2 +
7622 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007623 */
7624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007626 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7627 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007628#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007631 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7632 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007633#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007636 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007637
Paul Bakker1ef83d62012-04-11 12:09:53 +00007638 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007639
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007640 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7641 mbedtls_md5_update_ret( &md5, session->master, 48 );
7642 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7643 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007644
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007645 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7646 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7647 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7648 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007649
Paul Bakker1ef83d62012-04-11 12:09:53 +00007650 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007651
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007652 mbedtls_md5_starts_ret( &md5 );
7653 mbedtls_md5_update_ret( &md5, session->master, 48 );
7654 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7655 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7656 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007657
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007658 mbedtls_sha1_starts_ret( &sha1 );
7659 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7660 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7661 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7662 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007665
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007666 mbedtls_md5_free( &md5 );
7667 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007668
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007669 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7670 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7671 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007674}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007678static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007680{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007681 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007682 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007683 mbedtls_md5_context md5;
7684 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007685 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007688 if( !session )
7689 session = ssl->session;
7690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007692
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007693 mbedtls_md5_init( &md5 );
7694 mbedtls_sha1_init( &sha1 );
7695
7696 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7697 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007698
Paul Bakker1ef83d62012-04-11 12:09:53 +00007699 /*
7700 * TLSv1:
7701 * hash = PRF( master, finished_label,
7702 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7703 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007706 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7707 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007708#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007711 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7712 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007713#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007716 ? "client finished"
7717 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007718
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007719 mbedtls_md5_finish_ret( &md5, padbuf );
7720 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007721
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007722 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007723 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007726
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007727 mbedtls_md5_free( &md5 );
7728 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007729
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007730 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007733}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7737#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007738static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007740{
7741 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007742 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007743 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007744#if defined(MBEDTLS_USE_PSA_CRYPTO)
7745 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007746 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007747 psa_status_t status;
7748#else
7749 mbedtls_sha256_context sha256;
7750#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007753 if( !session )
7754 session = ssl->session;
7755
Andrzej Kurekeb342242019-01-29 09:14:33 -05007756 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7757 ? "client finished"
7758 : "server finished";
7759
7760#if defined(MBEDTLS_USE_PSA_CRYPTO)
7761 sha256_psa = psa_hash_operation_init();
7762
7763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7764
7765 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7766 if( status != PSA_SUCCESS )
7767 {
7768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7769 return;
7770 }
7771
7772 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7773 if( status != PSA_SUCCESS )
7774 {
7775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7776 return;
7777 }
7778 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7779#else
7780
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007781 mbedtls_sha256_init( &sha256 );
7782
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007784
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007785 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007786
7787 /*
7788 * TLSv1.2:
7789 * hash = PRF( master, finished_label,
7790 * Hash( handshake ) )[0.11]
7791 */
7792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793#if !defined(MBEDTLS_SHA256_ALT)
7794 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007795 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007796#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007797
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007798 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007799 mbedtls_sha256_free( &sha256 );
7800#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007801
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007802 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007803 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007806
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007807 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007810}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007814static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007816{
7817 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007818 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007819 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007820#if defined(MBEDTLS_USE_PSA_CRYPTO)
7821 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007822 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007823 psa_status_t status;
7824#else
7825 mbedtls_sha512_context sha512;
7826#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007829 if( !session )
7830 session = ssl->session;
7831
Andrzej Kurekeb342242019-01-29 09:14:33 -05007832 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7833 ? "client finished"
7834 : "server finished";
7835
7836#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007837 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007838
7839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7840
Andrzej Kurek972fba52019-01-30 03:29:12 -05007841 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007842 if( status != PSA_SUCCESS )
7843 {
7844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7845 return;
7846 }
7847
Andrzej Kurek972fba52019-01-30 03:29:12 -05007848 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007849 if( status != PSA_SUCCESS )
7850 {
7851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7852 return;
7853 }
7854 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7855#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007856 mbedtls_sha512_init( &sha512 );
7857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007859
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007860 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007861
7862 /*
7863 * TLSv1.2:
7864 * hash = PRF( master, finished_label,
7865 * Hash( handshake ) )[0.11]
7866 */
7867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007869 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7870 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007871#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007872
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007873 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007874 mbedtls_sha512_free( &sha512 );
7875#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007876
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007877 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007878 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007881
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007882 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007885}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886#endif /* MBEDTLS_SHA512_C */
7887#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007890{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007892
7893 /*
7894 * Free our handshake params
7895 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007896 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007898 ssl->handshake = NULL;
7899
7900 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007901 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007902 */
7903 if( ssl->transform )
7904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 mbedtls_ssl_transform_free( ssl->transform );
7906 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007907 }
7908 ssl->transform = ssl->transform_negotiate;
7909 ssl->transform_negotiate = NULL;
7910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007912}
7913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007915{
7916 int resume = ssl->handshake->resume;
7917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#if defined(MBEDTLS_SSL_RENEGOTIATION)
7921 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007924 ssl->renego_records_seen = 0;
7925 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007926#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007927
7928 /*
7929 * Free the previous session and switch in the current one
7930 */
Paul Bakker0a597072012-09-25 21:55:46 +00007931 if( ssl->session )
7932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007934 /* RFC 7366 3.1: keep the EtM state */
7935 ssl->session_negotiate->encrypt_then_mac =
7936 ssl->session->encrypt_then_mac;
7937#endif
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 mbedtls_ssl_session_free( ssl->session );
7940 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007941 }
7942 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007943 ssl->session_negotiate = NULL;
7944
Paul Bakker0a597072012-09-25 21:55:46 +00007945 /*
7946 * Add cache entry
7947 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007948 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007949 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007950 resume == 0 )
7951 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007952 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007954 }
Paul Bakker0a597072012-09-25 21:55:46 +00007955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007958 ssl->handshake->flight != NULL )
7959 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007960 /* Cancel handshake timer */
7961 ssl_set_timer( ssl, 0 );
7962
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007963 /* Keep last flight around in case we need to resend it:
7964 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007966 }
7967 else
7968#endif
7969 ssl_handshake_wrapup_free_hs_transform( ssl );
7970
Paul Bakker48916f92012-09-16 19:57:18 +00007971 ssl->state++;
7972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007974}
7975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007977{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007978 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007981
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007982 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007983
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007984 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007985
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007986 /*
7987 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7988 * may define some other value. Currently (early 2016), no defined
7989 * ciphersuite does this (and this is unlikely to change as activity has
7990 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007995 ssl->verify_data_len = hash_len;
7996 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007997#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007998
Paul Bakker5121ce52009-01-03 21:22:43 +00007999 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8001 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008002
8003 /*
8004 * In case of session resuming, invert the client and server
8005 * ChangeCipherSpec messages order.
8006 */
Paul Bakker0a597072012-09-25 21:55:46 +00008007 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008010 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008012#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008014 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008016#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008017 }
8018 else
8019 ssl->state++;
8020
Paul Bakker48916f92012-09-16 19:57:18 +00008021 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008022 * Switch to our negotiated transform and session parameters for outbound
8023 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008027#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008028 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008029 {
8030 unsigned char i;
8031
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008032 /* Remember current epoch settings for resending */
8033 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01008034 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008035
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008036 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008037 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008038
8039 /* Increment epoch */
8040 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008041 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008042 break;
8043
8044 /* The loop goes to its end iff the counter is wrapping */
8045 if( i == 0 )
8046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8048 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008049 }
8050 }
8051 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008053 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008054
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008055 ssl->transform_out = ssl->transform_negotiate;
8056 ssl->session_out = ssl->session_negotiate;
8057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8059 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8064 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008065 }
8066 }
8067#endif
8068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008069#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008070 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008072#endif
8073
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008074 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008075 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008077 return( ret );
8078 }
8079
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008080#if defined(MBEDTLS_SSL_PROTO_DTLS)
8081 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8082 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8083 {
8084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8085 return( ret );
8086 }
8087#endif
8088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008090
8091 return( 0 );
8092}
8093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008095#define SSL_MAX_HASH_LEN 36
8096#else
8097#define SSL_MAX_HASH_LEN 12
8098#endif
8099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008101{
Paul Bakker23986e52011-04-24 08:57:21 +00008102 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008103 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008104 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008107
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008108 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008109
Hanno Becker327c93b2018-08-15 13:56:18 +01008110 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008113 return( ret );
8114 }
8115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008119 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8120 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008122 }
8123
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008124 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125#if defined(MBEDTLS_SSL_PROTO_SSL3)
8126 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008127 hash_len = 36;
8128 else
8129#endif
8130 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8133 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008136 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8137 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008139 }
8140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008142 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008145 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8146 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008148 }
8149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008151 ssl->verify_data_len = hash_len;
8152 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008153#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008154
Paul Bakker0a597072012-09-25 21:55:46 +00008155 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008157#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008158 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008162 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008164#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008165 }
8166 else
8167 ssl->state++;
8168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008170 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008172#endif
8173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008175
8176 return( 0 );
8177}
8178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008180{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8184 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8185 mbedtls_md5_init( &handshake->fin_md5 );
8186 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008187 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8188 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008189#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8191#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008192#if defined(MBEDTLS_USE_PSA_CRYPTO)
8193 handshake->fin_sha256_psa = psa_hash_operation_init();
8194 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8195#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008197 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008198#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008201#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008202 handshake->fin_sha384_psa = psa_hash_operation_init();
8203 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008204#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008206 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008207#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008208#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008210
8211 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008212
8213#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8214 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8215 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8216#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218#if defined(MBEDTLS_DHM_C)
8219 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008220#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221#if defined(MBEDTLS_ECDH_C)
8222 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008223#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008224#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008225 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008226#if defined(MBEDTLS_SSL_CLI_C)
8227 handshake->ecjpake_cache = NULL;
8228 handshake->ecjpake_cache_len = 0;
8229#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008230#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008231
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008232#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008233 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008234#endif
8235
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008236#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8237 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8238#endif
Hanno Becker75173122019-02-06 16:18:31 +00008239
8240#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8241 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8242 mbedtls_pk_init( &handshake->peer_pubkey );
8243#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008244}
8245
Hanno Beckera18d1322018-01-03 14:27:32 +00008246void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008247{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8251 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008252
Hanno Beckerd56ed242018-01-03 15:32:51 +00008253#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008254 mbedtls_md_init( &transform->md_ctx_enc );
8255 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008256#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008257}
8258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008262}
8263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008265{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008266 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008267 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008268 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008269 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008270 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008271 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008272 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008273
8274 /*
8275 * Either the pointers are now NULL or cleared properly and can be freed.
8276 * Now allocate missing structures.
8277 */
8278 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008279 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008280 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008281 }
Paul Bakker48916f92012-09-16 19:57:18 +00008282
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008283 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008284 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008285 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008286 }
Paul Bakker48916f92012-09-16 19:57:18 +00008287
Paul Bakker82788fb2014-10-20 13:59:19 +02008288 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008289 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008290 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008291 }
Paul Bakker48916f92012-09-16 19:57:18 +00008292
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008293 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008294 if( ssl->handshake == NULL ||
8295 ssl->transform_negotiate == NULL ||
8296 ssl->session_negotiate == NULL )
8297 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300 mbedtls_free( ssl->handshake );
8301 mbedtls_free( ssl->transform_negotiate );
8302 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008303
8304 ssl->handshake = NULL;
8305 ssl->transform_negotiate = NULL;
8306 ssl->session_negotiate = NULL;
8307
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008308 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008309 }
8310
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008311 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008312 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008313 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008314 ssl_handshake_params_init( ssl->handshake );
8315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008317 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8318 {
8319 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008320
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008321 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8322 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8323 else
8324 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008325
8326 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008327 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008328#endif
8329
Paul Bakker48916f92012-09-16 19:57:18 +00008330 return( 0 );
8331}
8332
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008333#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008334/* Dummy cookie callbacks for defaults */
8335static int ssl_cookie_write_dummy( void *ctx,
8336 unsigned char **p, unsigned char *end,
8337 const unsigned char *cli_id, size_t cli_id_len )
8338{
8339 ((void) ctx);
8340 ((void) p);
8341 ((void) end);
8342 ((void) cli_id);
8343 ((void) cli_id_len);
8344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008346}
8347
8348static int ssl_cookie_check_dummy( void *ctx,
8349 const unsigned char *cookie, size_t cookie_len,
8350 const unsigned char *cli_id, size_t cli_id_len )
8351{
8352 ((void) ctx);
8353 ((void) cookie);
8354 ((void) cookie_len);
8355 ((void) cli_id);
8356 ((void) cli_id_len);
8357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008359}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008360#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008361
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008362/* Once ssl->out_hdr as the address of the beginning of the
8363 * next outgoing record is set, deduce the other pointers.
8364 *
8365 * Note: For TLS, we save the implicit record sequence number
8366 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8367 * and the caller has to make sure there's space for this.
8368 */
8369
8370static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8371 mbedtls_ssl_transform *transform )
8372{
8373#if defined(MBEDTLS_SSL_PROTO_DTLS)
8374 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8375 {
8376 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008377#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008378 ssl->out_cid = ssl->out_ctr + 8;
8379 ssl->out_len = ssl->out_cid;
8380 if( transform != NULL )
8381 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008382#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008383 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008384#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008385 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008386 }
8387 else
8388#endif
8389 {
8390 ssl->out_ctr = ssl->out_hdr - 8;
8391 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008392#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008393 ssl->out_cid = ssl->out_len;
8394#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008395 ssl->out_iv = ssl->out_hdr + 5;
8396 }
8397
8398 /* Adjust out_msg to make space for explicit IV, if used. */
8399 if( transform != NULL &&
8400 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8401 {
8402 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8403 }
8404 else
8405 ssl->out_msg = ssl->out_iv;
8406}
8407
8408/* Once ssl->in_hdr as the address of the beginning of the
8409 * next incoming record is set, deduce the other pointers.
8410 *
8411 * Note: For TLS, we save the implicit record sequence number
8412 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8413 * and the caller has to make sure there's space for this.
8414 */
8415
Hanno Becker79594fd2019-05-08 09:38:41 +01008416static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008417{
Hanno Becker79594fd2019-05-08 09:38:41 +01008418 /* This function sets the pointers to match the case
8419 * of unprotected TLS/DTLS records, with both ssl->in_iv
8420 * and ssl->in_msg pointing to the beginning of the record
8421 * content.
8422 *
8423 * When decrypting a protected record, ssl->in_msg
8424 * will be shifted to point to the beginning of the
8425 * record plaintext.
8426 */
8427
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008428#if defined(MBEDTLS_SSL_PROTO_DTLS)
8429 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8430 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008431 /* This sets the header pointers to match records
8432 * without CID. When we receive a record containing
8433 * a CID, the fields are shifted accordingly in
8434 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008435 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008436#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008437 ssl->in_cid = ssl->in_ctr + 8;
8438 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008439#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008440 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008441#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008442 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008443 }
8444 else
8445#endif
8446 {
8447 ssl->in_ctr = ssl->in_hdr - 8;
8448 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008449#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008450 ssl->in_cid = ssl->in_len;
8451#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008452 ssl->in_iv = ssl->in_hdr + 5;
8453 }
8454
Hanno Becker79594fd2019-05-08 09:38:41 +01008455 /* This will be adjusted at record decryption time. */
8456 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008457}
8458
Paul Bakker5121ce52009-01-03 21:22:43 +00008459/*
8460 * Initialize an SSL context
8461 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008462void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8463{
8464 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8465}
8466
8467/*
8468 * Setup an SSL context
8469 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008470
8471static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8472{
8473 /* Set the incoming and outgoing record pointers. */
8474#if defined(MBEDTLS_SSL_PROTO_DTLS)
8475 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8476 {
8477 ssl->out_hdr = ssl->out_buf;
8478 ssl->in_hdr = ssl->in_buf;
8479 }
8480 else
8481#endif /* MBEDTLS_SSL_PROTO_DTLS */
8482 {
8483 ssl->out_hdr = ssl->out_buf + 8;
8484 ssl->in_hdr = ssl->in_buf + 8;
8485 }
8486
8487 /* Derive other internal pointers. */
8488 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008489 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008490}
8491
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008492int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008493 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008494{
Paul Bakker48916f92012-09-16 19:57:18 +00008495 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008496
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008497 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008498
8499 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008500 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008501 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008502
8503 /* Set to NULL in case of an error condition */
8504 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008505
Angus Grattond8213d02016-05-25 20:56:48 +10008506 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8507 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008508 {
Angus Grattond8213d02016-05-25 20:56:48 +10008509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008510 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008511 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008512 }
8513
8514 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8515 if( ssl->out_buf == NULL )
8516 {
8517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008518 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008519 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008520 }
8521
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008522 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008523
Paul Bakker48916f92012-09-16 19:57:18 +00008524 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008525 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008526
8527 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008528
8529error:
8530 mbedtls_free( ssl->in_buf );
8531 mbedtls_free( ssl->out_buf );
8532
8533 ssl->conf = NULL;
8534
8535 ssl->in_buf = NULL;
8536 ssl->out_buf = NULL;
8537
8538 ssl->in_hdr = NULL;
8539 ssl->in_ctr = NULL;
8540 ssl->in_len = NULL;
8541 ssl->in_iv = NULL;
8542 ssl->in_msg = NULL;
8543
8544 ssl->out_hdr = NULL;
8545 ssl->out_ctr = NULL;
8546 ssl->out_len = NULL;
8547 ssl->out_iv = NULL;
8548 ssl->out_msg = NULL;
8549
k-stachowiak9f7798e2018-07-31 16:52:32 +02008550 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008551}
8552
8553/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008554 * Reset an initialized and used SSL context for re-use while retaining
8555 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008556 *
8557 * If partial is non-zero, keep data in the input buffer and client ID.
8558 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008559 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008560static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008561{
Paul Bakker48916f92012-09-16 19:57:18 +00008562 int ret;
8563
Hanno Becker7e772132018-08-10 12:38:21 +01008564#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8565 !defined(MBEDTLS_SSL_SRV_C)
8566 ((void) partial);
8567#endif
8568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008569 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008570
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008571 /* Cancel any possibly running timer */
8572 ssl_set_timer( ssl, 0 );
8573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008574#if defined(MBEDTLS_SSL_RENEGOTIATION)
8575 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008576 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008577
8578 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8580 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008581#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008583
Paul Bakker7eb013f2011-10-06 12:37:39 +00008584 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008585 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008586
8587 ssl->in_msgtype = 0;
8588 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008590 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008591 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008592#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008594 ssl_dtls_replay_reset( ssl );
8595#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008596
8597 ssl->in_hslen = 0;
8598 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008599
8600 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008601
8602 ssl->out_msgtype = 0;
8603 ssl->out_msglen = 0;
8604 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8606 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008607 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008608#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008609
Hanno Becker19859472018-08-06 09:40:20 +01008610 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8611
Paul Bakker48916f92012-09-16 19:57:18 +00008612 ssl->transform_in = NULL;
8613 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008614
Hanno Becker78640902018-08-13 16:35:15 +01008615 ssl->session_in = NULL;
8616 ssl->session_out = NULL;
8617
Angus Grattond8213d02016-05-25 20:56:48 +10008618 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008619
8620#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008621 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008622#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8623 {
8624 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008625 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008626 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8629 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8632 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8635 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008636 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008637 }
8638#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008639
Paul Bakker48916f92012-09-16 19:57:18 +00008640 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642 mbedtls_ssl_transform_free( ssl->transform );
8643 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008644 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008645 }
Paul Bakker48916f92012-09-16 19:57:18 +00008646
Paul Bakkerc0463502013-02-14 11:19:38 +01008647 if( ssl->session )
8648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008649 mbedtls_ssl_session_free( ssl->session );
8650 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008651 ssl->session = NULL;
8652 }
8653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008655 ssl->alpn_chosen = NULL;
8656#endif
8657
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008658#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008659#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008660 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008661#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008662 {
8663 mbedtls_free( ssl->cli_id );
8664 ssl->cli_id = NULL;
8665 ssl->cli_id_len = 0;
8666 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008667#endif
8668
Paul Bakker48916f92012-09-16 19:57:18 +00008669 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8670 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008671
8672 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008673}
8674
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008675/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008676 * Reset an initialized and used SSL context for re-use while retaining
8677 * all application-set variables, function pointers and data.
8678 */
8679int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8680{
8681 return( ssl_session_reset_int( ssl, 0 ) );
8682}
8683
8684/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008685 * SSL set accessors
8686 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008687void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008688{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008689 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008690}
8691
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008692void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008693{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008694 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008695}
8696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008697#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008698void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008699{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008700 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008701}
8702#endif
8703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008705void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008706{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008707 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008708}
8709#endif
8710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008712
Hanno Becker1841b0a2018-08-24 11:13:57 +01008713void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8714 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008715{
8716 ssl->disable_datagram_packing = !allow_packing;
8717}
8718
8719void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8720 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008721{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008722 conf->hs_timeout_min = min;
8723 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008724}
8725#endif
8726
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008727void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008728{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008729 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008730}
8731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008733void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008734 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008735 void *p_vrfy )
8736{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008737 conf->f_vrfy = f_vrfy;
8738 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008739}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008741
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008742void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008743 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008744 void *p_rng )
8745{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008746 conf->f_rng = f_rng;
8747 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008748}
8749
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008750void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008751 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008752 void *p_dbg )
8753{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008754 conf->f_dbg = f_dbg;
8755 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008756}
8757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008759 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008760 mbedtls_ssl_send_t *f_send,
8761 mbedtls_ssl_recv_t *f_recv,
8762 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008763{
8764 ssl->p_bio = p_bio;
8765 ssl->f_send = f_send;
8766 ssl->f_recv = f_recv;
8767 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008768}
8769
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008770#if defined(MBEDTLS_SSL_PROTO_DTLS)
8771void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8772{
8773 ssl->mtu = mtu;
8774}
8775#endif
8776
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008777void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008778{
8779 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008780}
8781
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008782void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8783 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008784 mbedtls_ssl_set_timer_t *f_set_timer,
8785 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008786{
8787 ssl->p_timer = p_timer;
8788 ssl->f_set_timer = f_set_timer;
8789 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008790
8791 /* Make sure we start with no timer running */
8792 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008793}
8794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008796void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008797 void *p_cache,
8798 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8799 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008800{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008801 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008802 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008803 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008804}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807#if defined(MBEDTLS_SSL_CLI_C)
8808int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008809{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008810 int ret;
8811
8812 if( ssl == NULL ||
8813 session == NULL ||
8814 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008815 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008818 }
8819
Hanno Becker52055ae2019-02-06 14:30:46 +00008820 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8821 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008822 return( ret );
8823
Paul Bakker0a597072012-09-25 21:55:46 +00008824 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008825
8826 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008827}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008829
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008830void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008831 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008832{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008833 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8834 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8835 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8836 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008837}
8838
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008839void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008840 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008841 int major, int minor )
8842{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008844 return;
8845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008846 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008847 return;
8848
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008849 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008850}
8851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008852#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008853void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008854 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008855{
8856 conf->cert_profile = profile;
8857}
8858
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008859/* Append a new keycert entry to a (possibly empty) list */
8860static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8861 mbedtls_x509_crt *cert,
8862 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008863{
niisato8ee24222018-06-25 19:05:48 +09008864 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008865
niisato8ee24222018-06-25 19:05:48 +09008866 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8867 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008868 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008869
niisato8ee24222018-06-25 19:05:48 +09008870 new_cert->cert = cert;
8871 new_cert->key = key;
8872 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008873
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008874 /* Update head is the list was null, else add to the end */
8875 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008876 {
niisato8ee24222018-06-25 19:05:48 +09008877 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008878 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008879 else
8880 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008881 mbedtls_ssl_key_cert *cur = *head;
8882 while( cur->next != NULL )
8883 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008884 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008885 }
8886
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008887 return( 0 );
8888}
8889
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008890int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008891 mbedtls_x509_crt *own_cert,
8892 mbedtls_pk_context *pk_key )
8893{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008894 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008895}
8896
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008897void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008898 mbedtls_x509_crt *ca_chain,
8899 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008900{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008901 conf->ca_chain = ca_chain;
8902 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008903
8904#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8905 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8906 * cannot be used together. */
8907 conf->f_ca_cb = NULL;
8908 conf->p_ca_cb = NULL;
8909#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008910}
Hanno Becker5adaad92019-03-27 16:54:37 +00008911
8912#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8913void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008914 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008915 void *p_ca_cb )
8916{
8917 conf->f_ca_cb = f_ca_cb;
8918 conf->p_ca_cb = p_ca_cb;
8919
8920 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8921 * cannot be used together. */
8922 conf->ca_chain = NULL;
8923 conf->ca_crl = NULL;
8924}
8925#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008926#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008927
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008928#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8929int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8930 mbedtls_x509_crt *own_cert,
8931 mbedtls_pk_context *pk_key )
8932{
8933 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8934 own_cert, pk_key ) );
8935}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008936
8937void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8938 mbedtls_x509_crt *ca_chain,
8939 mbedtls_x509_crl *ca_crl )
8940{
8941 ssl->handshake->sni_ca_chain = ca_chain;
8942 ssl->handshake->sni_ca_crl = ca_crl;
8943}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008944
8945void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8946 int authmode )
8947{
8948 ssl->handshake->sni_authmode = authmode;
8949}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008950#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8951
Hanno Becker8927c832019-04-03 12:52:50 +01008952#if defined(MBEDTLS_X509_CRT_PARSE_C)
8953void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8954 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8955 void *p_vrfy )
8956{
8957 ssl->f_vrfy = f_vrfy;
8958 ssl->p_vrfy = p_vrfy;
8959}
8960#endif
8961
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008962#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008963/*
8964 * Set EC J-PAKE password for current handshake
8965 */
8966int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8967 const unsigned char *pw,
8968 size_t pw_len )
8969{
8970 mbedtls_ecjpake_role role;
8971
Janos Follath8eb64132016-06-03 15:40:57 +01008972 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8974
8975 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8976 role = MBEDTLS_ECJPAKE_SERVER;
8977 else
8978 role = MBEDTLS_ECJPAKE_CLIENT;
8979
8980 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8981 role,
8982 MBEDTLS_MD_SHA256,
8983 MBEDTLS_ECP_DP_SECP256R1,
8984 pw, pw_len ) );
8985}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008986#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008988#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008989
8990static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8991{
8992 /* Remove reference to existing PSK, if any. */
8993#if defined(MBEDTLS_USE_PSA_CRYPTO)
8994 if( conf->psk_opaque != 0 )
8995 {
8996 /* The maintenance of the PSK key slot is the
8997 * user's responsibility. */
8998 conf->psk_opaque = 0;
8999 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00009000 /* This and the following branch should never
9001 * be taken simultaenously as we maintain the
9002 * invariant that raw and opaque PSKs are never
9003 * configured simultaneously. As a safeguard,
9004 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009005#endif /* MBEDTLS_USE_PSA_CRYPTO */
9006 if( conf->psk != NULL )
9007 {
9008 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
9009
9010 mbedtls_free( conf->psk );
9011 conf->psk = NULL;
9012 conf->psk_len = 0;
9013 }
9014
9015 /* Remove reference to PSK identity, if any. */
9016 if( conf->psk_identity != NULL )
9017 {
9018 mbedtls_free( conf->psk_identity );
9019 conf->psk_identity = NULL;
9020 conf->psk_identity_len = 0;
9021 }
9022}
9023
Hanno Becker7390c712018-11-15 13:33:04 +00009024/* This function assumes that PSK identity in the SSL config is unset.
9025 * It checks that the provided identity is well-formed and attempts
9026 * to make a copy of it in the SSL config.
9027 * On failure, the PSK identity in the config remains unset. */
9028static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
9029 unsigned char const *psk_identity,
9030 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009031{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009032 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00009033 if( psk_identity == NULL ||
9034 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009035 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009036 {
9037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9038 }
9039
Hanno Becker7390c712018-11-15 13:33:04 +00009040 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
9041 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009042 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02009043
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009044 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009045 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02009046
9047 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02009048}
9049
Hanno Becker7390c712018-11-15 13:33:04 +00009050int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9051 const unsigned char *psk, size_t psk_len,
9052 const unsigned char *psk_identity, size_t psk_identity_len )
9053{
9054 int ret;
9055 /* Remove opaque/raw PSK + PSK Identity */
9056 ssl_conf_remove_psk( conf );
9057
9058 /* Check and set raw PSK */
9059 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9061 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9062 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9063 conf->psk_len = psk_len;
9064 memcpy( conf->psk, psk, conf->psk_len );
9065
9066 /* Check and set PSK Identity */
9067 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9068 if( ret != 0 )
9069 ssl_conf_remove_psk( conf );
9070
9071 return( ret );
9072}
9073
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009074static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9075{
9076#if defined(MBEDTLS_USE_PSA_CRYPTO)
9077 if( ssl->handshake->psk_opaque != 0 )
9078 {
9079 ssl->handshake->psk_opaque = 0;
9080 }
9081 else
9082#endif /* MBEDTLS_USE_PSA_CRYPTO */
9083 if( ssl->handshake->psk != NULL )
9084 {
9085 mbedtls_platform_zeroize( ssl->handshake->psk,
9086 ssl->handshake->psk_len );
9087 mbedtls_free( ssl->handshake->psk );
9088 ssl->handshake->psk_len = 0;
9089 }
9090}
9091
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009092int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9093 const unsigned char *psk, size_t psk_len )
9094{
9095 if( psk == NULL || ssl->handshake == NULL )
9096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9097
9098 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9100
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009101 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009102
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009103 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009104 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009105
9106 ssl->handshake->psk_len = psk_len;
9107 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9108
9109 return( 0 );
9110}
9111
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009112#if defined(MBEDTLS_USE_PSA_CRYPTO)
9113int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009114 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009115 const unsigned char *psk_identity,
9116 size_t psk_identity_len )
9117{
Hanno Becker7390c712018-11-15 13:33:04 +00009118 int ret;
9119 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009120 ssl_conf_remove_psk( conf );
9121
Hanno Becker7390c712018-11-15 13:33:04 +00009122 /* Check and set opaque PSK */
9123 if( psk_slot == 0 )
9124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009125 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009126
9127 /* Check and set PSK Identity */
9128 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9129 psk_identity_len );
9130 if( ret != 0 )
9131 ssl_conf_remove_psk( conf );
9132
9133 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009134}
9135
9136int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009137 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009138{
9139 if( psk_slot == 0 || ssl->handshake == NULL )
9140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9141
9142 ssl_remove_psk( ssl );
9143 ssl->handshake->psk_opaque = psk_slot;
9144 return( 0 );
9145}
9146#endif /* MBEDTLS_USE_PSA_CRYPTO */
9147
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009148void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009150 size_t),
9151 void *p_psk )
9152{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009153 conf->f_psk = f_psk;
9154 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009157
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009158#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009159
9160#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009161int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009162{
9163 int ret;
9164
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009165 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9166 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9167 {
9168 mbedtls_mpi_free( &conf->dhm_P );
9169 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009170 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009172
9173 return( 0 );
9174}
Hanno Becker470a8c42017-10-04 15:28:46 +01009175#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009176
Hanno Beckera90658f2017-10-04 15:29:08 +01009177int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9178 const unsigned char *dhm_P, size_t P_len,
9179 const unsigned char *dhm_G, size_t G_len )
9180{
9181 int ret;
9182
9183 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9184 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9185 {
9186 mbedtls_mpi_free( &conf->dhm_P );
9187 mbedtls_mpi_free( &conf->dhm_G );
9188 return( ret );
9189 }
9190
9191 return( 0 );
9192}
Paul Bakker5121ce52009-01-03 21:22:43 +00009193
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009194int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009195{
9196 int ret;
9197
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009198 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9199 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9200 {
9201 mbedtls_mpi_free( &conf->dhm_P );
9202 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009203 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009204 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009205
9206 return( 0 );
9207}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009208#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009209
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009210#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9211/*
9212 * Set the minimum length for Diffie-Hellman parameters
9213 */
9214void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9215 unsigned int bitlen )
9216{
9217 conf->dhm_min_bitlen = bitlen;
9218}
9219#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9220
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009221#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009222/*
9223 * Set allowed/preferred hashes for handshake signatures
9224 */
9225void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9226 const int *hashes )
9227{
9228 conf->sig_hashes = hashes;
9229}
Hanno Becker947194e2017-04-07 13:25:49 +01009230#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009231
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009232#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009233/*
9234 * Set the allowed elliptic curves
9235 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009236void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009237 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009238{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009239 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009240}
Hanno Becker947194e2017-04-07 13:25:49 +01009241#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009242
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009243#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009244int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009245{
Hanno Becker947194e2017-04-07 13:25:49 +01009246 /* Initialize to suppress unnecessary compiler warning */
9247 size_t hostname_len = 0;
9248
9249 /* Check if new hostname is valid before
9250 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009251 if( hostname != NULL )
9252 {
9253 hostname_len = strlen( hostname );
9254
9255 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9257 }
9258
9259 /* Now it's clear that we will overwrite the old hostname,
9260 * so we can free it safely */
9261
9262 if( ssl->hostname != NULL )
9263 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009264 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009265 mbedtls_free( ssl->hostname );
9266 }
9267
9268 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009269
Paul Bakker5121ce52009-01-03 21:22:43 +00009270 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009271 {
9272 ssl->hostname = NULL;
9273 }
9274 else
9275 {
9276 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009277 if( ssl->hostname == NULL )
9278 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009279
Hanno Becker947194e2017-04-07 13:25:49 +01009280 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009281
Hanno Becker947194e2017-04-07 13:25:49 +01009282 ssl->hostname[hostname_len] = '\0';
9283 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009284
9285 return( 0 );
9286}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009287#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009288
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009289#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009290void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009292 const unsigned char *, size_t),
9293 void *p_sni )
9294{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009295 conf->f_sni = f_sni;
9296 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009301int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009302{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009303 size_t cur_len, tot_len;
9304 const char **p;
9305
9306 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009307 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9308 * MUST NOT be truncated."
9309 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009310 */
9311 tot_len = 0;
9312 for( p = protos; *p != NULL; p++ )
9313 {
9314 cur_len = strlen( *p );
9315 tot_len += cur_len;
9316
9317 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009319 }
9320
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009321 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009322
9323 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009324}
9325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009327{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009328 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009329}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009331
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009332void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009333{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009334 conf->max_major_ver = major;
9335 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009336}
9337
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009338void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009339{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009340 conf->min_major_ver = major;
9341 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009342}
9343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009345void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009346{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009347 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009348}
9349#endif
9350
Janos Follath088ce432017-04-10 12:42:31 +01009351#if defined(MBEDTLS_SSL_SRV_C)
9352void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9353 char cert_req_ca_list )
9354{
9355 conf->cert_req_ca_list = cert_req_ca_list;
9356}
9357#endif
9358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009360void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009361{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009362 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009363}
9364#endif
9365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009367void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009368{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009369 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009370}
9371#endif
9372
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009373#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009374void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009375{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009376 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009377}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009378#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009381int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009382{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009384 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009387 }
9388
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009389 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009390
9391 return( 0 );
9392}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009393#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009396void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009397{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009398 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009403void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009404{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009405 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009406}
9407#endif
9408
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009409void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009410{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009411 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009412}
9413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009415void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009416{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009417 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009418}
9419
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009420void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009421{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009422 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009423}
9424
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009425void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009426 const unsigned char period[8] )
9427{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009428 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009432#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009433#if defined(MBEDTLS_SSL_CLI_C)
9434void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009435{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009436 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009437}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009438#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009439
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009440#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009441void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9442 mbedtls_ssl_ticket_write_t *f_ticket_write,
9443 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9444 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009445{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009446 conf->f_ticket_write = f_ticket_write;
9447 conf->f_ticket_parse = f_ticket_parse;
9448 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009449}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009450#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009452
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009453#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9454void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9455 mbedtls_ssl_export_keys_t *f_export_keys,
9456 void *p_export_keys )
9457{
9458 conf->f_export_keys = f_export_keys;
9459 conf->p_export_keys = p_export_keys;
9460}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009461
9462void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9463 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9464 void *p_export_keys )
9465{
9466 conf->f_export_keys_ext = f_export_keys_ext;
9467 conf->p_export_keys = p_export_keys;
9468}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009469#endif
9470
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009471#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009472void mbedtls_ssl_conf_async_private_cb(
9473 mbedtls_ssl_config *conf,
9474 mbedtls_ssl_async_sign_t *f_async_sign,
9475 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9476 mbedtls_ssl_async_resume_t *f_async_resume,
9477 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009478 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009479{
9480 conf->f_async_sign_start = f_async_sign;
9481 conf->f_async_decrypt_start = f_async_decrypt;
9482 conf->f_async_resume = f_async_resume;
9483 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009484 conf->p_async_config_data = async_config_data;
9485}
9486
Gilles Peskine8f97af72018-04-26 11:46:10 +02009487void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9488{
9489 return( conf->p_async_config_data );
9490}
9491
Gilles Peskine1febfef2018-04-30 11:54:39 +02009492void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009493{
9494 if( ssl->handshake == NULL )
9495 return( NULL );
9496 else
9497 return( ssl->handshake->user_async_ctx );
9498}
9499
Gilles Peskine1febfef2018-04-30 11:54:39 +02009500void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009501 void *ctx )
9502{
9503 if( ssl->handshake != NULL )
9504 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009505}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009506#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009507
Paul Bakker5121ce52009-01-03 21:22:43 +00009508/*
9509 * SSL get accessors
9510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009512{
9513 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9514}
9515
Hanno Becker8b170a02017-10-10 11:51:19 +01009516int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9517{
9518 /*
9519 * Case A: We're currently holding back
9520 * a message for further processing.
9521 */
9522
9523 if( ssl->keep_current_message == 1 )
9524 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009525 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009526 return( 1 );
9527 }
9528
9529 /*
9530 * Case B: Further records are pending in the current datagram.
9531 */
9532
9533#if defined(MBEDTLS_SSL_PROTO_DTLS)
9534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9535 ssl->in_left > ssl->next_record_offset )
9536 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009537 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009538 return( 1 );
9539 }
9540#endif /* MBEDTLS_SSL_PROTO_DTLS */
9541
9542 /*
9543 * Case C: A handshake message is being processed.
9544 */
9545
Hanno Becker8b170a02017-10-10 11:51:19 +01009546 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9547 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009549 return( 1 );
9550 }
9551
9552 /*
9553 * Case D: An application data message is being processed
9554 */
9555 if( ssl->in_offt != NULL )
9556 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009558 return( 1 );
9559 }
9560
9561 /*
9562 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009563 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009564 * we implement support for multiple alerts in single records.
9565 */
9566
9567 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9568 return( 0 );
9569}
9570
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009571uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009572{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009573 if( ssl->session != NULL )
9574 return( ssl->session->verify_result );
9575
9576 if( ssl->session_negotiate != NULL )
9577 return( ssl->session_negotiate->verify_result );
9578
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009579 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009580}
9581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009583{
Paul Bakker926c8e42013-03-06 10:23:34 +01009584 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009585 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009588}
9589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009591{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009593 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009594 {
9595 switch( ssl->minor_ver )
9596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009598 return( "DTLSv1.0" );
9599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009601 return( "DTLSv1.2" );
9602
9603 default:
9604 return( "unknown (DTLS)" );
9605 }
9606 }
9607#endif
9608
Paul Bakker43ca69c2011-01-15 17:35:19 +00009609 switch( ssl->minor_ver )
9610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009611 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009612 return( "SSLv3.0" );
9613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009615 return( "TLSv1.0" );
9616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009618 return( "TLSv1.1" );
9619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009621 return( "TLSv1.2" );
9622
Paul Bakker43ca69c2011-01-15 17:35:19 +00009623 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009624 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009625 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009626}
9627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009629{
Hanno Becker3136ede2018-08-17 15:28:19 +01009630 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009632 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009633
Hanno Becker5903de42019-05-03 14:46:38 +01009634 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9635
Hanno Becker78640902018-08-13 16:35:15 +01009636 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009637 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639#if defined(MBEDTLS_ZLIB_SUPPORT)
9640 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9641 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009642#endif
9643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646 case MBEDTLS_MODE_GCM:
9647 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009648 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009650 transform_expansion = transform->minlen;
9651 break;
9652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009653 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009654
9655 block_size = mbedtls_cipher_get_block_size(
9656 &transform->cipher_ctx_enc );
9657
Hanno Becker3136ede2018-08-17 15:28:19 +01009658 /* Expansion due to the addition of the MAC. */
9659 transform_expansion += transform->maclen;
9660
9661 /* Expansion due to the addition of CBC padding;
9662 * Theoretically up to 256 bytes, but we never use
9663 * more than the block size of the underlying cipher. */
9664 transform_expansion += block_size;
9665
9666 /* For TLS 1.1 or higher, an explicit IV is added
9667 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009668#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9669 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009670 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009671#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009672
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009673 break;
9674
9675 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009678 }
9679
Hanno Beckera0e20d02019-05-15 14:03:01 +01009680#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009681 if( transform->out_cid_len != 0 )
9682 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009683#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009684
Hanno Becker5903de42019-05-03 14:46:38 +01009685 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009686}
9687
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009688#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9689size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9690{
9691 size_t max_len;
9692
9693 /*
9694 * Assume mfl_code is correct since it was checked when set
9695 */
Angus Grattond8213d02016-05-25 20:56:48 +10009696 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009697
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009698 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009699 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009700 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009701 {
Angus Grattond8213d02016-05-25 20:56:48 +10009702 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009703 }
9704
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009705 /* During a handshake, use the value being negotiated */
9706 if( ssl->session_negotiate != NULL &&
9707 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9708 {
9709 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9710 }
9711
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009712 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009713}
9714#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9715
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009716#if defined(MBEDTLS_SSL_PROTO_DTLS)
9717static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9718{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009719 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9720 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9721 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9722 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9723 return ( 0 );
9724
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009725 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9726 return( ssl->mtu );
9727
9728 if( ssl->mtu == 0 )
9729 return( ssl->handshake->mtu );
9730
9731 return( ssl->mtu < ssl->handshake->mtu ?
9732 ssl->mtu : ssl->handshake->mtu );
9733}
9734#endif /* MBEDTLS_SSL_PROTO_DTLS */
9735
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009736int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9737{
9738 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9739
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009740#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9741 !defined(MBEDTLS_SSL_PROTO_DTLS)
9742 (void) ssl;
9743#endif
9744
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009745#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9746 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9747
9748 if( max_len > mfl )
9749 max_len = mfl;
9750#endif
9751
9752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009753 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009754 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009755 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009756 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9757 const size_t overhead = (size_t) ret;
9758
9759 if( ret < 0 )
9760 return( ret );
9761
9762 if( mtu <= overhead )
9763 {
9764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9765 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9766 }
9767
9768 if( max_len > mtu - overhead )
9769 max_len = mtu - overhead;
9770 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009771#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009772
Hanno Becker0defedb2018-08-10 12:35:02 +01009773#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9774 !defined(MBEDTLS_SSL_PROTO_DTLS)
9775 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009776#endif
9777
9778 return( (int) max_len );
9779}
9780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781#if defined(MBEDTLS_X509_CRT_PARSE_C)
9782const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009783{
9784 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009785 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009786
Hanno Beckere6824572019-02-07 13:18:46 +00009787#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009788 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009789#else
9790 return( NULL );
9791#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009792}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009796int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9797 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009798{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009799 if( ssl == NULL ||
9800 dst == NULL ||
9801 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009802 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009805 }
9806
Hanno Becker52055ae2019-02-06 14:30:46 +00009807 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009810
Paul Bakker5121ce52009-01-03 21:22:43 +00009811/*
Paul Bakker1961b702013-01-25 14:49:24 +01009812 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009815{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009817
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009818 if( ssl == NULL || ssl->conf == NULL )
9819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009822 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009824#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009826 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009828#endif
9829
Paul Bakker1961b702013-01-25 14:49:24 +01009830 return( ret );
9831}
9832
9833/*
9834 * Perform the SSL handshake
9835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009837{
9838 int ret = 0;
9839
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009840 if( ssl == NULL || ssl->conf == NULL )
9841 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009848
9849 if( ret != 0 )
9850 break;
9851 }
9852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009854
9855 return( ret );
9856}
9857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858#if defined(MBEDTLS_SSL_RENEGOTIATION)
9859#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009860/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009861 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009864{
9865 int ret;
9866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009868
9869 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009870 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9871 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009872
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009873 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009874 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009875 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009876 return( ret );
9877 }
9878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009880
9881 return( 0 );
9882}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009884
9885/*
9886 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887 * - any side: calling mbedtls_ssl_renegotiate(),
9888 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9889 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009890 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009891 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009893 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009895{
9896 int ret;
9897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009899
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009900 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9901 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009902
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009903 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9904 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009906 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009908 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009909 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009910 ssl->handshake->out_msg_seq = 1;
9911 else
9912 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009913 }
9914#endif
9915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9917 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009922 return( ret );
9923 }
9924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009926
9927 return( 0 );
9928}
9929
9930/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009931 * Renegotiate current connection on client,
9932 * or request renegotiation on server
9933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009935{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009936 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009937
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009938 if( ssl == NULL || ssl->conf == NULL )
9939 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009941#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009942 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009943 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9946 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009949
9950 /* Did we already try/start sending HelloRequest? */
9951 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009953
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009954 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009955 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009959 /*
9960 * On client, either start the renegotiation process or,
9961 * if already in progress, continue the handshake
9962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009967
9968 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009970 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009971 return( ret );
9972 }
9973 }
9974 else
9975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009979 return( ret );
9980 }
9981 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009983
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009984 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009985}
9986
9987/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009988 * Check record counters and renegotiate if they're above the limit.
9989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009991{
Andres AG2196c7f2016-12-15 17:01:16 +00009992 size_t ep_len = ssl_ep_len( ssl );
9993 int in_ctr_cmp;
9994 int out_ctr_cmp;
9995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9997 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009998 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009999 {
10000 return( 0 );
10001 }
10002
Andres AG2196c7f2016-12-15 17:01:16 +000010003 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10004 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010005 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010006 ssl->conf->renego_period + ep_len, 8 - ep_len );
10007
10008 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010009 {
10010 return( 0 );
10011 }
10012
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010014 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010015}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010016#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010017
10018/*
10019 * Receive application data decrypted from the SSL layer
10020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010021int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010022{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010023 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010024 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010025
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010026 if( ssl == NULL || ssl->conf == NULL )
10027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010031#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010032 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010035 return( ret );
10036
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010037 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010039 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010040 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010041 return( ret );
10042 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010043 }
10044#endif
10045
Hanno Becker4a810fb2017-05-24 16:27:30 +010010046 /*
10047 * Check if renegotiation is necessary and/or handshake is
10048 * in process. If yes, perform/continue, and fall through
10049 * if an unexpected packet is received while the client
10050 * is waiting for the ServerHello.
10051 *
10052 * (There is no equivalent to the last condition on
10053 * the server-side as it is not treated as within
10054 * a handshake while waiting for the ClientHello
10055 * after a renegotiation request.)
10056 */
10057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010058#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010059 ret = ssl_check_ctr_renegotiate( ssl );
10060 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10061 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010064 return( ret );
10065 }
10066#endif
10067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010068 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010071 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10072 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010075 return( ret );
10076 }
10077 }
10078
Hanno Beckere41158b2017-10-23 13:30:32 +010010079 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010080 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010081 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010082 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010083 if( ssl->f_get_timer != NULL &&
10084 ssl->f_get_timer( ssl->p_timer ) == -1 )
10085 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010086 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010087 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010088
Hanno Becker327c93b2018-08-15 13:56:18 +010010089 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010090 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010091 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10092 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010093
Hanno Becker4a810fb2017-05-24 16:27:30 +010010094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10095 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010096 }
10097
10098 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010100 {
10101 /*
10102 * OpenSSL sends empty messages to randomize the IV
10103 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010104 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010106 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010107 return( 0 );
10108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010110 return( ret );
10111 }
10112 }
10113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010114 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010117
Hanno Becker4a810fb2017-05-24 16:27:30 +010010118 /*
10119 * - For client-side, expect SERVER_HELLO_REQUEST.
10120 * - For server-side, expect CLIENT_HELLO.
10121 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10122 */
10123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010125 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010127 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010130
10131 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010132#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010133 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010134 {
10135 continue;
10136 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010137#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010138 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010139 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010140#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010141
Hanno Becker4a810fb2017-05-24 16:27:30 +010010142#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010143 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010147
10148 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010150 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010151 {
10152 continue;
10153 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010154#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010155 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010156 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010157#endif /* MBEDTLS_SSL_SRV_C */
10158
Hanno Becker21df7f92017-10-17 11:03:26 +010010159#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010160 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010161 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10162 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10163 ssl->conf->allow_legacy_renegotiation ==
10164 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10165 {
10166 /*
10167 * Accept renegotiation request
10168 */
Paul Bakker48916f92012-09-16 19:57:18 +000010169
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010170 /* DTLS clients need to know renego is server-initiated */
10171#if defined(MBEDTLS_SSL_PROTO_DTLS)
10172 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10173 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10174 {
10175 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10176 }
10177#endif
10178 ret = ssl_start_renegotiation( ssl );
10179 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10180 ret != 0 )
10181 {
10182 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10183 return( ret );
10184 }
10185 }
10186 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010187#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010188 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010189 /*
10190 * Refuse renegotiation
10191 */
10192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195#if defined(MBEDTLS_SSL_PROTO_SSL3)
10196 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010197 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010198 /* SSLv3 does not have a "no_renegotiation" warning, so
10199 we send a fatal alert and abort the connection. */
10200 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10201 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10202 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010203 }
10204 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10206#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10207 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10208 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10211 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10212 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010213 {
10214 return( ret );
10215 }
Paul Bakker48916f92012-09-16 19:57:18 +000010216 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010217 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10219 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10222 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010223 }
Paul Bakker48916f92012-09-16 19:57:18 +000010224 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010225
Hanno Becker90333da2017-10-10 11:27:13 +010010226 /* At this point, we don't know whether the renegotiation has been
10227 * completed or not. The cases to consider are the following:
10228 * 1) The renegotiation is complete. In this case, no new record
10229 * has been read yet.
10230 * 2) The renegotiation is incomplete because the client received
10231 * an application data record while awaiting the ServerHello.
10232 * 3) The renegotiation is incomplete because the client received
10233 * a non-handshake, non-application data message while awaiting
10234 * the ServerHello.
10235 * In each of these case, looping will be the proper action:
10236 * - For 1), the next iteration will read a new record and check
10237 * if it's application data.
10238 * - For 2), the loop condition isn't satisfied as application data
10239 * is present, hence continue is the same as break
10240 * - For 3), the loop condition is satisfied and read_record
10241 * will re-deliver the message that was held back by the client
10242 * when expecting the ServerHello.
10243 */
10244 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010245 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010246#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010248 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010249 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010250 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010251 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010254 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010256 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010257 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010258 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010259#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10262 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010265 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010266 }
10267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010268 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10271 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010272 }
10273
10274 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010275
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010276 /* We're going to return something now, cancel timer,
10277 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010278 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010279 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010280
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010281#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010282 /* If we requested renego but received AppData, resend HelloRequest.
10283 * Do it now, after setting in_offt, to avoid taking this branch
10284 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010285#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010286 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010287 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010288 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010289 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010291 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010292 return( ret );
10293 }
10294 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010296#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010297 }
10298
10299 n = ( len < ssl->in_msglen )
10300 ? len : ssl->in_msglen;
10301
10302 memcpy( buf, ssl->in_offt, n );
10303 ssl->in_msglen -= n;
10304
10305 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010306 {
10307 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010308 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010309 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010310 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010311 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010312 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010313 /* more data available */
10314 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010315 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010318
Paul Bakker23986e52011-04-24 08:57:21 +000010319 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010320}
10321
10322/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010323 * Send application data to be encrypted by the SSL layer, taking care of max
10324 * fragment length and buffer size.
10325 *
10326 * According to RFC 5246 Section 6.2.1:
10327 *
10328 * Zero-length fragments of Application data MAY be sent as they are
10329 * potentially useful as a traffic analysis countermeasure.
10330 *
10331 * Therefore, it is possible that the input message length is 0 and the
10332 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010333 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010334static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010335 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010336{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010337 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10338 const size_t max_len = (size_t) ret;
10339
10340 if( ret < 0 )
10341 {
10342 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10343 return( ret );
10344 }
10345
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010346 if( len > max_len )
10347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010348#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010349 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010352 "maximum fragment length: %d > %d",
10353 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010355 }
10356 else
10357#endif
10358 len = max_len;
10359 }
Paul Bakker887bd502011-06-08 13:10:54 +000010360
Paul Bakker5121ce52009-01-03 21:22:43 +000010361 if( ssl->out_left != 0 )
10362 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010363 /*
10364 * The user has previously tried to send the data and
10365 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10366 * written. In this case, we expect the high-level write function
10367 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010369 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010372 return( ret );
10373 }
10374 }
Paul Bakker887bd502011-06-08 13:10:54 +000010375 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010376 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010377 /*
10378 * The user is trying to send a message the first time, so we need to
10379 * copy the data into the internal buffers and setup the data structure
10380 * to keep track of partial writes
10381 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010382 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010383 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010384 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010385
Hanno Becker67bc7c32018-08-06 11:33:50 +010010386 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010389 return( ret );
10390 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010391 }
10392
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010393 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010394}
10395
10396/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010397 * Write application data, doing 1/n-1 splitting if necessary.
10398 *
10399 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010400 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010401 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010403#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010404static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010405 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010406{
10407 int ret;
10408
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010409 if( ssl->conf->cbc_record_splitting ==
10410 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010411 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10413 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10414 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010415 {
10416 return( ssl_write_real( ssl, buf, len ) );
10417 }
10418
10419 if( ssl->split_done == 0 )
10420 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010421 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010422 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010423 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010424 }
10425
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010426 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10427 return( ret );
10428 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010429
10430 return( ret + 1 );
10431}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010432#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010433
10434/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010435 * Write application data (public-facing wrapper)
10436 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010437int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010438{
10439 int ret;
10440
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010442
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010443 if( ssl == NULL || ssl->conf == NULL )
10444 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10445
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010446#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010447 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10448 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010449 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010450 return( ret );
10451 }
10452#endif
10453
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010454 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010455 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010456 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010457 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010459 return( ret );
10460 }
10461 }
10462
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010463#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010464 ret = ssl_write_split( ssl, buf, len );
10465#else
10466 ret = ssl_write_real( ssl, buf, len );
10467#endif
10468
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010470
10471 return( ret );
10472}
10473
10474/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010475 * Notify the peer that the connection is being closed
10476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010477int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010478{
10479 int ret;
10480
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010481 if( ssl == NULL || ssl->conf == NULL )
10482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010485
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010486 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010489 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010491 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10492 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10493 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010496 return( ret );
10497 }
10498 }
10499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010501
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010502 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010503}
10504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010505void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010506{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010507 if( transform == NULL )
10508 return;
10509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010510#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010511 deflateEnd( &transform->ctx_deflate );
10512 inflateEnd( &transform->ctx_inflate );
10513#endif
10514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010515 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10516 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010517
Hanno Beckerd56ed242018-01-03 15:32:51 +000010518#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010519 mbedtls_md_free( &transform->md_ctx_enc );
10520 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010521#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010522
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010523 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010524}
10525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010526#if defined(MBEDTLS_X509_CRT_PARSE_C)
10527static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010529 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010530
10531 while( cur != NULL )
10532 {
10533 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010534 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010535 cur = next;
10536 }
10537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010538#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010539
Hanno Becker0271f962018-08-16 13:23:47 +010010540#if defined(MBEDTLS_SSL_PROTO_DTLS)
10541
10542static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10543{
10544 unsigned offset;
10545 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10546
10547 if( hs == NULL )
10548 return;
10549
Hanno Becker283f5ef2018-08-24 09:34:47 +010010550 ssl_free_buffered_record( ssl );
10551
Hanno Becker0271f962018-08-16 13:23:47 +010010552 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010553 ssl_buffering_free_slot( ssl, offset );
10554}
10555
10556static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10557 uint8_t slot )
10558{
10559 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10560 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010561
10562 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10563 return;
10564
Hanno Beckere605b192018-08-21 15:59:07 +010010565 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010566 {
Hanno Beckere605b192018-08-21 15:59:07 +010010567 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010568 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010569 mbedtls_free( hs_buf->data );
10570 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010571 }
10572}
10573
10574#endif /* MBEDTLS_SSL_PROTO_DTLS */
10575
Gilles Peskine9b562d52018-04-25 20:32:43 +020010576void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010577{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010578 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10579
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010580 if( handshake == NULL )
10581 return;
10582
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010583#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10584 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10585 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010586 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010587 handshake->async_in_progress = 0;
10588 }
10589#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10590
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010591#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10592 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10593 mbedtls_md5_free( &handshake->fin_md5 );
10594 mbedtls_sha1_free( &handshake->fin_sha1 );
10595#endif
10596#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10597#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010598#if defined(MBEDTLS_USE_PSA_CRYPTO)
10599 psa_hash_abort( &handshake->fin_sha256_psa );
10600#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010601 mbedtls_sha256_free( &handshake->fin_sha256 );
10602#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010603#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010604#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010605#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010606 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010607#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010608 mbedtls_sha512_free( &handshake->fin_sha512 );
10609#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010610#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010611#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010613#if defined(MBEDTLS_DHM_C)
10614 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010615#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010616#if defined(MBEDTLS_ECDH_C)
10617 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010618#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010619#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010620 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010621#if defined(MBEDTLS_SSL_CLI_C)
10622 mbedtls_free( handshake->ecjpake_cache );
10623 handshake->ecjpake_cache = NULL;
10624 handshake->ecjpake_cache_len = 0;
10625#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010626#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010627
Janos Follath4ae5c292016-02-10 11:27:43 +000010628#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10629 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010630 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010632#endif
10633
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010634#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10635 if( handshake->psk != NULL )
10636 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010637 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010638 mbedtls_free( handshake->psk );
10639 }
10640#endif
10641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010642#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10643 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010644 /*
10645 * Free only the linked list wrapper, not the keys themselves
10646 * since the belong to the SNI callback
10647 */
10648 if( handshake->sni_key_cert != NULL )
10649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010650 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010651
10652 while( cur != NULL )
10653 {
10654 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010656 cur = next;
10657 }
10658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010659#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010660
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010661#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010662 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010663 if( handshake->ecrs_peer_cert != NULL )
10664 {
10665 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10666 mbedtls_free( handshake->ecrs_peer_cert );
10667 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010668#endif
10669
Hanno Becker75173122019-02-06 16:18:31 +000010670#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10671 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10672 mbedtls_pk_free( &handshake->peer_pubkey );
10673#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675#if defined(MBEDTLS_SSL_PROTO_DTLS)
10676 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010677 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010678 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010679#endif
10680
Hanno Becker4a63ed42019-01-08 11:39:35 +000010681#if defined(MBEDTLS_ECDH_C) && \
10682 defined(MBEDTLS_USE_PSA_CRYPTO)
10683 psa_destroy_key( handshake->ecdh_psa_privkey );
10684#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10685
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010686 mbedtls_platform_zeroize( handshake,
10687 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010688}
10689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010691{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010692 if( session == NULL )
10693 return;
10694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010695#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010696 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010697#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010698
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010699#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010700 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010701#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010702
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010703 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010704}
10705
Paul Bakker5121ce52009-01-03 21:22:43 +000010706/*
10707 * Free an SSL context
10708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010709void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010710{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010711 if( ssl == NULL )
10712 return;
10713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010715
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010716 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010717 {
Angus Grattond8213d02016-05-25 20:56:48 +100010718 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010719 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010720 }
10721
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010722 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010723 {
Angus Grattond8213d02016-05-25 20:56:48 +100010724 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010725 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010726 }
10727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010729 if( ssl->compress_buf != NULL )
10730 {
Angus Grattond8213d02016-05-25 20:56:48 +100010731 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010732 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010733 }
10734#endif
10735
Paul Bakker48916f92012-09-16 19:57:18 +000010736 if( ssl->transform )
10737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010738 mbedtls_ssl_transform_free( ssl->transform );
10739 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010740 }
10741
10742 if( ssl->handshake )
10743 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010744 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10746 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010748 mbedtls_free( ssl->handshake );
10749 mbedtls_free( ssl->transform_negotiate );
10750 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010751 }
10752
Paul Bakkerc0463502013-02-14 11:19:38 +010010753 if( ssl->session )
10754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755 mbedtls_ssl_session_free( ssl->session );
10756 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010757 }
10758
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010759#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010760 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010761 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010762 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010763 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010764 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010765#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10768 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10771 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010772 }
10773#endif
10774
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010775#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010777#endif
10778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010780
Paul Bakker86f04f42013-02-14 11:20:09 +010010781 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010782 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010783}
10784
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010785/*
10786 * Initialze mbedtls_ssl_config
10787 */
10788void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10789{
10790 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10791}
10792
Simon Butcherc97b6972015-12-27 23:48:17 +000010793#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010794static int ssl_preset_default_hashes[] = {
10795#if defined(MBEDTLS_SHA512_C)
10796 MBEDTLS_MD_SHA512,
10797 MBEDTLS_MD_SHA384,
10798#endif
10799#if defined(MBEDTLS_SHA256_C)
10800 MBEDTLS_MD_SHA256,
10801 MBEDTLS_MD_SHA224,
10802#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010803#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010804 MBEDTLS_MD_SHA1,
10805#endif
10806 MBEDTLS_MD_NONE
10807};
Simon Butcherc97b6972015-12-27 23:48:17 +000010808#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010809
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010810static int ssl_preset_suiteb_ciphersuites[] = {
10811 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10812 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10813 0
10814};
10815
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010816#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010817static int ssl_preset_suiteb_hashes[] = {
10818 MBEDTLS_MD_SHA256,
10819 MBEDTLS_MD_SHA384,
10820 MBEDTLS_MD_NONE
10821};
10822#endif
10823
10824#if defined(MBEDTLS_ECP_C)
10825static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010826#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010827 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010828#endif
10829#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010830 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010831#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010832 MBEDTLS_ECP_DP_NONE
10833};
10834#endif
10835
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010836/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010837 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010838 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010839int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010840 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010841{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010842#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010843 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010844#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010845
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010846 /* Use the functions here so that they are covered in tests,
10847 * but otherwise access member directly for efficiency */
10848 mbedtls_ssl_conf_endpoint( conf, endpoint );
10849 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010850
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010851 /*
10852 * Things that are common to all presets
10853 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010854#if defined(MBEDTLS_SSL_CLI_C)
10855 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10856 {
10857 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10858#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10859 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10860#endif
10861 }
10862#endif
10863
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010864#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010865 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010866#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010867
10868#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10869 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10870#endif
10871
10872#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10873 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10874#endif
10875
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010876#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10877 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10878#endif
10879
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010880#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010881 conf->f_cookie_write = ssl_cookie_write_dummy;
10882 conf->f_cookie_check = ssl_cookie_check_dummy;
10883#endif
10884
10885#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10886 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10887#endif
10888
Janos Follath088ce432017-04-10 12:42:31 +010010889#if defined(MBEDTLS_SSL_SRV_C)
10890 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10891#endif
10892
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010893#if defined(MBEDTLS_SSL_PROTO_DTLS)
10894 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10895 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10896#endif
10897
10898#if defined(MBEDTLS_SSL_RENEGOTIATION)
10899 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010900 memset( conf->renego_period, 0x00, 2 );
10901 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010902#endif
10903
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010904#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10905 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10906 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010907 const unsigned char dhm_p[] =
10908 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10909 const unsigned char dhm_g[] =
10910 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10911
Hanno Beckera90658f2017-10-04 15:29:08 +010010912 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10913 dhm_p, sizeof( dhm_p ),
10914 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010915 {
10916 return( ret );
10917 }
10918 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010919#endif
10920
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010921 /*
10922 * Preset-specific defaults
10923 */
10924 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010925 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010926 /*
10927 * NSA Suite B
10928 */
10929 case MBEDTLS_SSL_PRESET_SUITEB:
10930 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10931 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10932 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10933 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10934
10935 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10936 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10937 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10938 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10939 ssl_preset_suiteb_ciphersuites;
10940
10941#if defined(MBEDTLS_X509_CRT_PARSE_C)
10942 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010943#endif
10944
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010945#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010946 conf->sig_hashes = ssl_preset_suiteb_hashes;
10947#endif
10948
10949#if defined(MBEDTLS_ECP_C)
10950 conf->curve_list = ssl_preset_suiteb_curves;
10951#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010952 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010953
10954 /*
10955 * Default
10956 */
10957 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010958 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10959 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10960 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10961 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10962 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10963 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10964 MBEDTLS_SSL_MIN_MINOR_VERSION :
10965 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010966 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10967 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10968
10969#if defined(MBEDTLS_SSL_PROTO_DTLS)
10970 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10971 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10972#endif
10973
10974 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10975 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10976 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10977 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10978 mbedtls_ssl_list_ciphersuites();
10979
10980#if defined(MBEDTLS_X509_CRT_PARSE_C)
10981 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10982#endif
10983
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010984#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010985 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010986#endif
10987
10988#if defined(MBEDTLS_ECP_C)
10989 conf->curve_list = mbedtls_ecp_grp_id_list();
10990#endif
10991
10992#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10993 conf->dhm_min_bitlen = 1024;
10994#endif
10995 }
10996
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010997 return( 0 );
10998}
10999
11000/*
11001 * Free mbedtls_ssl_config
11002 */
11003void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
11004{
11005#if defined(MBEDTLS_DHM_C)
11006 mbedtls_mpi_free( &conf->dhm_P );
11007 mbedtls_mpi_free( &conf->dhm_G );
11008#endif
11009
11010#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11011 if( conf->psk != NULL )
11012 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011013 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011014 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000011015 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011016 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090011017 }
11018
11019 if( conf->psk_identity != NULL )
11020 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011021 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090011022 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000011023 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011024 conf->psk_identity_len = 0;
11025 }
11026#endif
11027
11028#if defined(MBEDTLS_X509_CRT_PARSE_C)
11029 ssl_key_cert_free( conf->key_cert );
11030#endif
11031
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011032 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011033}
11034
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011035#if defined(MBEDTLS_PK_C) && \
11036 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011037/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011038 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011039 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011040unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011041{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011042#if defined(MBEDTLS_RSA_C)
11043 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
11044 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011046#if defined(MBEDTLS_ECDSA_C)
11047 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
11048 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011049#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011050 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011051}
11052
Hanno Becker7e5437a2017-04-28 17:15:26 +010011053unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11054{
11055 switch( type ) {
11056 case MBEDTLS_PK_RSA:
11057 return( MBEDTLS_SSL_SIG_RSA );
11058 case MBEDTLS_PK_ECDSA:
11059 case MBEDTLS_PK_ECKEY:
11060 return( MBEDTLS_SSL_SIG_ECDSA );
11061 default:
11062 return( MBEDTLS_SSL_SIG_ANON );
11063 }
11064}
11065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011066mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011067{
11068 switch( sig )
11069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011070#if defined(MBEDTLS_RSA_C)
11071 case MBEDTLS_SSL_SIG_RSA:
11072 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011073#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011074#if defined(MBEDTLS_ECDSA_C)
11075 case MBEDTLS_SSL_SIG_ECDSA:
11076 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011077#endif
11078 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011079 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011080 }
11081}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011082#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011083
Hanno Becker7e5437a2017-04-28 17:15:26 +010011084#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11085 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11086
11087/* Find an entry in a signature-hash set matching a given hash algorithm. */
11088mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11089 mbedtls_pk_type_t sig_alg )
11090{
11091 switch( sig_alg )
11092 {
11093 case MBEDTLS_PK_RSA:
11094 return( set->rsa );
11095 case MBEDTLS_PK_ECDSA:
11096 return( set->ecdsa );
11097 default:
11098 return( MBEDTLS_MD_NONE );
11099 }
11100}
11101
11102/* Add a signature-hash-pair to a signature-hash set */
11103void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11104 mbedtls_pk_type_t sig_alg,
11105 mbedtls_md_type_t md_alg )
11106{
11107 switch( sig_alg )
11108 {
11109 case MBEDTLS_PK_RSA:
11110 if( set->rsa == MBEDTLS_MD_NONE )
11111 set->rsa = md_alg;
11112 break;
11113
11114 case MBEDTLS_PK_ECDSA:
11115 if( set->ecdsa == MBEDTLS_MD_NONE )
11116 set->ecdsa = md_alg;
11117 break;
11118
11119 default:
11120 break;
11121 }
11122}
11123
11124/* Allow exactly one hash algorithm for each signature. */
11125void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11126 mbedtls_md_type_t md_alg )
11127{
11128 set->rsa = md_alg;
11129 set->ecdsa = md_alg;
11130}
11131
11132#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11133 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11134
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011135/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011136 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011138mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011139{
11140 switch( hash )
11141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011142#if defined(MBEDTLS_MD5_C)
11143 case MBEDTLS_SSL_HASH_MD5:
11144 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011146#if defined(MBEDTLS_SHA1_C)
11147 case MBEDTLS_SSL_HASH_SHA1:
11148 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011150#if defined(MBEDTLS_SHA256_C)
11151 case MBEDTLS_SSL_HASH_SHA224:
11152 return( MBEDTLS_MD_SHA224 );
11153 case MBEDTLS_SSL_HASH_SHA256:
11154 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011156#if defined(MBEDTLS_SHA512_C)
11157 case MBEDTLS_SSL_HASH_SHA384:
11158 return( MBEDTLS_MD_SHA384 );
11159 case MBEDTLS_SSL_HASH_SHA512:
11160 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011161#endif
11162 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011163 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011164 }
11165}
11166
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011167/*
11168 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11169 */
11170unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11171{
11172 switch( md )
11173 {
11174#if defined(MBEDTLS_MD5_C)
11175 case MBEDTLS_MD_MD5:
11176 return( MBEDTLS_SSL_HASH_MD5 );
11177#endif
11178#if defined(MBEDTLS_SHA1_C)
11179 case MBEDTLS_MD_SHA1:
11180 return( MBEDTLS_SSL_HASH_SHA1 );
11181#endif
11182#if defined(MBEDTLS_SHA256_C)
11183 case MBEDTLS_MD_SHA224:
11184 return( MBEDTLS_SSL_HASH_SHA224 );
11185 case MBEDTLS_MD_SHA256:
11186 return( MBEDTLS_SSL_HASH_SHA256 );
11187#endif
11188#if defined(MBEDTLS_SHA512_C)
11189 case MBEDTLS_MD_SHA384:
11190 return( MBEDTLS_SSL_HASH_SHA384 );
11191 case MBEDTLS_MD_SHA512:
11192 return( MBEDTLS_SSL_HASH_SHA512 );
11193#endif
11194 default:
11195 return( MBEDTLS_SSL_HASH_NONE );
11196 }
11197}
11198
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011199#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011200/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011201 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011202 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011203 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011204int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011205{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011206 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011207
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011208 if( ssl->conf->curve_list == NULL )
11209 return( -1 );
11210
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011211 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011212 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011213 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011214
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011215 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011216}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011217#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011218
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011219#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011220/*
11221 * Check if a hash proposed by the peer is in our list.
11222 * Return 0 if we're willing to use it, -1 otherwise.
11223 */
11224int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11225 mbedtls_md_type_t md )
11226{
11227 const int *cur;
11228
11229 if( ssl->conf->sig_hashes == NULL )
11230 return( -1 );
11231
11232 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11233 if( *cur == (int) md )
11234 return( 0 );
11235
11236 return( -1 );
11237}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011238#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011240#if defined(MBEDTLS_X509_CRT_PARSE_C)
11241int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11242 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011243 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011244 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011245{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011246 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011247#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011248 int usage = 0;
11249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011250#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011251 const char *ext_oid;
11252 size_t ext_len;
11253#endif
11254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011255#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11256 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011257 ((void) cert);
11258 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011259 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011260#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011262#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11263 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011264 {
11265 /* Server part of the key exchange */
11266 switch( ciphersuite->key_exchange )
11267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011268 case MBEDTLS_KEY_EXCHANGE_RSA:
11269 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011270 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011271 break;
11272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011273 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11274 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11275 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11276 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011277 break;
11278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011279 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11280 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011281 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011282 break;
11283
11284 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011285 case MBEDTLS_KEY_EXCHANGE_NONE:
11286 case MBEDTLS_KEY_EXCHANGE_PSK:
11287 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11288 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011289 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011290 usage = 0;
11291 }
11292 }
11293 else
11294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011295 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11296 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011297 }
11298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011299 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011300 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011301 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011302 ret = -1;
11303 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011304#else
11305 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011306#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011308#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11309 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011311 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11312 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011313 }
11314 else
11315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011316 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11317 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011318 }
11319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011320 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011321 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011322 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011323 ret = -1;
11324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011325#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011326
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011327 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011328}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011329#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011330
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011331/*
11332 * Convert version numbers to/from wire format
11333 * and, for DTLS, to/from TLS equivalent.
11334 *
11335 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011336 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011337 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11338 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011340void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011341 unsigned char ver[2] )
11342{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011343#if defined(MBEDTLS_SSL_PROTO_DTLS)
11344 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011346 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011347 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11348
11349 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11350 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11351 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011352 else
11353#else
11354 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011355#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011356 {
11357 ver[0] = (unsigned char) major;
11358 ver[1] = (unsigned char) minor;
11359 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011360}
11361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011362void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011363 const unsigned char ver[2] )
11364{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011365#if defined(MBEDTLS_SSL_PROTO_DTLS)
11366 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011367 {
11368 *major = 255 - ver[0] + 2;
11369 *minor = 255 - ver[1] + 1;
11370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011371 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011372 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11373 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011374 else
11375#else
11376 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011377#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011378 {
11379 *major = ver[0];
11380 *minor = ver[1];
11381 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011382}
11383
Simon Butcher99000142016-10-13 17:21:01 +010011384int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11385{
11386#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11387 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11388 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11389
11390 switch( md )
11391 {
11392#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11393#if defined(MBEDTLS_MD5_C)
11394 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011395 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011396#endif
11397#if defined(MBEDTLS_SHA1_C)
11398 case MBEDTLS_SSL_HASH_SHA1:
11399 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11400 break;
11401#endif
11402#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11403#if defined(MBEDTLS_SHA512_C)
11404 case MBEDTLS_SSL_HASH_SHA384:
11405 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11406 break;
11407#endif
11408#if defined(MBEDTLS_SHA256_C)
11409 case MBEDTLS_SSL_HASH_SHA256:
11410 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11411 break;
11412#endif
11413 default:
11414 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11415 }
11416
11417 return 0;
11418#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11419 (void) ssl;
11420 (void) md;
11421
11422 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11423#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11424}
11425
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011426#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11427 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11428int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11429 unsigned char *output,
11430 unsigned char *data, size_t data_len )
11431{
11432 int ret = 0;
11433 mbedtls_md5_context mbedtls_md5;
11434 mbedtls_sha1_context mbedtls_sha1;
11435
11436 mbedtls_md5_init( &mbedtls_md5 );
11437 mbedtls_sha1_init( &mbedtls_sha1 );
11438
11439 /*
11440 * digitally-signed struct {
11441 * opaque md5_hash[16];
11442 * opaque sha_hash[20];
11443 * };
11444 *
11445 * md5_hash
11446 * MD5(ClientHello.random + ServerHello.random
11447 * + ServerParams);
11448 * sha_hash
11449 * SHA(ClientHello.random + ServerHello.random
11450 * + ServerParams);
11451 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011452 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011453 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011455 goto exit;
11456 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011457 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011458 ssl->handshake->randbytes, 64 ) ) != 0 )
11459 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011461 goto exit;
11462 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011463 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011464 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011465 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011466 goto exit;
11467 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011468 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011469 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011471 goto exit;
11472 }
11473
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011474 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011475 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011477 goto exit;
11478 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011479 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011480 ssl->handshake->randbytes, 64 ) ) != 0 )
11481 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011483 goto exit;
11484 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011485 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011486 data_len ) ) != 0 )
11487 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011489 goto exit;
11490 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011491 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011492 output + 16 ) ) != 0 )
11493 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011495 goto exit;
11496 }
11497
11498exit:
11499 mbedtls_md5_free( &mbedtls_md5 );
11500 mbedtls_sha1_free( &mbedtls_sha1 );
11501
11502 if( ret != 0 )
11503 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11504 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11505
11506 return( ret );
11507
11508}
11509#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11510 MBEDTLS_SSL_PROTO_TLS1_1 */
11511
11512#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11513 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011514
11515#if defined(MBEDTLS_USE_PSA_CRYPTO)
11516int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11517 unsigned char *hash, size_t *hashlen,
11518 unsigned char *data, size_t data_len,
11519 mbedtls_md_type_t md_alg )
11520{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011521 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011522 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011523 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11524
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011525 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011526
11527 if( ( status = psa_hash_setup( &hash_operation,
11528 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011529 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011530 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011531 goto exit;
11532 }
11533
Andrzej Kurek814feff2019-01-14 04:35:19 -050011534 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11535 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011536 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011537 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011538 goto exit;
11539 }
11540
Andrzej Kurek814feff2019-01-14 04:35:19 -050011541 if( ( status = psa_hash_update( &hash_operation,
11542 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011543 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011544 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011545 goto exit;
11546 }
11547
Andrzej Kurek814feff2019-01-14 04:35:19 -050011548 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11549 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011550 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011551 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011552 goto exit;
11553 }
11554
11555exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011556 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011557 {
11558 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11559 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011560 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011561 {
11562 case PSA_ERROR_NOT_SUPPORTED:
11563 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011564 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011565 case PSA_ERROR_BUFFER_TOO_SMALL:
11566 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11567 case PSA_ERROR_INSUFFICIENT_MEMORY:
11568 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11569 default:
11570 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11571 }
11572 }
11573 return( 0 );
11574}
11575
11576#else
11577
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011578int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011579 unsigned char *hash, size_t *hashlen,
11580 unsigned char *data, size_t data_len,
11581 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011582{
11583 int ret = 0;
11584 mbedtls_md_context_t ctx;
11585 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011586 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011587
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011589
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011590 mbedtls_md_init( &ctx );
11591
11592 /*
11593 * digitally-signed struct {
11594 * opaque client_random[32];
11595 * opaque server_random[32];
11596 * ServerDHParams params;
11597 * };
11598 */
11599 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11600 {
11601 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11602 goto exit;
11603 }
11604 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11605 {
11606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11607 goto exit;
11608 }
11609 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11610 {
11611 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11612 goto exit;
11613 }
11614 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11615 {
11616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11617 goto exit;
11618 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011619 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011620 {
11621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11622 goto exit;
11623 }
11624
11625exit:
11626 mbedtls_md_free( &ctx );
11627
11628 if( ret != 0 )
11629 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11630 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11631
11632 return( ret );
11633}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011634#endif /* MBEDTLS_USE_PSA_CRYPTO */
11635
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011636#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11637 MBEDTLS_SSL_PROTO_TLS1_2 */
11638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011639#endif /* MBEDTLS_SSL_TLS_C */