blob: 3e0552c4eca3281af0f02ed8e009eaad271c0c1f [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
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100114
Hanno Beckercfe45792019-07-03 16:13:00 +0100115#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +0100116static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
117 unsigned char *buf,
118 size_t len,
119 mbedtls_record *rec );
120
Hanno Beckercfe45792019-07-03 16:13:00 +0100121int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
122 unsigned char *buf,
123 size_t buflen )
124{
Hanno Becker54229812019-07-12 14:40:00 +0100125 int ret = 0;
126 mbedtls_record rec;
127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
128 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
129
130 /* We don't support record checking in TLS because
131 * (a) there doesn't seem to be a usecase for it, and
132 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
133 * and we'd need to backup the transform here.
134 */
135 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
136 {
137 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
138 goto exit;
139 }
140#if defined(MBEDTLS_SSL_PROTO_DTLS)
141 else
142 {
143 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
144 if( ret != 0 )
145 {
146 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
147 goto exit;
148 }
149
150 if( ssl->transform_in != NULL )
151 {
152 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
153 if( ret != 0 )
154 {
155 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
156 goto exit;
157 }
158 }
159 }
160#endif /* MBEDTLS_SSL_PROTO_DTLS */
161
162exit:
163 /* On success, we have decrypted the buffer in-place, so make
164 * sure we don't leak any plaintext data. */
165 mbedtls_platform_zeroize( buf, buflen );
166
167 /* For the purpose of this API, treat messages with unexpected CID
168 * as well as such from future epochs as unexpected. */
169 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
170 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
171 {
172 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
173 }
174
175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
176 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100177}
178#endif /* MBEDTLS_SSL_RECORD_CHECKING */
179
Hanno Becker67bc7c32018-08-06 11:33:50 +0100180#define SSL_DONT_FORCE_FLUSH 0
181#define SSL_FORCE_FLUSH 1
182
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200183#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100184
Hanno Beckera0e20d02019-05-15 14:03:01 +0100185#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100186/* Top-level Connection ID API */
187
Hanno Becker8367ccc2019-05-14 11:30:10 +0100188int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
189 size_t len,
190 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100191{
192 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
194
Hanno Becker611ac772019-05-14 11:45:26 +0100195 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
196 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
197 {
198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
199 }
200
201 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100202 conf->cid_len = len;
203 return( 0 );
204}
205
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100206int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
207 int enable,
208 unsigned char const *own_cid,
209 size_t own_cid_len )
210{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100211 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
213
Hanno Beckerca092242019-04-25 16:01:49 +0100214 ssl->negotiate_cid = enable;
215 if( enable == MBEDTLS_SSL_CID_DISABLED )
216 {
217 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
218 return( 0 );
219 }
220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100221 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100222
Hanno Beckerad4a1372019-05-03 13:06:44 +0100223 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100224 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
226 (unsigned) own_cid_len,
227 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
229 }
230
231 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100232 /* Truncation is not an issue here because
233 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
234 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100235
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100236 return( 0 );
237}
238
239int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
240 int *enabled,
241 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
242 size_t *peer_cid_len )
243{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100244 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100245
Hanno Becker76a79ab2019-05-03 14:38:32 +0100246 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
247 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
248 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100250 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100251
Hanno Beckerc5f24222019-05-03 12:54:52 +0100252 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
253 * were used, but client and server requested the empty CID.
254 * This is indistinguishable from not using the CID extension
255 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100256 if( ssl->transform_in->in_cid_len == 0 &&
257 ssl->transform_in->out_cid_len == 0 )
258 {
259 return( 0 );
260 }
261
Hanno Becker615ef172019-05-22 16:50:35 +0100262 if( peer_cid_len != NULL )
263 {
264 *peer_cid_len = ssl->transform_in->out_cid_len;
265 if( peer_cid != NULL )
266 {
267 memcpy( peer_cid, ssl->transform_in->out_cid,
268 ssl->transform_in->out_cid_len );
269 }
270 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100271
272 *enabled = MBEDTLS_SSL_CID_ENABLED;
273
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100274 return( 0 );
275}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100276#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100277
Hanno Beckerd5847772018-08-28 10:09:23 +0100278/* Forward declarations for functions related to message buffering. */
279static void ssl_buffering_free( mbedtls_ssl_context *ssl );
280static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
281 uint8_t slot );
282static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
283static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
284static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
285static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100286static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
287 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100288static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100289
Hanno Beckera67dee22018-08-22 10:05:20 +0100290static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100291static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100292{
Hanno Becker11682cc2018-08-22 14:41:02 +0100293 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100294
295 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100296 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100297
298 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
299}
300
Hanno Becker67bc7c32018-08-06 11:33:50 +0100301static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
302{
Hanno Becker11682cc2018-08-22 14:41:02 +0100303 size_t const bytes_written = ssl->out_left;
304 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100305
306 /* Double-check that the write-index hasn't gone
307 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100308 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100309 {
310 /* Should never happen... */
311 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
312 }
313
314 return( (int) ( mtu - bytes_written ) );
315}
316
317static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
318{
319 int ret;
320 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400321 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100322
323#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
324 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
325
326 if( max_len > mfl )
327 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100328
329 /* By the standard (RFC 6066 Sect. 4), the MFL extension
330 * only limits the maximum record payload size, so in theory
331 * we would be allowed to pack multiple records of payload size
332 * MFL into a single datagram. However, this would mean that there's
333 * no way to explicitly communicate MTU restrictions to the peer.
334 *
335 * The following reduction of max_len makes sure that we never
336 * write datagrams larger than MFL + Record Expansion Overhead.
337 */
338 if( max_len <= ssl->out_left )
339 return( 0 );
340
341 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100342#endif
343
344 ret = ssl_get_remaining_space_in_datagram( ssl );
345 if( ret < 0 )
346 return( ret );
347 remaining = (size_t) ret;
348
349 ret = mbedtls_ssl_get_record_expansion( ssl );
350 if( ret < 0 )
351 return( ret );
352 expansion = (size_t) ret;
353
354 if( remaining <= expansion )
355 return( 0 );
356
357 remaining -= expansion;
358 if( remaining >= max_len )
359 remaining = max_len;
360
361 return( (int) remaining );
362}
363
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200364/*
365 * Double the retransmit timeout value, within the allowed range,
366 * returning -1 if the maximum value has already been reached.
367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200369{
370 uint32_t new_timeout;
371
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200372 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200373 return( -1 );
374
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200375 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
376 * in the following way: after the initial transmission and a first
377 * retransmission, back off to a temporary estimated MTU of 508 bytes.
378 * This value is guaranteed to be deliverable (if not guaranteed to be
379 * delivered) of any compliant IPv4 (and IPv6) network, and should work
380 * on most non-IP stacks too. */
381 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400382 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200383 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
385 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200386
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200387 new_timeout = 2 * ssl->handshake->retransmit_timeout;
388
389 /* Avoid arithmetic overflow and range overflow */
390 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200391 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200392 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200393 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200394 }
395
396 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200398 ssl->handshake->retransmit_timeout ) );
399
400 return( 0 );
401}
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200404{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200405 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200407 ssl->handshake->retransmit_timeout ) );
408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200412/*
413 * Convert max_fragment_length codes to length.
414 * RFC 6066 says:
415 * enum{
416 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
417 * } MaxFragmentLength;
418 * and we add 0 -> extension unused
419 */
Angus Grattond8213d02016-05-25 20:56:48 +1000420static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200421{
Angus Grattond8213d02016-05-25 20:56:48 +1000422 switch( mfl )
423 {
424 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
425 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
426 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
427 return 512;
428 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
429 return 1024;
430 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
431 return 2048;
432 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
433 return 4096;
434 default:
435 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
436 }
437}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200439
Hanno Becker52055ae2019-02-06 14:30:46 +0000440int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
441 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_ssl_session_free( dst );
444 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000447
448#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200449 if( src->peer_cert != NULL )
450 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200451 int ret;
452
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200453 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200454 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200455 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200460 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200463 dst->peer_cert = NULL;
464 return( ret );
465 }
466 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000467#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000468 if( src->peer_cert_digest != NULL )
469 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000470 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000471 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000472 if( dst->peer_cert_digest == NULL )
473 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
474
475 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
476 src->peer_cert_digest_len );
477 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000478 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000479 }
480#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200483
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200484#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200485 if( src->ticket != NULL )
486 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200487 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200488 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200489 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200490
491 memcpy( dst->ticket, src->ticket, src->ticket_len );
492 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200493#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200494
495 return( 0 );
496}
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
499int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200500 const unsigned char *key_enc, const unsigned char *key_dec,
501 size_t keylen,
502 const unsigned char *iv_enc, const unsigned char *iv_dec,
503 size_t ivlen,
504 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200505 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
507int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
508int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
509int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
510int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
511#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000512
Paul Bakker5121ce52009-01-03 21:22:43 +0000513/*
514 * Key material generation
515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200517static int ssl3_prf( const unsigned char *secret, size_t slen,
518 const char *label,
519 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000520 unsigned char *dstbuf, size_t dlen )
521{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100522 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000523 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200524 mbedtls_md5_context md5;
525 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000526 unsigned char padding[16];
527 unsigned char sha1sum[20];
528 ((void)label);
529
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200530 mbedtls_md5_init( &md5 );
531 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200532
Paul Bakker5f70b252012-09-13 14:23:06 +0000533 /*
534 * SSLv3:
535 * block =
536 * MD5( secret + SHA1( 'A' + secret + random ) ) +
537 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
538 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
539 * ...
540 */
541 for( i = 0; i < dlen / 16; i++ )
542 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200543 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000544
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100545 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 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, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100548 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100549 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100550 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100551 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100552 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100553 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100554 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000555
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100556 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100557 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100558 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100559 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100560 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100561 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100562 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100563 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000564 }
565
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100566exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200567 mbedtls_md5_free( &md5 );
568 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000569
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500570 mbedtls_platform_zeroize( padding, sizeof( padding ) );
571 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000572
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100573 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200578static int tls1_prf( const unsigned char *secret, size_t slen,
579 const char *label,
580 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000581 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000582{
Paul Bakker23986e52011-04-24 08:57:21 +0000583 size_t nb, hs;
584 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200585 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300586 unsigned char *tmp;
587 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 const mbedtls_md_info_t *md_info;
590 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100591 int ret;
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Ron Eldor3b350852019-05-07 18:31:49 +0300595 tmp_len = 20 + strlen( label ) + rlen;
596 tmp = mbedtls_calloc( 1, tmp_len );
597 if( tmp == NULL )
598 {
599 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
600 goto exit;
601 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
603 hs = ( slen + 1 ) / 2;
604 S1 = secret;
605 S2 = secret + slen - hs;
606
607 nb = strlen( label );
608 memcpy( tmp + 20, label, nb );
609 memcpy( tmp + 20 + nb, random, rlen );
610 nb += rlen;
611
612 /*
613 * First compute P_md5(secret,label+random)[0..dlen]
614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300616 {
617 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
618 goto exit;
619 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300622 {
623 goto exit;
624 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
627 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
628 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630 for( i = 0; i < dlen; i += 16 )
631 {
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 + nb );
634 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_md_hmac_reset ( &md_ctx );
637 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
638 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
641
642 for( j = 0; j < k; j++ )
643 dstbuf[i + j] = h_i[j];
644 }
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100647
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 /*
649 * XOR out with P_sha1(secret,label+random)[0..dlen]
650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300652 {
653 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
654 goto exit;
655 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300658 {
659 goto exit;
660 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
663 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
664 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
666 for( i = 0; i < dlen; i += 20 )
667 {
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 + nb );
670 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_md_hmac_reset ( &md_ctx );
673 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
674 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
677
678 for( j = 0; j < k; j++ )
679 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
680 }
681
Ron Eldor3b350852019-05-07 18:31:49 +0300682exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100684
Ron Eldor3b350852019-05-07 18:31:49 +0300685 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500686 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000687
Ron Eldor3b350852019-05-07 18:31:49 +0300688 mbedtls_free( tmp );
689 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500694#if defined(MBEDTLS_USE_PSA_CRYPTO)
695static int tls_prf_generic( mbedtls_md_type_t md_type,
696 const unsigned char *secret, size_t slen,
697 const char *label,
698 const unsigned char *random, size_t rlen,
699 unsigned char *dstbuf, size_t dlen )
700{
701 psa_status_t status;
702 psa_algorithm_t alg;
703 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500704 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500705 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
706
Andrzej Kurek2f760752019-01-28 08:08:15 -0500707 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500708 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500709
Andrzej Kurekc929a822019-01-14 03:51:11 -0500710 if( md_type == MBEDTLS_MD_SHA384 )
711 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
712 else
713 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
714
Andrzej Kurek2f760752019-01-28 08:08:15 -0500715 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500716 psa_key_policy_set_usage( &policy,
717 PSA_KEY_USAGE_DERIVE,
718 alg );
719 status = psa_set_key_policy( master_slot, &policy );
720 if( status != PSA_SUCCESS )
721 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
722
723 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
724 if( status != PSA_SUCCESS )
725 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
726
727 status = psa_key_derivation( &generator,
728 master_slot, alg,
729 random, rlen,
730 (unsigned char const *) label,
731 (size_t) strlen( label ),
732 dlen );
733 if( status != PSA_SUCCESS )
734 {
735 psa_generator_abort( &generator );
736 psa_destroy_key( master_slot );
737 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
738 }
739
740 status = psa_generator_read( &generator, dstbuf, dlen );
741 if( status != PSA_SUCCESS )
742 {
743 psa_generator_abort( &generator );
744 psa_destroy_key( master_slot );
745 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
746 }
747
748 status = psa_generator_abort( &generator );
749 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500750 {
751 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500752 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500753 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500754
755 status = psa_destroy_key( master_slot );
756 if( status != PSA_SUCCESS )
757 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
758
Andrzej Kurek33171262019-01-15 03:25:18 -0500759 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500760}
761
762#else /* MBEDTLS_USE_PSA_CRYPTO */
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100765 const unsigned char *secret, size_t slen,
766 const char *label,
767 const unsigned char *random, size_t rlen,
768 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000769{
770 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100771 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300772 unsigned char *tmp;
773 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
775 const mbedtls_md_info_t *md_info;
776 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100777 int ret;
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
782 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100785
Ron Eldor3b350852019-05-07 18:31:49 +0300786 tmp_len = md_len + strlen( label ) + rlen;
787 tmp = mbedtls_calloc( 1, tmp_len );
788 if( tmp == NULL )
789 {
790 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
791 goto exit;
792 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000793
794 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100795 memcpy( tmp + md_len, label, nb );
796 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000797 nb += rlen;
798
799 /*
800 * Compute P_<hash>(secret, label + random)[0..dlen]
801 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300803 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
806 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
807 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100808
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100809 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_md_hmac_reset ( &md_ctx );
812 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
813 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_md_hmac_reset ( &md_ctx );
816 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
817 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000818
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100819 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000820
821 for( j = 0; j < k; j++ )
822 dstbuf[i + j] = h_i[j];
823 }
824
Ron Eldor3b350852019-05-07 18:31:49 +0300825exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100827
Ron Eldor3b350852019-05-07 18:31:49 +0300828 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500829 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000830
Ron Eldor3b350852019-05-07 18:31:49 +0300831 mbedtls_free( tmp );
832
833 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000834}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500835#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100837static int tls_prf_sha256( const unsigned char *secret, size_t slen,
838 const char *label,
839 const unsigned char *random, size_t rlen,
840 unsigned char *dstbuf, size_t dlen )
841{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100843 label, random, rlen, dstbuf, dlen ) );
844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200848static int tls_prf_sha384( const unsigned char *secret, size_t slen,
849 const char *label,
850 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000851 unsigned char *dstbuf, size_t dlen )
852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100854 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000855}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#endif /* MBEDTLS_SHA512_C */
857#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
862 defined(MBEDTLS_SSL_PROTO_TLS1_1)
863static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200864#endif
Paul Bakker380da532012-04-18 16:10:25 +0000865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if defined(MBEDTLS_SSL_PROTO_SSL3)
867static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
868static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200869#endif
870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
872static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
873static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200874#endif
875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
877#if defined(MBEDTLS_SHA256_C)
878static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
879static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
880static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200881#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883#if defined(MBEDTLS_SHA512_C)
884static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
885static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
886static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100887#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000889
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100890#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100891 defined(MBEDTLS_USE_PSA_CRYPTO)
892static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
893{
894 if( ssl->conf->f_psk != NULL )
895 {
896 /* If we've used a callback to select the PSK,
897 * the static configuration is irrelevant. */
898 if( ssl->handshake->psk_opaque != 0 )
899 return( 1 );
900
901 return( 0 );
902 }
903
904 if( ssl->conf->psk_opaque != 0 )
905 return( 1 );
906
907 return( 0 );
908}
909#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100910 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100911
Ron Eldorcf280092019-05-14 20:19:13 +0300912#if defined(MBEDTLS_SSL_EXPORT_KEYS)
913static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
914{
915#if defined(MBEDTLS_SSL_PROTO_SSL3)
916 if( tls_prf == ssl3_prf )
917 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300918 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300919 }
920 else
921#endif
922#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
923 if( tls_prf == tls1_prf )
924 {
925 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
926 }
927 else
928#endif
929#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
930#if defined(MBEDTLS_SHA512_C)
931 if( tls_prf == tls_prf_sha384 )
932 {
933 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
934 }
935 else
936#endif
937#if defined(MBEDTLS_SHA256_C)
938 if( tls_prf == tls_prf_sha256 )
939 {
940 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
941 }
942 else
943#endif
944#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
945 return( MBEDTLS_SSL_TLS_PRF_NONE );
946}
947#endif /* MBEDTLS_SSL_EXPORT_KEYS */
948
Ron Eldor51d3ab52019-05-12 14:54:30 +0300949int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
950 const unsigned char *secret, size_t slen,
951 const char *label,
952 const unsigned char *random, size_t rlen,
953 unsigned char *dstbuf, size_t dlen )
954{
955 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
956
957 switch( prf )
958 {
959#if defined(MBEDTLS_SSL_PROTO_SSL3)
960 case MBEDTLS_SSL_TLS_PRF_SSL3:
961 tls_prf = ssl3_prf;
962 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300963#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300964#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
965 case MBEDTLS_SSL_TLS_PRF_TLS1:
966 tls_prf = tls1_prf;
967 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300968#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
969
970#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300971#if defined(MBEDTLS_SHA512_C)
972 case MBEDTLS_SSL_TLS_PRF_SHA384:
973 tls_prf = tls_prf_sha384;
974 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300975#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300976#if defined(MBEDTLS_SHA256_C)
977 case MBEDTLS_SSL_TLS_PRF_SHA256:
978 tls_prf = tls_prf_sha256;
979 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300980#endif /* MBEDTLS_SHA256_C */
981#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300982 default:
983 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
984 }
985
986 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
987}
988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000990{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200991 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000992#if defined(MBEDTLS_USE_PSA_CRYPTO)
993 int psa_fallthrough;
994#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 unsigned char keyblk[256];
997 unsigned char *key1;
998 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100999 unsigned char *mac_enc;
1000 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001001 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001002 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001003 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001004 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 const mbedtls_cipher_info_t *cipher_info;
1006 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001007
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001008 /* cf. RFC 5246, Section 8.1:
1009 * "The master secret is always exactly 48 bytes in length." */
1010 size_t const master_secret_len = 48;
1011
Hanno Becker35b23c72018-10-23 12:10:41 +01001012#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1013 unsigned char session_hash[48];
1014#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_ssl_session *session = ssl->session_negotiate;
1017 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
1018 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +00001019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Jaeden Amero2de07f12019-06-05 13:32:08 +01001022#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1023 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001024 transform->encrypt_then_mac = session->encrypt_then_mac;
1025#endif
1026 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001027
1028 ciphersuite_info = handshake->ciphersuite_info;
1029 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001030 if( cipher_info == NULL )
1031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001033 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001035 }
1036
Hanno Beckere694c3e2017-12-27 21:34:08 +00001037 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001038 if( md_info == NULL )
1039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001041 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001043 }
1044
Hanno Beckera0e20d02019-05-15 14:03:01 +01001045#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001046 /* Copy own and peer's CID if the use of the CID
1047 * extension has been negotiated. */
1048 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1049 {
1050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001051
Hanno Becker05154c32019-05-03 15:23:51 +01001052 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001053 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001054 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001055 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001056
1057 transform->out_cid_len = ssl->handshake->peer_cid_len;
1058 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1059 ssl->handshake->peer_cid_len );
1060 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1061 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001062 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001063#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001064
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +00001066 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +00001067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_SSL_PROTO_SSL3)
1069 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001070 {
Paul Bakker48916f92012-09-16 19:57:18 +00001071 handshake->tls_prf = ssl3_prf;
1072 handshake->calc_verify = ssl_calc_verify_ssl;
1073 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001074 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001075 else
1076#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1078 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001079 {
Paul Bakker48916f92012-09-16 19:57:18 +00001080 handshake->tls_prf = tls1_prf;
1081 handshake->calc_verify = ssl_calc_verify_tls;
1082 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001083 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001084 else
1085#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1087#if defined(MBEDTLS_SHA512_C)
1088 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001089 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001090 {
Paul Bakker48916f92012-09-16 19:57:18 +00001091 handshake->tls_prf = tls_prf_sha384;
1092 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1093 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001094 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001095 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001096#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_SHA256_C)
1098 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001099 {
Paul Bakker48916f92012-09-16 19:57:18 +00001100 handshake->tls_prf = tls_prf_sha256;
1101 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1102 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001103 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001104 else
1105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1109 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001110 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001111
1112 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001113 * SSLv3:
1114 * master =
1115 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1116 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1117 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001118 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001119 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 * master = PRF( premaster, "master secret", randbytes )[0..47]
1121 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001122 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1125 }
1126 else
1127 {
1128 /* The label for the KDF used for key expansion.
1129 * This is either "master secret" or "extended master secret"
1130 * depending on whether the Extended Master Secret extension
1131 * is used. */
1132 char const *lbl = "master secret";
1133
1134 /* The salt for the KDF used for key expansion.
1135 * - If the Extended Master Secret extension is not used,
1136 * this is ClientHello.Random + ServerHello.Random
1137 * (see Sect. 8.1 in RFC 5246).
1138 * - If the Extended Master Secret extension is used,
1139 * this is the transcript of the handshake so far.
1140 * (see Sect. 4 in RFC 7627). */
1141 unsigned char const *salt = handshake->randbytes;
1142 size_t salt_len = 64;
1143
1144#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001148
Hanno Becker35b23c72018-10-23 12:10:41 +01001149 lbl = "extended master secret";
1150 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001151 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1153 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001156 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1157 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001158 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001159 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001160 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001161#endif /* MBEDTLS_SHA512_C */
1162 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001163 }
1164 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001166 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001167
Hanno Becker35b23c72018-10-23 12:10:41 +01001168 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001169 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001170#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1171
Hanno Becker7d0a5692018-10-23 15:26:22 +01001172#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1173 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1174 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1175 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1176 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001177 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001178 /* Perform PSK-to-MS expansion in a single step. */
1179 psa_status_t status;
1180 psa_algorithm_t alg;
1181 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001182 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001183
1184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1185
1186 psk = ssl->conf->psk_opaque;
1187 if( ssl->handshake->psk_opaque != 0 )
1188 psk = ssl->handshake->psk_opaque;
1189
Hanno Becker22bf1452019-04-05 11:21:08 +01001190 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001191 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1192 else
1193 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1194
1195 status = psa_key_derivation( &generator, psk, alg,
1196 salt, salt_len,
1197 (unsigned char const *) lbl,
1198 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001199 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001200 if( status != PSA_SUCCESS )
1201 {
1202 psa_generator_abort( &generator );
1203 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1204 }
1205
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001206 status = psa_generator_read( &generator, session->master,
1207 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001208 if( status != PSA_SUCCESS )
1209 {
1210 psa_generator_abort( &generator );
1211 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1212 }
1213
1214 status = psa_generator_abort( &generator );
1215 if( status != PSA_SUCCESS )
1216 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001217 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001218 else
1219#endif
1220 {
1221 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1222 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001223 session->master,
1224 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001225 if( ret != 0 )
1226 {
1227 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1228 return( ret );
1229 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001230
Hanno Becker7d0a5692018-10-23 15:26:22 +01001231 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1232 handshake->premaster,
1233 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001234
Hanno Becker7d0a5692018-10-23 15:26:22 +01001235 mbedtls_platform_zeroize( handshake->premaster,
1236 sizeof(handshake->premaster) );
1237 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
1240 /*
1241 * Swap the client and server random values.
1242 */
Paul Bakker48916f92012-09-16 19:57:18 +00001243 memcpy( tmp, handshake->randbytes, 64 );
1244 memcpy( handshake->randbytes, tmp + 32, 32 );
1245 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001246 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
1248 /*
1249 * SSLv3:
1250 * key block =
1251 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1252 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1253 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1254 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1255 * ...
1256 *
1257 * TLSv1:
1258 * key block = PRF( master, "key expansion", randbytes )
1259 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001260 ret = handshake->tls_prf( session->master, 48, "key expansion",
1261 handshake->randbytes, 64, keyblk, 256 );
1262 if( ret != 0 )
1263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001265 return( ret );
1266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1269 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1270 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1271 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1272 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 /*
1275 * Determine the appropriate key, IV and MAC length.
1276 */
Paul Bakker68884e32013-01-07 18:20:04 +01001277
Hanno Becker88aaf652017-12-27 08:17:40 +00001278 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001279
Hanno Becker8031d062018-01-03 15:32:31 +00001280#if defined(MBEDTLS_GCM_C) || \
1281 defined(MBEDTLS_CCM_C) || \
1282 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001284 cipher_info->mode == MBEDTLS_MODE_CCM ||
1285 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001287 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001288
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001289 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001290 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001291 transform->taglen =
1292 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001293
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001294 /* All modes haves 96-bit IVs;
1295 * GCM and CCM has 4 implicit and 8 explicit bytes
1296 * ChachaPoly has all 12 bytes implicit
1297 */
Paul Bakker68884e32013-01-07 18:20:04 +01001298 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001299 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1300 transform->fixed_ivlen = 12;
1301 else
1302 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001303
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001304 /* Minimum length of encrypted record */
1305 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001306 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001307 }
1308 else
Hanno Becker8031d062018-01-03 15:32:31 +00001309#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1310#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1311 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1312 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001313 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001314 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1316 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001319 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001320 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001322 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001323 mac_key_len = mbedtls_md_get_size( md_info );
1324 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001327 /*
1328 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1329 * (rfc 6066 page 13 or rfc 2104 section 4),
1330 * so we only need to adjust the length here.
1331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001335
1336#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1337 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001338 * HMAC implementation which also truncates the key
1339 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001340 mac_key_len = transform->maclen;
1341#endif
1342 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001344
1345 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001346 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001347
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001348 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001350 transform->minlen = transform->maclen;
1351 else
Paul Bakker68884e32013-01-07 18:20:04 +01001352 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001353 /*
1354 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001355 * 1. if EtM is in use: one block plus MAC
1356 * otherwise: * first multiple of blocklen greater than maclen
1357 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1360 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001361 {
1362 transform->minlen = transform->maclen
1363 + cipher_info->block_size;
1364 }
1365 else
1366#endif
1367 {
1368 transform->minlen = transform->maclen
1369 + cipher_info->block_size
1370 - transform->maclen % cipher_info->block_size;
1371 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1374 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1375 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001376 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001377 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001378#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1380 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1381 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001382 {
1383 transform->minlen += transform->ivlen;
1384 }
1385 else
1386#endif
1387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001389 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1390 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001391 }
Paul Bakker68884e32013-01-07 18:20:04 +01001392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 }
Hanno Becker8031d062018-01-03 15:32:31 +00001394 else
1395#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1396 {
1397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Hanno Becker88aaf652017-12-27 08:17:40 +00001401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1402 (unsigned) keylen,
1403 (unsigned) transform->minlen,
1404 (unsigned) transform->ivlen,
1405 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
1407 /*
1408 * Finally setup the cipher contexts, IVs and MAC secrets.
1409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001411 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001413 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001414 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Paul Bakker68884e32013-01-07 18:20:04 +01001416 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001417 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001419 /*
1420 * This is not used in TLS v1.1.
1421 */
Paul Bakker48916f92012-09-16 19:57:18 +00001422 iv_copy_len = ( transform->fixed_ivlen ) ?
1423 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001424 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1425 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001426 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 }
1428 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#endif /* MBEDTLS_SSL_CLI_C */
1430#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001431 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001433 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001434 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
Hanno Becker81c7b182017-11-09 18:39:33 +00001436 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001437 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001438
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001439 /*
1440 * This is not used in TLS v1.1.
1441 */
Paul Bakker48916f92012-09-16 19:57:18 +00001442 iv_copy_len = ( transform->fixed_ivlen ) ?
1443 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001444 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1445 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001446 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001448 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001452 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1453 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001454 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001455
Hanno Beckerd56ed242018-01-03 15:32:51 +00001456#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_SSL_PROTO_SSL3)
1458 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001459 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001460 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001463 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1464 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001465 }
1466
Hanno Becker81c7b182017-11-09 18:39:33 +00001467 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1468 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001469 }
1470 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1472#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1473 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1474 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001475 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001476 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1477 For AEAD-based ciphersuites, there is nothing to do here. */
1478 if( mac_key_len != 0 )
1479 {
1480 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1481 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1482 }
Paul Bakker68884e32013-01-07 18:20:04 +01001483 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001484 else
1485#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001488 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1489 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001490 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001491#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1494 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001495 {
1496 int ret = 0;
1497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001499
Hanno Becker88aaf652017-12-27 08:17:40 +00001500 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001501 transform->iv_enc, transform->iv_dec,
1502 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001503 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001504 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001507 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1508 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001509 }
1510 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001511#else
1512 ((void) mac_dec);
1513 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001515
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001516#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1517 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001518 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001519 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1520 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001521 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001522 iv_copy_len );
1523 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001524
1525 if( ssl->conf->f_export_keys_ext != NULL )
1526 {
1527 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1528 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001529 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001530 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001531 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001532 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001533 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001534 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001535#endif
1536
Hanno Beckerf704bef2018-11-16 15:21:18 +00001537#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001538
1539 /* Only use PSA-based ciphers for TLS-1.2.
1540 * That's relevant at least for TLS-1.0, where
1541 * we assume that mbedtls_cipher_crypt() updates
1542 * the structure field for the IV, which the PSA-based
1543 * implementation currently doesn't. */
1544#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1545 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001546 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001547 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001548 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001549 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1550 {
1551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001552 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001553 }
1554
1555 if( ret == 0 )
1556 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001558 psa_fallthrough = 0;
1559 }
1560 else
1561 {
1562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1563 psa_fallthrough = 1;
1564 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001565 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001566 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001567 psa_fallthrough = 1;
1568#else
1569 psa_fallthrough = 1;
1570#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001571
Hanno Beckercb1cc802018-11-17 22:27:38 +00001572 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001573#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001574 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001575 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001576 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001578 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001580
Hanno Beckerf704bef2018-11-16 15:21:18 +00001581#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001582 /* Only use PSA-based ciphers for TLS-1.2.
1583 * That's relevant at least for TLS-1.0, where
1584 * we assume that mbedtls_cipher_crypt() updates
1585 * the structure field for the IV, which the PSA-based
1586 * implementation currently doesn't. */
1587#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1588 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001589 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001590 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001591 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001592 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1593 {
1594 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001595 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001596 }
1597
1598 if( ret == 0 )
1599 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001601 psa_fallthrough = 0;
1602 }
1603 else
1604 {
1605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1606 psa_fallthrough = 1;
1607 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001608 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001609 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001610 psa_fallthrough = 1;
1611#else
1612 psa_fallthrough = 1;
1613#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001614
Hanno Beckercb1cc802018-11-17 22:27:38 +00001615 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001616#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001617 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001618 cipher_info ) ) != 0 )
1619 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001621 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001622 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001625 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001629 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001630 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001633 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001637 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001638 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#if defined(MBEDTLS_CIPHER_MODE_CBC)
1641 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1644 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001647 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001648 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1651 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001654 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001661 // Initialize compression
1662 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001664 {
Paul Bakker16770332013-10-11 09:59:44 +02001665 if( ssl->compress_buf == NULL )
1666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001668 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001669 if( ssl->compress_buf == NULL )
1670 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001672 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001673 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1674 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001675 }
1676 }
1677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001679
Paul Bakker48916f92012-09-16 19:57:18 +00001680 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1681 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001682
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001683 if( deflateInit( &transform->ctx_deflate,
1684 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001685 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001688 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1689 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001690 }
1691 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001695end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001696 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1697 mbedtls_platform_zeroize( handshake->randbytes,
1698 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001699 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001700}
1701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#if defined(MBEDTLS_SSL_PROTO_SSL3)
1703void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001704{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001705 mbedtls_md5_context md5;
1706 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 unsigned char pad_1[48];
1708 unsigned char pad_2[48];
1709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001711
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001712 mbedtls_md5_init( &md5 );
1713 mbedtls_sha1_init( &sha1 );
1714
1715 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1716 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001717
Paul Bakker380da532012-04-18 16:10:25 +00001718 memset( pad_1, 0x36, 48 );
1719 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001721 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1722 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1723 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001724
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001725 mbedtls_md5_starts_ret( &md5 );
1726 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1727 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1728 mbedtls_md5_update_ret( &md5, hash, 16 );
1729 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001731 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1732 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1733 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001734
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001735 mbedtls_sha1_starts_ret( &sha1 );
1736 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1737 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1738 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1739 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001743
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001744 mbedtls_md5_free( &md5 );
1745 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001746
Paul Bakker380da532012-04-18 16:10:25 +00001747 return;
1748}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1752void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001753{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001754 mbedtls_md5_context md5;
1755 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001758
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001759 mbedtls_md5_init( &md5 );
1760 mbedtls_sha1_init( &sha1 );
1761
1762 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1763 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001764
Andrzej Kurekeb342242019-01-29 09:14:33 -05001765 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001766 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001770
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001771 mbedtls_md5_free( &md5 );
1772 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001773
Paul Bakker380da532012-04-18 16:10:25 +00001774 return;
1775}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1779#if defined(MBEDTLS_SHA256_C)
1780void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001781{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001782#if defined(MBEDTLS_USE_PSA_CRYPTO)
1783 size_t hash_size;
1784 psa_status_t status;
1785 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1786
1787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1788 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1789 if( status != PSA_SUCCESS )
1790 {
1791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1792 return;
1793 }
1794
1795 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1796 if( status != PSA_SUCCESS )
1797 {
1798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1799 return;
1800 }
1801 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1803#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001804 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001805
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001806 mbedtls_sha256_init( &sha256 );
1807
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001809
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001810 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001811 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001815
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001816 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001817#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001818 return;
1819}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#if defined(MBEDTLS_SHA512_C)
1823void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001824{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001825#if defined(MBEDTLS_USE_PSA_CRYPTO)
1826 size_t hash_size;
1827 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001828 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001829
1830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001831 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001832 if( status != PSA_SUCCESS )
1833 {
1834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1835 return;
1836 }
1837
Andrzej Kurek972fba52019-01-30 03:29:12 -05001838 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001839 if( status != PSA_SUCCESS )
1840 {
1841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1842 return;
1843 }
1844 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1846#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001847 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001848
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001849 mbedtls_sha512_init( &sha512 );
1850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001852
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001853 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001854 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001859 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001860#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001861 return;
1862}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#endif /* MBEDTLS_SHA512_C */
1864#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1867int 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 +02001868{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001869 unsigned char *p = ssl->handshake->premaster;
1870 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001871 const unsigned char *psk = ssl->conf->psk;
1872 size_t psk_len = ssl->conf->psk_len;
1873
1874 /* If the psk callback was called, use its result */
1875 if( ssl->handshake->psk != NULL )
1876 {
1877 psk = ssl->handshake->psk;
1878 psk_len = ssl->handshake->psk_len;
1879 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001880
1881 /*
1882 * PMS = struct {
1883 * opaque other_secret<0..2^16-1>;
1884 * opaque psk<0..2^16-1>;
1885 * };
1886 * with "other_secret" depending on the particular key exchange
1887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1889 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001890 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001891 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001893
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001894 *(p++) = (unsigned char)( psk_len >> 8 );
1895 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001896
1897 if( end < p || (size_t)( end - p ) < psk_len )
1898 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1899
1900 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001901 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001902 }
1903 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1905#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1906 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001907 {
1908 /*
1909 * other_secret already set by the ClientKeyExchange message,
1910 * and is 48 bytes long
1911 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001912 if( end - p < 2 )
1913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1914
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001915 *p++ = 0;
1916 *p++ = 48;
1917 p += 48;
1918 }
1919 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1921#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1922 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001923 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001924 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001925 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001926
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001927 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001929 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001930 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001933 return( ret );
1934 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001935 *(p++) = (unsigned char)( len >> 8 );
1936 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001937 p += len;
1938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001940 }
1941 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1943#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1944 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001945 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001946 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001947 size_t zlen;
1948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001950 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001951 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001954 return( ret );
1955 }
1956
1957 *(p++) = (unsigned char)( zlen >> 8 );
1958 *(p++) = (unsigned char)( zlen );
1959 p += zlen;
1960
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001961 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1962 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001963 }
1964 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001969 }
1970
1971 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001972 if( end - p < 2 )
1973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001974
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001975 *(p++) = (unsigned char)( psk_len >> 8 );
1976 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001977
1978 if( end < p || (size_t)( end - p ) < psk_len )
1979 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1980
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001981 memcpy( p, psk, psk_len );
1982 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001983
1984 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1985
1986 return( 0 );
1987}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001991/*
1992 * SSLv3.0 MAC functions
1993 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001994#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001995static void ssl_mac( mbedtls_md_context_t *md_ctx,
1996 const unsigned char *secret,
1997 const unsigned char *buf, size_t len,
1998 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001999 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002000{
2001 unsigned char header[11];
2002 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002003 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2005 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002006
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002007 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002009 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002010 else
Paul Bakker68884e32013-01-07 18:20:04 +01002011 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002012
2013 memcpy( header, ctr, 8 );
2014 header[ 8] = (unsigned char) type;
2015 header[ 9] = (unsigned char)( len >> 8 );
2016 header[10] = (unsigned char)( len );
2017
Paul Bakker68884e32013-01-07 18:20:04 +01002018 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 mbedtls_md_starts( md_ctx );
2020 mbedtls_md_update( md_ctx, secret, md_size );
2021 mbedtls_md_update( md_ctx, padding, padlen );
2022 mbedtls_md_update( md_ctx, header, 11 );
2023 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002024 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002025
Paul Bakker68884e32013-01-07 18:20:04 +01002026 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 mbedtls_md_starts( md_ctx );
2028 mbedtls_md_update( md_ctx, secret, md_size );
2029 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002030 mbedtls_md_update( md_ctx, out, md_size );
2031 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002032}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002034
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002035/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002036 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002037#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002038 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2039 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2040 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2041/* This function makes sure every byte in the memory region is accessed
2042 * (in ascending addresses order) */
2043static void ssl_read_memory( unsigned char *p, size_t len )
2044{
2045 unsigned char acc = 0;
2046 volatile unsigned char force;
2047
2048 for( ; len != 0; p++, len-- )
2049 acc ^= *p;
2050
2051 force = acc;
2052 (void) force;
2053}
2054#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2055
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002056/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002058 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002059
Hanno Beckera0e20d02019-05-15 14:03:01 +01002060#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002061/* This functions transforms a DTLS plaintext fragment and a record content
2062 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002063 *
2064 * struct {
2065 * opaque content[DTLSPlaintext.length];
2066 * ContentType real_type;
2067 * uint8 zeros[length_of_padding];
2068 * } DTLSInnerPlaintext;
2069 *
2070 * Input:
2071 * - `content`: The beginning of the buffer holding the
2072 * plaintext to be wrapped.
2073 * - `*content_size`: The length of the plaintext in Bytes.
2074 * - `max_len`: The number of Bytes available starting from
2075 * `content`. This must be `>= *content_size`.
2076 * - `rec_type`: The desired record content type.
2077 *
2078 * Output:
2079 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2080 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2081 *
2082 * Returns:
2083 * - `0` on success.
2084 * - A negative error code if `max_len` didn't offer enough space
2085 * for the expansion.
2086 */
2087static int ssl_cid_build_inner_plaintext( unsigned char *content,
2088 size_t *content_size,
2089 size_t remaining,
2090 uint8_t rec_type )
2091{
2092 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002093 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2094 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2095 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002096
2097 /* Write real content type */
2098 if( remaining == 0 )
2099 return( -1 );
2100 content[ len ] = rec_type;
2101 len++;
2102 remaining--;
2103
2104 if( remaining < pad )
2105 return( -1 );
2106 memset( content + len, 0, pad );
2107 len += pad;
2108 remaining -= pad;
2109
2110 *content_size = len;
2111 return( 0 );
2112}
2113
Hanno Becker07dc97d2019-05-20 15:08:01 +01002114/* This function parses a DTLSInnerPlaintext structure.
2115 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002116static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2117 size_t *content_size,
2118 uint8_t *rec_type )
2119{
2120 size_t remaining = *content_size;
2121
2122 /* Determine length of padding by skipping zeroes from the back. */
2123 do
2124 {
2125 if( remaining == 0 )
2126 return( -1 );
2127 remaining--;
2128 } while( content[ remaining ] == 0 );
2129
2130 *content_size = remaining;
2131 *rec_type = content[ remaining ];
2132
2133 return( 0 );
2134}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002135#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002136
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002137/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002138 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002139static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002140 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002141 mbedtls_record *rec )
2142{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002143 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002144 *
2145 * additional_data = seq_num + TLSCompressed.type +
2146 * TLSCompressed.version + TLSCompressed.length;
2147 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002148 * For the CID extension, this is extended as follows
2149 * (quoting draft-ietf-tls-dtls-connection-id-05,
2150 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002151 *
2152 * additional_data = seq_num + DTLSPlaintext.type +
2153 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002154 * cid +
2155 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002156 * length_of_DTLSInnerPlaintext;
2157 */
2158
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002159 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2160 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002161 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002162
Hanno Beckera0e20d02019-05-15 14:03:01 +01002163#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002164 if( rec->cid_len != 0 )
2165 {
2166 memcpy( add_data + 11, rec->cid, rec->cid_len );
2167 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2168 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2169 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2170 *add_data_len = 13 + 1 + rec->cid_len;
2171 }
2172 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002173#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002174 {
2175 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2176 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2177 *add_data_len = 13;
2178 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002179}
2180
Hanno Beckera18d1322018-01-03 14:27:32 +00002181int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2182 mbedtls_ssl_transform *transform,
2183 mbedtls_record *rec,
2184 int (*f_rng)(void *, unsigned char *, size_t),
2185 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002188 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002189 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002190 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002191 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002192 size_t post_avail;
2193
2194 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002195#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002196 ((void) ssl);
2197#endif
2198
2199 /* The PRNG is used for dynamic IV generation that's used
2200 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2201#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2202 ( defined(MBEDTLS_AES_C) || \
2203 defined(MBEDTLS_ARIA_C) || \
2204 defined(MBEDTLS_CAMELLIA_C) ) && \
2205 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2206 ((void) f_rng);
2207 ((void) p_rng);
2208#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002212 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002213 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2216 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002217 if( rec == NULL
2218 || rec->buf == NULL
2219 || rec->buf_len < rec->data_offset
2220 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002221#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002222 || rec->cid_len != 0
2223#endif
2224 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002225 {
2226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002228 }
2229
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002230 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002231 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233 data, rec->data_len );
2234
2235 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2236
2237 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2238 {
2239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2240 (unsigned) rec->data_len,
2241 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2243 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002244
Hanno Beckera0e20d02019-05-15 14:03:01 +01002245#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002246 /*
2247 * Add CID information
2248 */
2249 rec->cid_len = transform->out_cid_len;
2250 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2251 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002252
2253 if( rec->cid_len != 0 )
2254 {
2255 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002256 * Wrap plaintext into DTLSInnerPlaintext structure.
2257 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002258 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002259 * Note that this changes `rec->data_len`, and hence
2260 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002261 */
2262 if( ssl_cid_build_inner_plaintext( data,
2263 &rec->data_len,
2264 post_avail,
2265 rec->type ) != 0 )
2266 {
2267 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2268 }
2269
2270 rec->type = MBEDTLS_SSL_MSG_CID;
2271 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002272#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002273
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002274 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2275
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002277 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 */
Hanno Becker52344c22018-01-03 15:24:20 +00002279#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 if( mode == MBEDTLS_MODE_STREAM ||
2281 ( mode == MBEDTLS_MODE_CBC
2282#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002283 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002284#endif
2285 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002287 if( post_avail < transform->maclen )
2288 {
2289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2290 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2291 }
2292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002294 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002295 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002296 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002297 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2298 data, rec->data_len, rec->ctr, rec->type, mac );
2299 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002300 }
2301 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2304 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002305 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002306 {
Hanno Becker992b6872017-11-09 18:57:39 +00002307 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2308
Hanno Beckercab87e62019-04-29 13:52:53 +01002309 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002310
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002311 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002312 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002313 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2314 data, rec->data_len );
2315 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2316 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2317
2318 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002319 }
2320 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002321#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2324 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002325 }
2326
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002327 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2328 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002329
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002330 rec->data_len += transform->maclen;
2331 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002332 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002333 }
Hanno Becker52344c22018-01-03 15:24:20 +00002334#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002335
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002336 /*
2337 * Encrypt
2338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2340 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002341 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002342 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002343 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002345 "including %d bytes of padding",
2346 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002347
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002348 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2349 transform->iv_enc, transform->ivlen,
2350 data, rec->data_len,
2351 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002354 return( ret );
2355 }
2356
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002357 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2360 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002361 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002362 }
Paul Bakker68884e32013-01-07 18:20:04 +01002363 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002365
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002366#if defined(MBEDTLS_GCM_C) || \
2367 defined(MBEDTLS_CCM_C) || \
2368 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002370 mode == MBEDTLS_MODE_CCM ||
2371 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002372 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002373 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002374 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002375 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002376
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002377 /* Check that there's space for both the authentication tag
2378 * and the explicit IV before and after the record content. */
2379 if( post_avail < transform->taglen ||
2380 rec->data_offset < explicit_iv_len )
2381 {
2382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2383 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2384 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002385
Paul Bakker68884e32013-01-07 18:20:04 +01002386 /*
2387 * Generate IV
2388 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002389 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2390 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002391 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002392 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002393 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2394 explicit_iv_len );
2395 /* Prefix record content with explicit IV. */
2396 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002397 }
2398 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2399 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002400 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002401 unsigned char i;
2402
2403 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2404
2405 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002406 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002407 }
2408 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002409 {
2410 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2412 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002413 }
2414
Hanno Beckercab87e62019-04-29 13:52:53 +01002415 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002416
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002417 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2418 iv, transform->ivlen );
2419 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002420 data - explicit_iv_len, explicit_iv_len );
2421 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002422 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002424 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002425 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002426
Paul Bakker68884e32013-01-07 18:20:04 +01002427 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002428 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002429 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002430
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002431 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002432 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002433 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002434 data, rec->data_len, /* source */
2435 data, &rec->data_len, /* destination */
2436 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002439 return( ret );
2440 }
2441
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002442 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2443 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002444
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002445 rec->data_len += transform->taglen + explicit_iv_len;
2446 rec->data_offset -= explicit_iv_len;
2447 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002448 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002449 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2452#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002453 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002456 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002457 size_t padlen, i;
2458 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002459
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002460 /* Currently we're always using minimal padding
2461 * (up to 255 bytes would be allowed). */
2462 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2463 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 padlen = 0;
2465
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002466 /* Check there's enough space in the buffer for the padding. */
2467 if( post_avail < padlen + 1 )
2468 {
2469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2470 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2471 }
2472
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002474 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002475
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002476 rec->data_len += padlen + 1;
2477 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002480 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002481 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2482 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002483 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002484 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002485 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002486 if( f_rng == NULL )
2487 {
2488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2490 }
2491
2492 if( rec->data_offset < transform->ivlen )
2493 {
2494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2495 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2496 }
2497
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002498 /*
2499 * Generate IV
2500 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002501 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002502 if( ret != 0 )
2503 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002504
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002505 memcpy( data - transform->ivlen, transform->iv_enc,
2506 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002507
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002508 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002512 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002513 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002514 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002516 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2517 transform->iv_enc,
2518 transform->ivlen,
2519 data, rec->data_len,
2520 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002523 return( ret );
2524 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002525
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002526 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002530 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002533 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002534 {
2535 /*
2536 * Save IV in SSL3 and TLS1
2537 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002538 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2539 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002541 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002542#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002543 {
2544 data -= transform->ivlen;
2545 rec->data_offset -= transform->ivlen;
2546 rec->data_len += transform->ivlen;
2547 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002550 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002551 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002552 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2553
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002554 /*
2555 * MAC(MAC_write_key, seq_num +
2556 * TLSCipherText.type +
2557 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002558 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002559 * IV + // except for TLS 1.0
2560 * ENC(content + padding + padding_length));
2561 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002562
2563 if( post_avail < transform->maclen)
2564 {
2565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2566 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2567 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002568
Hanno Beckercab87e62019-04-29 13:52:53 +01002569 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002572 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002573 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002574
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002575 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002576 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002577 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2578 data, rec->data_len );
2579 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2580 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002581
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002582 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002583
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002584 rec->data_len += transform->maclen;
2585 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002586 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002587 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002590 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002592 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002598 /* Make extra sure authentication was performed, exactly once */
2599 if( auth_done != 1 )
2600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2602 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002603 }
2604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
2607 return( 0 );
2608}
2609
Hanno Becker605949f2019-07-12 08:23:59 +01002610int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002611 mbedtls_ssl_transform *transform,
2612 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002613{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002614 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002616 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002617#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002618 size_t padlen = 0, correct = 1;
2619#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002620 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002621 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002622 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002623
Hanno Beckera18d1322018-01-03 14:27:32 +00002624#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002625 ((void) ssl);
2626#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002629 if( rec == NULL ||
2630 rec->buf == NULL ||
2631 rec->buf_len < rec->data_offset ||
2632 rec->buf_len - rec->data_offset < rec->data_len )
2633 {
2634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002636 }
2637
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002638 data = rec->buf + rec->data_offset;
2639 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Hanno Beckera0e20d02019-05-15 14:03:01 +01002641#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002642 /*
2643 * Match record's CID with incoming CID.
2644 */
Hanno Becker938489a2019-05-08 13:02:22 +01002645 if( rec->cid_len != transform->in_cid_len ||
2646 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2647 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002648 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002649 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002650#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2653 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002654 {
2655 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002656 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2657 transform->iv_dec,
2658 transform->ivlen,
2659 data, rec->data_len,
2660 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002663 return( ret );
2664 }
2665
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002666 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2669 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 }
Paul Bakker68884e32013-01-07 18:20:04 +01002672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002674#if defined(MBEDTLS_GCM_C) || \
2675 defined(MBEDTLS_CCM_C) || \
2676 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002678 mode == MBEDTLS_MODE_CCM ||
2679 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002680 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002681 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002682 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002683
Hanno Beckerd96a6522019-07-10 13:55:25 +01002684 /*
2685 * Prepare IV from explicit and implicit data.
2686 */
2687
2688 /* Check that there's enough space for the explicit IV
2689 * (at the beginning of the record) and the MAC (at the
2690 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002691 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002694 "+ taglen (%d)", rec->data_len,
2695 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002697 }
Paul Bakker68884e32013-01-07 18:20:04 +01002698
Hanno Beckerd96a6522019-07-10 13:55:25 +01002699#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002700 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2701 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002702 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002703
Hanno Beckerd96a6522019-07-10 13:55:25 +01002704 /* Fixed */
2705 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2706 /* Explicit */
2707 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002708 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002709 else
2710#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2711#if defined(MBEDTLS_CHACHAPOLY_C)
2712 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002713 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002714 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002715 unsigned char i;
2716
2717 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2718
2719 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002720 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002721 }
2722 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002723#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002724 {
2725 /* Reminder if we ever add an AEAD mode with a different size */
2726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2728 }
2729
Hanno Beckerd96a6522019-07-10 13:55:25 +01002730 /* Group changes to data, data_len, and add_data, because
2731 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002732 data += explicit_iv_len;
2733 rec->data_offset += explicit_iv_len;
2734 rec->data_len -= explicit_iv_len + transform->taglen;
2735
Hanno Beckercab87e62019-04-29 13:52:53 +01002736 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002737 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002738 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002739
Hanno Beckerd96a6522019-07-10 13:55:25 +01002740 /* Because of the check above, we know that there are
2741 * explicit_iv_len Bytes preceeding data, and taglen
2742 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002743 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002744 * mbedtls_cipher_auth_decrypt() below. */
2745
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002746 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002747 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002748 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002749
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002750 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002751 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002752 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002753 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2754 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002755 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002756 data, rec->data_len,
2757 data, &olen,
2758 data + rec->data_len,
2759 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2764 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002765
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002766 return( ret );
2767 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002768 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002769
Hanno Beckerd96a6522019-07-10 13:55:25 +01002770 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002771 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2774 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002775 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002776 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2779#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002780 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002782 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002783 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002784
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 /*
Paul Bakker45829992013-01-03 14:52:21 +01002786 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002789 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2790 {
2791 /* The ciphertext is prefixed with the CBC IV. */
2792 minlen += transform->ivlen;
2793 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794#endif
Paul Bakker45829992013-01-03 14:52:21 +01002795
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002796 /* Size considerations:
2797 *
2798 * - The CBC cipher text must not be empty and hence
2799 * at least of size transform->ivlen.
2800 *
2801 * Together with the potential IV-prefix, this explains
2802 * the first of the two checks below.
2803 *
2804 * - The record must contain a MAC, either in plain or
2805 * encrypted, depending on whether Encrypt-then-MAC
2806 * is used or not.
2807 * - If it is, the message contains the IV-prefix,
2808 * the CBC ciphertext, and the MAC.
2809 * - If it is not, the padded plaintext, and hence
2810 * the CBC ciphertext, has at least length maclen + 1
2811 * because there is at least the padding length byte.
2812 *
2813 * As the CBC ciphertext is not empty, both cases give the
2814 * lower bound minlen + maclen + 1 on the record size, which
2815 * we test for in the second check below.
2816 */
2817 if( rec->data_len < minlen + transform->ivlen ||
2818 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002821 "+ 1 ) ( + expl IV )", rec->data_len,
2822 transform->ivlen,
2823 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002825 }
2826
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002827 /*
2828 * Authenticate before decrypt if enabled
2829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002831 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002832 {
Hanno Becker992b6872017-11-09 18:57:39 +00002833 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002836
Hanno Beckerd96a6522019-07-10 13:55:25 +01002837 /* Update data_len in tandem with add_data.
2838 *
2839 * The subtraction is safe because of the previous check
2840 * data_len >= minlen + maclen + 1.
2841 *
2842 * Afterwards, we know that data + data_len is followed by at
2843 * least maclen Bytes, which justifies the call to
2844 * mbedtls_ssl_safer_memcmp() below.
2845 *
2846 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002847 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002848 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002849
Hanno Beckerd96a6522019-07-10 13:55:25 +01002850 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002851 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2852 add_data_len );
2853 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2854 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002855 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2856 data, rec->data_len );
2857 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2858 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002859
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002860 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2861 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002862 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002863 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002864
Hanno Beckerd96a6522019-07-10 13:55:25 +01002865 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002866 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2867 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002871 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002872 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002873 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002875
2876 /*
2877 * Check length sanity
2878 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002879
2880 /* We know from above that data_len > minlen >= 0,
2881 * so the following check in particular implies that
2882 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002883 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002886 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002888 }
2889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002891 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002892 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002893 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002894 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002895 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002896 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002897 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002898
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002899 data += transform->ivlen;
2900 rec->data_offset += transform->ivlen;
2901 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002902 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002904
Hanno Beckerd96a6522019-07-10 13:55:25 +01002905 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2906
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002907 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2908 transform->iv_dec, transform->ivlen,
2909 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002912 return( ret );
2913 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002914
Hanno Beckerd96a6522019-07-10 13:55:25 +01002915 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002916 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002920 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002923 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002924 {
2925 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002926 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
2927 * records is equivalent to CBC decryption of the concatenation
2928 * of the records; in other words, IVs are maintained across
2929 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02002930 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002931 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2932 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002934#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002936 /* Safe since data_len >= minlen + maclen + 1, so after having
2937 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01002938 * data_len > 0 (because of data_len % ivlen == 0, it's actually
2939 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002940 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002941
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002942 if( auth_done == 1 )
2943 {
2944 correct *= ( rec->data_len >= padlen + 1 );
2945 padlen *= ( rec->data_len >= padlen + 1 );
2946 }
2947 else
Paul Bakker45829992013-01-03 14:52:21 +01002948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002950 if( rec->data_len < transform->maclen + padlen + 1 )
2951 {
2952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2953 rec->data_len,
2954 transform->maclen,
2955 padlen + 1 ) );
2956 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002957#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002958
2959 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2960 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002963 padlen++;
2964
2965 /* Regardless of the validity of the padding,
2966 * we have data_len >= padlen here. */
2967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002969 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002970 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002971 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973#if defined(MBEDTLS_SSL_DEBUG_ALL)
2974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002975 "should be no more than %d",
2976 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002977#endif
Paul Bakker45829992013-01-03 14:52:21 +01002978 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 }
2980 }
2981 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2983#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2984 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002985 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002986 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002987 /* The padding check involves a series of up to 256
2988 * consecutive memory reads at the end of the record
2989 * plaintext buffer. In order to hide the length and
2990 * validity of the padding, always perform exactly
2991 * `min(256,plaintext_len)` reads (but take into account
2992 * only the last `padlen` bytes for the padding check). */
2993 size_t pad_count = 0;
2994 size_t real_count = 0;
2995 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002996
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002997 /* Index of first padding byte; it has been ensured above
2998 * that the subtraction is safe. */
2999 size_t const padding_idx = rec->data_len - padlen;
3000 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3001 size_t const start_idx = rec->data_len - num_checks;
3002 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003003
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003004 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003005 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003006 real_count |= ( idx >= padding_idx );
3007 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003008 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003009 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003012 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003014#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003015 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003016 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003017 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3019 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003023 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003024
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003025 /* If the padding was found to be invalid, padlen == 0
3026 * and the subtraction is safe. If the padding was found valid,
3027 * padlen hasn't been changed and the previous assertion
3028 * data_len >= padlen still holds. */
3029 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003030 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003031 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003033 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003037 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003039#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003041 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
3044 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003045 * Authenticate if not done yet.
3046 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 */
Hanno Becker52344c22018-01-03 15:24:20 +00003048#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003049 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003050 {
Hanno Becker992b6872017-11-09 18:57:39 +00003051 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003052
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003053 /* If the initial value of padlen was such that
3054 * data_len < maclen + padlen + 1, then padlen
3055 * got reset to 1, and the initial check
3056 * data_len >= minlen + maclen + 1
3057 * guarantees that at this point we still
3058 * have at least data_len >= maclen.
3059 *
3060 * If the initial value of padlen was such that
3061 * data_len >= maclen + padlen + 1, then we have
3062 * subtracted either padlen + 1 (if the padding was correct)
3063 * or 0 (if the padding was incorrect) since then,
3064 * hence data_len >= maclen in any case.
3065 */
3066 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003067 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003070 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003071 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003072 ssl_mac( &transform->md_ctx_dec,
3073 transform->mac_dec,
3074 data, rec->data_len,
3075 rec->ctr, rec->type,
3076 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003077 }
3078 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3080#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3081 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003082 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003083 {
3084 /*
3085 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003086 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003087 *
3088 * Known timing attacks:
3089 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3090 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003091 * To compensate for different timings for the MAC calculation
3092 * depending on how much padding was removed (which is determined
3093 * by padlen), process extra_run more blocks through the hash
3094 * function.
3095 *
3096 * The formula in the paper is
3097 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3098 * where L1 is the size of the header plus the decrypted message
3099 * plus CBC padding and L2 is the size of the header plus the
3100 * decrypted message. This is for an underlying hash function
3101 * with 64-byte blocks.
3102 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3103 * correctly. We round down instead of up, so -56 is the correct
3104 * value for our calculations instead of -55.
3105 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003106 * Repeat the formula rather than defining a block_size variable.
3107 * This avoids requiring division by a variable at runtime
3108 * (which would be marginally less efficient and would require
3109 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003110 */
3111 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003112 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003113
3114 /*
3115 * The next two sizes are the minimum and maximum values of
3116 * in_msglen over all padlen values.
3117 *
3118 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003119 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003120 *
3121 * Note that max_len + maclen is never more than the buffer
3122 * length, as we previously did in_msglen -= maclen too.
3123 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003124 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003125 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3126
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003127 memset( tmp, 0, sizeof( tmp ) );
3128
3129 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003130 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003131#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3132 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003133 case MBEDTLS_MD_MD5:
3134 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003135 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003136 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003137 extra_run =
3138 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3139 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003140 break;
3141#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003142#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003143 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003144 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003145 extra_run =
3146 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3147 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003148 break;
3149#endif
3150 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003152 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3153 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003154
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003155 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003156
Hanno Beckercab87e62019-04-29 13:52:53 +01003157 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3158 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003159 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3160 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003161 /* Make sure we access everything even when padlen > 0. This
3162 * makes the synchronisation requirements for just-in-time
3163 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003164 ssl_read_memory( data + rec->data_len, padlen );
3165 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003166
3167 /* Call mbedtls_md_process at least once due to cache attacks
3168 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003169 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003170 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003171
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003172 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003173
3174 /* Make sure we access all the memory that could contain the MAC,
3175 * before we check it in the next code block. This makes the
3176 * synchronisation requirements for just-in-time Prime+Probe
3177 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003178 ssl_read_memory( data + min_len,
3179 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003180 }
3181 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3183 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3186 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003188
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003189#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003190 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3191 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003192#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003193
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003194 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3195 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197#if defined(MBEDTLS_SSL_DEBUG_ALL)
3198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003199#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003200 correct = 0;
3201 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003202 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003203 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003204
3205 /*
3206 * Finally check the correct flag
3207 */
3208 if( correct == 0 )
3209 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003210#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003211
3212 /* Make extra sure authentication was performed, exactly once */
3213 if( auth_done != 1 )
3214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3216 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003217 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003218
Hanno Beckera0e20d02019-05-15 14:03:01 +01003219#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003220 if( rec->cid_len != 0 )
3221 {
3222 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3223 &rec->type );
3224 if( ret != 0 )
3225 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3226 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003227#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003230
3231 return( 0 );
3232}
3233
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003234#undef MAC_NONE
3235#undef MAC_PLAINTEXT
3236#undef MAC_CIPHERTEXT
3237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003239/*
3240 * Compression/decompression functions
3241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003243{
3244 int ret;
3245 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003246 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003247 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003248 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003251
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003252 if( len_pre == 0 )
3253 return( 0 );
3254
Paul Bakker2770fbd2012-07-03 13:30:23 +00003255 memcpy( msg_pre, ssl->out_msg, len_pre );
3256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003258 ssl->out_msglen ) );
3259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003261 ssl->out_msg, ssl->out_msglen );
3262
Paul Bakker48916f92012-09-16 19:57:18 +00003263 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3264 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3265 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003266 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003267
Paul Bakker48916f92012-09-16 19:57:18 +00003268 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003269 if( ret != Z_OK )
3270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3272 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003273 }
3274
Angus Grattond8213d02016-05-25 20:56:48 +10003275 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003276 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003279 ssl->out_msglen ) );
3280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003282 ssl->out_msg, ssl->out_msglen );
3283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003285
3286 return( 0 );
3287}
3288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003290{
3291 int ret;
3292 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003293 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003294 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003295 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003298
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003299 if( len_pre == 0 )
3300 return( 0 );
3301
Paul Bakker2770fbd2012-07-03 13:30:23 +00003302 memcpy( msg_pre, ssl->in_msg, len_pre );
3303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003305 ssl->in_msglen ) );
3306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003308 ssl->in_msg, ssl->in_msglen );
3309
Paul Bakker48916f92012-09-16 19:57:18 +00003310 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3311 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3312 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003313 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003314 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003315
Paul Bakker48916f92012-09-16 19:57:18 +00003316 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003317 if( ret != Z_OK )
3318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3320 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003321 }
3322
Angus Grattond8213d02016-05-25 20:56:48 +10003323 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003324 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003327 ssl->in_msglen ) );
3328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003330 ssl->in_msg, ssl->in_msglen );
3331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003333
3334 return( 0 );
3335}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3339static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341#if defined(MBEDTLS_SSL_PROTO_DTLS)
3342static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003343{
3344 /* If renegotiation is not enforced, retransmit until we would reach max
3345 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003346 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003347 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003348 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003349 unsigned char doublings = 1;
3350
3351 while( ratio != 0 )
3352 {
3353 ++doublings;
3354 ratio >>= 1;
3355 }
3356
3357 if( ++ssl->renego_records_seen > doublings )
3358 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003360 return( 0 );
3361 }
3362 }
3363
3364 return( ssl_write_hello_request( ssl ) );
3365}
3366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003368
Paul Bakker5121ce52009-01-03 21:22:43 +00003369/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003370 * Fill the input message buffer by appending data to it.
3371 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003372 *
3373 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3374 * available (from this read and/or a previous one). Otherwise, an error code
3375 * is returned (possibly EOF or WANT_READ).
3376 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003377 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3378 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3379 * since we always read a whole datagram at once.
3380 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003381 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003382 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003385{
Paul Bakker23986e52011-04-24 08:57:21 +00003386 int ret;
3387 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003390
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003391 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003394 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003396 }
3397
Angus Grattond8213d02016-05-25 20:56:48 +10003398 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003402 }
3403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003405 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003406 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003407 uint32_t timeout;
3408
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003409 /* Just to be sure */
3410 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3411 {
3412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3413 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3415 }
3416
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003417 /*
3418 * The point is, we need to always read a full datagram at once, so we
3419 * sometimes read more then requested, and handle the additional data.
3420 * It could be the rest of the current record (while fetching the
3421 * header) and/or some other records in the same datagram.
3422 */
3423
3424 /*
3425 * Move to the next record in the already read datagram if applicable
3426 */
3427 if( ssl->next_record_offset != 0 )
3428 {
3429 if( ssl->in_left < ssl->next_record_offset )
3430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3432 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003433 }
3434
3435 ssl->in_left -= ssl->next_record_offset;
3436
3437 if( ssl->in_left != 0 )
3438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003440 ssl->next_record_offset ) );
3441 memmove( ssl->in_hdr,
3442 ssl->in_hdr + ssl->next_record_offset,
3443 ssl->in_left );
3444 }
3445
3446 ssl->next_record_offset = 0;
3447 }
3448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003451
3452 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003453 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003454 */
3455 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003458 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003459 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003460
3461 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003462 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003463 * are not at the beginning of a new record, the caller did something
3464 * wrong.
3465 */
3466 if( ssl->in_left != 0 )
3467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003470 }
3471
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003472 /*
3473 * Don't even try to read if time's out already.
3474 * This avoids by-passing the timer when repeatedly receiving messages
3475 * that will end up being dropped.
3476 */
3477 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003478 {
3479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003480 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003481 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003482 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003483 {
Angus Grattond8213d02016-05-25 20:56:48 +10003484 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003487 timeout = ssl->handshake->retransmit_timeout;
3488 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003489 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003492
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003493 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003494 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3495 timeout );
3496 else
3497 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003500
3501 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003503 }
3504
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003505 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003508 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003511 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003512 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003515 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003516 }
3517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003521 return( ret );
3522 }
3523
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003524 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003527 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003529 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003530 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003533 return( ret );
3534 }
3535
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003536 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003537 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003539 }
3540
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 if( ret < 0 )
3542 return( ret );
3543
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003544 ssl->in_left = ret;
3545 }
3546 else
3547#endif
3548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003550 ssl->in_left, nb_want ) );
3551
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003552 while( ssl->in_left < nb_want )
3553 {
3554 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003555
3556 if( ssl_check_timer( ssl ) != 0 )
3557 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3558 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003559 {
3560 if( ssl->f_recv_timeout != NULL )
3561 {
3562 ret = ssl->f_recv_timeout( ssl->p_bio,
3563 ssl->in_hdr + ssl->in_left, len,
3564 ssl->conf->read_timeout );
3565 }
3566 else
3567 {
3568 ret = ssl->f_recv( ssl->p_bio,
3569 ssl->in_hdr + ssl->in_left, len );
3570 }
3571 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003574 ssl->in_left, nb_want ) );
3575 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003576
3577 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003579
3580 if( ret < 0 )
3581 return( ret );
3582
mohammad160352aecb92018-03-28 23:41:40 -07003583 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003584 {
Darryl Green11999bb2018-03-13 15:22:58 +00003585 MBEDTLS_SSL_DEBUG_MSG( 1,
3586 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003587 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3589 }
3590
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003591 ssl->in_left += ret;
3592 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 }
3594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003596
3597 return( 0 );
3598}
3599
3600/*
3601 * Flush any data not yet written
3602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003604{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003605 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003606 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003610 if( ssl->f_send == NULL )
3611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003613 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003615 }
3616
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003617 /* Avoid incrementing counter if data is flushed */
3618 if( ssl->out_left == 0 )
3619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003621 return( 0 );
3622 }
3623
Paul Bakker5121ce52009-01-03 21:22:43 +00003624 while( ssl->out_left > 0 )
3625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003627 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003628
Hanno Becker2b1e3542018-08-06 11:19:13 +01003629 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003630 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003633
3634 if( ret <= 0 )
3635 return( ret );
3636
mohammad160352aecb92018-03-28 23:41:40 -07003637 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003638 {
Darryl Green11999bb2018-03-13 15:22:58 +00003639 MBEDTLS_SSL_DEBUG_MSG( 1,
3640 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003641 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3643 }
3644
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 ssl->out_left -= ret;
3646 }
3647
Hanno Becker2b1e3542018-08-06 11:19:13 +01003648#if defined(MBEDTLS_SSL_PROTO_DTLS)
3649 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003650 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003651 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003652 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003653 else
3654#endif
3655 {
3656 ssl->out_hdr = ssl->out_buf + 8;
3657 }
3658 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003661
3662 return( 0 );
3663}
3664
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003665/*
3666 * Functions to handle the DTLS retransmission state machine
3667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003669/*
3670 * Append current handshake message to current outgoing flight
3671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003673{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3676 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3677 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003678
3679 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003680 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003681 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003684 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003685 }
3686
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003687 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003688 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003691 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003692 }
3693
3694 /* Copy current handshake message with headers */
3695 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3696 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003697 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003698 msg->next = NULL;
3699
3700 /* Append to the current flight */
3701 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003702 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003703 else
3704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003706 while( cur->next != NULL )
3707 cur = cur->next;
3708 cur->next = msg;
3709 }
3710
Hanno Becker3b235902018-08-06 09:54:53 +01003711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003712 return( 0 );
3713}
3714
3715/*
3716 * Free the current flight of handshake messages
3717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003719{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 mbedtls_ssl_flight_item *cur = flight;
3721 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003722
3723 while( cur != NULL )
3724 {
3725 next = cur->next;
3726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 mbedtls_free( cur->p );
3728 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003729
3730 cur = next;
3731 }
3732}
3733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3735static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003736#endif
3737
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003738/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003739 * Swap transform_out and out_ctr with the alternative ones
3740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003742{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003744 unsigned char tmp_out_ctr[8];
3745
3746 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003749 return;
3750 }
3751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003753
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003754 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003755 tmp_transform = ssl->transform_out;
3756 ssl->transform_out = ssl->handshake->alt_transform_out;
3757 ssl->handshake->alt_transform_out = tmp_transform;
3758
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003759 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003760 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3761 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003762 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003763
3764 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003765 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3768 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3773 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003774 }
3775 }
3776#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003777}
3778
3779/*
3780 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003781 */
3782int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3783{
3784 int ret = 0;
3785
3786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3787
3788 ret = mbedtls_ssl_flight_transmit( ssl );
3789
3790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3791
3792 return( ret );
3793}
3794
3795/*
3796 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003797 *
3798 * Need to remember the current message in case flush_output returns
3799 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003800 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003801 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003802int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003803{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003804 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003807 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003808 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003810
3811 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003812 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003813 ssl_swap_epochs( ssl );
3814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003816 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003817
3818 while( ssl->handshake->cur_msg != NULL )
3819 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003821 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003822
Hanno Beckere1dcb032018-08-17 16:47:58 +01003823 int const is_finished =
3824 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3825 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3826
Hanno Becker04da1892018-08-14 13:22:10 +01003827 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3828 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3829
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003830 /* Swap epochs before sending Finished: we can't do it after
3831 * sending ChangeCipherSpec, in case write returns WANT_READ.
3832 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003833 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003834 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003836 ssl_swap_epochs( ssl );
3837 }
3838
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 ret = ssl_get_remaining_payload_in_datagram( ssl );
3840 if( ret < 0 )
3841 return( ret );
3842 max_frag_len = (size_t) ret;
3843
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003844 /* CCS is copied as is, while HS messages may need fragmentation */
3845 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3846 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003847 if( max_frag_len == 0 )
3848 {
3849 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3850 return( ret );
3851
3852 continue;
3853 }
3854
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003855 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003856 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003857 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003858
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003859 /* Update position inside current message */
3860 ssl->handshake->cur_msg_p += cur->len;
3861 }
3862 else
3863 {
3864 const unsigned char * const p = ssl->handshake->cur_msg_p;
3865 const size_t hs_len = cur->len - 12;
3866 const size_t frag_off = p - ( cur->p + 12 );
3867 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003868 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003869
Hanno Beckere1dcb032018-08-17 16:47:58 +01003870 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003871 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003872 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003873 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003874
Hanno Becker67bc7c32018-08-06 11:33:50 +01003875 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3876 return( ret );
3877
3878 continue;
3879 }
3880 max_hs_frag_len = max_frag_len - 12;
3881
3882 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3883 max_hs_frag_len : rem_len;
3884
3885 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003886 {
3887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003888 (unsigned) cur_hs_frag_len,
3889 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003890 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003891
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003892 /* Messages are stored with handshake headers as if not fragmented,
3893 * copy beginning of headers then fill fragmentation fields.
3894 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3895 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003896
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003897 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3898 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3899 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3900
Hanno Becker67bc7c32018-08-06 11:33:50 +01003901 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3902 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3903 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003904
3905 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3906
Hanno Becker3f7b9732018-08-28 09:53:25 +01003907 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003908 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3909 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003910 ssl->out_msgtype = cur->type;
3911
3912 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003913 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003914 }
3915
3916 /* If done with the current message move to the next one if any */
3917 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3918 {
3919 if( cur->next != NULL )
3920 {
3921 ssl->handshake->cur_msg = cur->next;
3922 ssl->handshake->cur_msg_p = cur->next->p + 12;
3923 }
3924 else
3925 {
3926 ssl->handshake->cur_msg = NULL;
3927 ssl->handshake->cur_msg_p = NULL;
3928 }
3929 }
3930
3931 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003932 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003935 return( ret );
3936 }
3937 }
3938
Hanno Becker67bc7c32018-08-06 11:33:50 +01003939 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3940 return( ret );
3941
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003942 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3944 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003945 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003948 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3949 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003950
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003952
3953 return( 0 );
3954}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003955
3956/*
3957 * To be called when the last message of an incoming flight is received.
3958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003960{
3961 /* We won't need to resend that one any more */
3962 ssl_flight_free( ssl->handshake->flight );
3963 ssl->handshake->flight = NULL;
3964 ssl->handshake->cur_msg = NULL;
3965
3966 /* The next incoming flight will start with this msg_seq */
3967 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3968
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003969 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003970 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003971
Hanno Becker0271f962018-08-16 13:23:47 +01003972 /* Clear future message buffering structure. */
3973 ssl_buffering_free( ssl );
3974
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003975 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003976 ssl_set_timer( ssl, 0 );
3977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3979 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003982 }
3983 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003985}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003986
3987/*
3988 * To be called when the last message of an outgoing flight is send.
3989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003991{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003992 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003993 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3996 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003999 }
4000 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004002}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004004
Paul Bakker5121ce52009-01-03 21:22:43 +00004005/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004006 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004007 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004008
4009/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004010 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004011 *
4012 * - fill in handshake headers
4013 * - update handshake checksum
4014 * - DTLS: save message for resending
4015 * - then pass to the record layer
4016 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004017 * DTLS: except for HelloRequest, messages are only queued, and will only be
4018 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004019 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004020 * Inputs:
4021 * - ssl->out_msglen: 4 + actual handshake message len
4022 * (4 is the size of handshake headers for TLS)
4023 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4024 * - ssl->out_msg + 4: the handshake message body
4025 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004026 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004027 * - ssl->out_msglen: the length of the record contents
4028 * (including handshake headers but excluding record headers)
4029 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004030 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004031int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004032{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004033 int ret;
4034 const size_t hs_len = ssl->out_msglen - 4;
4035 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004036
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4038
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004039 /*
4040 * Sanity checks
4041 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004042 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004043 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4044 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004045 /* In SSLv3, the client might send a NoCertificate alert. */
4046#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4047 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4048 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4049 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4050#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4051 {
4052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4054 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004056
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004057 /* Whenever we send anything different from a
4058 * HelloRequest we should be in a handshake - double check. */
4059 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4060 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004061 ssl->handshake == NULL )
4062 {
4063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4065 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004068 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004069 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004071 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004074 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004075#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004076
Hanno Beckerb50a2532018-08-06 11:52:54 +01004077 /* Double-check that we did not exceed the bounds
4078 * of the outgoing record buffer.
4079 * This should never fail as the various message
4080 * writing functions must obey the bounds of the
4081 * outgoing record buffer, but better be safe.
4082 *
4083 * Note: We deliberately do not check for the MTU or MFL here.
4084 */
4085 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4086 {
4087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4088 "size %u, maximum %u",
4089 (unsigned) ssl->out_msglen,
4090 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4091 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4092 }
4093
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004094 /*
4095 * Fill handshake headers
4096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004098 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004099 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4100 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4101 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004102
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004103 /*
4104 * DTLS has additional fields in the Handshake layer,
4105 * between the length field and the actual payload:
4106 * uint16 message_seq;
4107 * uint24 fragment_offset;
4108 * uint24 fragment_length;
4109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004111 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004112 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004113 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004114 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004115 {
4116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4117 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004118 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004119 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4121 }
4122
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004123 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004124 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004125
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004126 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004127 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004128 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004129 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4130 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4131 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004132 }
4133 else
4134 {
4135 ssl->out_msg[4] = 0;
4136 ssl->out_msg[5] = 0;
4137 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004138
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004139 /* Handshake hashes are computed without fragmentation,
4140 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004141 memset( ssl->out_msg + 6, 0x00, 3 );
4142 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004143 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004145
Hanno Becker0207e532018-08-28 10:28:28 +01004146 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004147 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4148 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004149 }
4150
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004151 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004153 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004154 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4155 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004156 {
4157 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004160 return( ret );
4161 }
4162 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004163 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004164#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004165 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004166 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004167 {
4168 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4169 return( ret );
4170 }
4171 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004172
4173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4174
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004175 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004176}
4177
4178/*
4179 * Record layer functions
4180 */
4181
4182/*
4183 * Write current record.
4184 *
4185 * Uses:
4186 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4187 * - ssl->out_msglen: length of the record content (excl headers)
4188 * - ssl->out_msg: record content
4189 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004190int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004191{
4192 int ret, done = 0;
4193 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004194 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004195
4196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004199 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004201 {
4202 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004205 return( ret );
4206 }
4207
4208 len = ssl->out_msglen;
4209 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4213 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217 ret = mbedtls_ssl_hw_record_write( ssl );
4218 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4221 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004222 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004223
4224 if( ret == 0 )
4225 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004226 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004228 if( !done )
4229 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004230 unsigned i;
4231 size_t protected_record_size;
4232
Hanno Becker6430faf2019-05-08 11:57:13 +01004233 /* Skip writing the record content type to after the encryption,
4234 * as it may change when using the CID extension. */
4235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004236 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004237 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004238
Hanno Becker19859472018-08-06 09:40:20 +01004239 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004240 ssl->out_len[0] = (unsigned char)( len >> 8 );
4241 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004242
Paul Bakker48916f92012-09-16 19:57:18 +00004243 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004244 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004245 mbedtls_record rec;
4246
4247 rec.buf = ssl->out_iv;
4248 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4249 ( ssl->out_iv - ssl->out_buf );
4250 rec.data_len = ssl->out_msglen;
4251 rec.data_offset = ssl->out_msg - rec.buf;
4252
4253 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4254 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4255 ssl->conf->transport, rec.ver );
4256 rec.type = ssl->out_msgtype;
4257
Hanno Beckera0e20d02019-05-15 14:03:01 +01004258#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004259 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004260 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004261#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004262
Hanno Beckera18d1322018-01-03 14:27:32 +00004263 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004264 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004267 return( ret );
4268 }
4269
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004270 if( rec.data_offset != 0 )
4271 {
4272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4273 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4274 }
4275
Hanno Becker6430faf2019-05-08 11:57:13 +01004276 /* Update the record content type and CID. */
4277 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004278#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004279 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004280#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004281 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004282 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4283 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004284 }
4285
Hanno Becker5903de42019-05-03 14:46:38 +01004286 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004287
4288#if defined(MBEDTLS_SSL_PROTO_DTLS)
4289 /* In case of DTLS, double-check that we don't exceed
4290 * the remaining space in the datagram. */
4291 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4292 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004293 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004294 if( ret < 0 )
4295 return( ret );
4296
4297 if( protected_record_size > (size_t) ret )
4298 {
4299 /* Should never happen */
4300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4301 }
4302 }
4303#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004304
Hanno Becker6430faf2019-05-08 11:57:13 +01004305 /* Now write the potentially updated record content type. */
4306 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004309 "version = [%d:%d], msglen = %d",
4310 ssl->out_hdr[0], ssl->out_hdr[1],
4311 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004314 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004315
4316 ssl->out_left += protected_record_size;
4317 ssl->out_hdr += protected_record_size;
4318 ssl_update_out_pointers( ssl, ssl->transform_out );
4319
Hanno Becker04484622018-08-06 09:49:38 +01004320 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4321 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4322 break;
4323
4324 /* The loop goes to its end iff the counter is wrapping */
4325 if( i == ssl_ep_len( ssl ) )
4326 {
4327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4328 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4329 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004330 }
4331
Hanno Becker67bc7c32018-08-06 11:33:50 +01004332#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004333 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4334 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004335 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004336 size_t remaining;
4337 ret = ssl_get_remaining_payload_in_datagram( ssl );
4338 if( ret < 0 )
4339 {
4340 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4341 ret );
4342 return( ret );
4343 }
4344
4345 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004346 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004347 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004348 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004349 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004350 else
4351 {
Hanno Becker513815a2018-08-20 11:56:09 +01004352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004353 }
4354 }
4355#endif /* MBEDTLS_SSL_PROTO_DTLS */
4356
4357 if( ( flush == SSL_FORCE_FLUSH ) &&
4358 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004361 return( ret );
4362 }
4363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004365
4366 return( 0 );
4367}
4368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004370
4371static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4372{
4373 if( ssl->in_msglen < ssl->in_hslen ||
4374 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4375 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4376 {
4377 return( 1 );
4378 }
4379 return( 0 );
4380}
Hanno Becker44650b72018-08-16 12:51:11 +01004381
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004382static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004383{
4384 return( ( ssl->in_msg[9] << 16 ) |
4385 ( ssl->in_msg[10] << 8 ) |
4386 ssl->in_msg[11] );
4387}
4388
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004389static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004390{
4391 return( ( ssl->in_msg[6] << 16 ) |
4392 ( ssl->in_msg[7] << 8 ) |
4393 ssl->in_msg[8] );
4394}
4395
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004396static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004397{
4398 uint32_t msg_len, frag_off, frag_len;
4399
4400 msg_len = ssl_get_hs_total_len( ssl );
4401 frag_off = ssl_get_hs_frag_off( ssl );
4402 frag_len = ssl_get_hs_frag_len( ssl );
4403
4404 if( frag_off > msg_len )
4405 return( -1 );
4406
4407 if( frag_len > msg_len - frag_off )
4408 return( -1 );
4409
4410 if( frag_len + 12 > ssl->in_msglen )
4411 return( -1 );
4412
4413 return( 0 );
4414}
4415
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004416/*
4417 * Mark bits in bitmask (used for DTLS HS reassembly)
4418 */
4419static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4420{
4421 unsigned int start_bits, end_bits;
4422
4423 start_bits = 8 - ( offset % 8 );
4424 if( start_bits != 8 )
4425 {
4426 size_t first_byte_idx = offset / 8;
4427
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004428 /* Special case */
4429 if( len <= start_bits )
4430 {
4431 for( ; len != 0; len-- )
4432 mask[first_byte_idx] |= 1 << ( start_bits - len );
4433
4434 /* Avoid potential issues with offset or len becoming invalid */
4435 return;
4436 }
4437
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004438 offset += start_bits; /* Now offset % 8 == 0 */
4439 len -= start_bits;
4440
4441 for( ; start_bits != 0; start_bits-- )
4442 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4443 }
4444
4445 end_bits = len % 8;
4446 if( end_bits != 0 )
4447 {
4448 size_t last_byte_idx = ( offset + len ) / 8;
4449
4450 len -= end_bits; /* Now len % 8 == 0 */
4451
4452 for( ; end_bits != 0; end_bits-- )
4453 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4454 }
4455
4456 memset( mask + offset / 8, 0xFF, len / 8 );
4457}
4458
4459/*
4460 * Check that bitmask is full
4461 */
4462static int ssl_bitmask_check( unsigned char *mask, size_t len )
4463{
4464 size_t i;
4465
4466 for( i = 0; i < len / 8; i++ )
4467 if( mask[i] != 0xFF )
4468 return( -1 );
4469
4470 for( i = 0; i < len % 8; i++ )
4471 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4472 return( -1 );
4473
4474 return( 0 );
4475}
4476
Hanno Becker56e205e2018-08-16 09:06:12 +01004477/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004478static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004479 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004480{
Hanno Becker56e205e2018-08-16 09:06:12 +01004481 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004482
Hanno Becker56e205e2018-08-16 09:06:12 +01004483 alloc_len = 12; /* Handshake header */
4484 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004485
Hanno Beckerd07df862018-08-16 09:14:58 +01004486 if( add_bitmap )
4487 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004488
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004489 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004490}
Hanno Becker56e205e2018-08-16 09:06:12 +01004491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004493
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004494static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004495{
4496 return( ( ssl->in_msg[1] << 16 ) |
4497 ( ssl->in_msg[2] << 8 ) |
4498 ssl->in_msg[3] );
4499}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004500
Simon Butcher99000142016-10-13 17:21:01 +01004501int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004503 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004506 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004508 }
4509
Hanno Becker12555c62018-08-16 12:47:53 +01004510 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004513 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004514 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004517 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004518 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004519 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004520 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004521
Hanno Becker44650b72018-08-16 12:51:11 +01004522 if( ssl_check_hs_header( ssl ) != 0 )
4523 {
4524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4525 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4526 }
4527
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004528 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004529 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4530 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4531 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4532 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004533 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004534 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4535 {
4536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4537 recv_msg_seq,
4538 ssl->handshake->in_msg_seq ) );
4539 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4540 }
4541
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004542 /* Retransmit only on last message from previous flight, to avoid
4543 * too many retransmissions.
4544 * Besides, No sane server ever retransmits HelloVerifyRequest */
4545 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004549 "message_seq = %d, start_of_flight = %d",
4550 recv_msg_seq,
4551 ssl->handshake->in_flight_start_seq ) );
4552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004556 return( ret );
4557 }
4558 }
4559 else
4560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004562 "message_seq = %d, expected = %d",
4563 recv_msg_seq,
4564 ssl->handshake->in_msg_seq ) );
4565 }
4566
Hanno Becker90333da2017-10-10 11:27:13 +01004567 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004568 }
4569 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004570
Hanno Becker6d97ef52018-08-16 13:09:04 +01004571 /* Message reassembly is handled alongside buffering of future
4572 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004573 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004574 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004575 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004578 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004579 }
4580 }
4581 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004583 /* With TLS we don't handle fragmentation (for now) */
4584 if( ssl->in_msglen < ssl->in_hslen )
4585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4587 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004588 }
4589
Simon Butcher99000142016-10-13 17:21:01 +01004590 return( 0 );
4591}
4592
4593void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4594{
Hanno Becker0271f962018-08-16 13:23:47 +01004595 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004596
Hanno Becker0271f962018-08-16 13:23:47 +01004597 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004598 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004599 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004600 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004601
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004602 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004604 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004605 ssl->handshake != NULL )
4606 {
Hanno Becker0271f962018-08-16 13:23:47 +01004607 unsigned offset;
4608 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004609
Hanno Becker0271f962018-08-16 13:23:47 +01004610 /* Increment handshake sequence number */
4611 hs->in_msg_seq++;
4612
4613 /*
4614 * Clear up handshake buffering and reassembly structure.
4615 */
4616
4617 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004618 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004619
4620 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004621 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4622 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004623 offset++, hs_buf++ )
4624 {
4625 *hs_buf = *(hs_buf + 1);
4626 }
4627
4628 /* Create a fresh last entry */
4629 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004630 }
4631#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004632}
4633
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004634/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004635 * DTLS anti-replay: RFC 6347 4.1.2.6
4636 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004637 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4638 * Bit n is set iff record number in_window_top - n has been seen.
4639 *
4640 * Usually, in_window_top is the last record number seen and the lsb of
4641 * in_window is set. The only exception is the initial state (record number 0
4642 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4645static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004646{
4647 ssl->in_window_top = 0;
4648 ssl->in_window = 0;
4649}
4650
4651static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4652{
4653 return( ( (uint64_t) buf[0] << 40 ) |
4654 ( (uint64_t) buf[1] << 32 ) |
4655 ( (uint64_t) buf[2] << 24 ) |
4656 ( (uint64_t) buf[3] << 16 ) |
4657 ( (uint64_t) buf[4] << 8 ) |
4658 ( (uint64_t) buf[5] ) );
4659}
4660
4661/*
4662 * Return 0 if sequence number is acceptable, -1 otherwise
4663 */
Hanno Becker0183d692019-07-12 08:50:37 +01004664int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004665{
4666 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4667 uint64_t bit;
4668
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004669 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004670 return( 0 );
4671
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004672 if( rec_seqnum > ssl->in_window_top )
4673 return( 0 );
4674
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004675 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004676
4677 if( bit >= 64 )
4678 return( -1 );
4679
4680 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4681 return( -1 );
4682
4683 return( 0 );
4684}
4685
4686/*
4687 * Update replay window on new validated record
4688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004690{
4691 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4692
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004693 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004694 return;
4695
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004696 if( rec_seqnum > ssl->in_window_top )
4697 {
4698 /* Update window_top and the contents of the window */
4699 uint64_t shift = rec_seqnum - ssl->in_window_top;
4700
4701 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004702 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004703 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004704 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004705 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004706 ssl->in_window |= 1;
4707 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004708
4709 ssl->in_window_top = rec_seqnum;
4710 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004711 else
4712 {
4713 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004714 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004715
4716 if( bit < 64 ) /* Always true, but be extra sure */
4717 ssl->in_window |= (uint64_t) 1 << bit;
4718 }
4719}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004721
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004722#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004723/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004724static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4725
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004726/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004727 * Without any SSL context, check if a datagram looks like a ClientHello with
4728 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004729 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004730 *
4731 * - if cookie is valid, return 0
4732 * - if ClientHello looks superficially valid but cookie is not,
4733 * fill obuf and set olen, then
4734 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4735 * - otherwise return a specific error code
4736 */
4737static int ssl_check_dtls_clihlo_cookie(
4738 mbedtls_ssl_cookie_write_t *f_cookie_write,
4739 mbedtls_ssl_cookie_check_t *f_cookie_check,
4740 void *p_cookie,
4741 const unsigned char *cli_id, size_t cli_id_len,
4742 const unsigned char *in, size_t in_len,
4743 unsigned char *obuf, size_t buf_len, size_t *olen )
4744{
4745 size_t sid_len, cookie_len;
4746 unsigned char *p;
4747
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004748 /*
4749 * Structure of ClientHello with record and handshake headers,
4750 * and expected values. We don't need to check a lot, more checks will be
4751 * done when actually parsing the ClientHello - skipping those checks
4752 * avoids code duplication and does not make cookie forging any easier.
4753 *
4754 * 0-0 ContentType type; copied, must be handshake
4755 * 1-2 ProtocolVersion version; copied
4756 * 3-4 uint16 epoch; copied, must be 0
4757 * 5-10 uint48 sequence_number; copied
4758 * 11-12 uint16 length; (ignored)
4759 *
4760 * 13-13 HandshakeType msg_type; (ignored)
4761 * 14-16 uint24 length; (ignored)
4762 * 17-18 uint16 message_seq; copied
4763 * 19-21 uint24 fragment_offset; copied, must be 0
4764 * 22-24 uint24 fragment_length; (ignored)
4765 *
4766 * 25-26 ProtocolVersion client_version; (ignored)
4767 * 27-58 Random random; (ignored)
4768 * 59-xx SessionID session_id; 1 byte len + sid_len content
4769 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4770 * ...
4771 *
4772 * Minimum length is 61 bytes.
4773 */
4774 if( in_len < 61 ||
4775 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4776 in[3] != 0 || in[4] != 0 ||
4777 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4778 {
4779 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4780 }
4781
4782 sid_len = in[59];
4783 if( sid_len > in_len - 61 )
4784 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4785
4786 cookie_len = in[60 + sid_len];
4787 if( cookie_len > in_len - 60 )
4788 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4789
4790 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4791 cli_id, cli_id_len ) == 0 )
4792 {
4793 /* Valid cookie */
4794 return( 0 );
4795 }
4796
4797 /*
4798 * If we get here, we've got an invalid cookie, let's prepare HVR.
4799 *
4800 * 0-0 ContentType type; copied
4801 * 1-2 ProtocolVersion version; copied
4802 * 3-4 uint16 epoch; copied
4803 * 5-10 uint48 sequence_number; copied
4804 * 11-12 uint16 length; olen - 13
4805 *
4806 * 13-13 HandshakeType msg_type; hello_verify_request
4807 * 14-16 uint24 length; olen - 25
4808 * 17-18 uint16 message_seq; copied
4809 * 19-21 uint24 fragment_offset; copied
4810 * 22-24 uint24 fragment_length; olen - 25
4811 *
4812 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4813 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4814 *
4815 * Minimum length is 28.
4816 */
4817 if( buf_len < 28 )
4818 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4819
4820 /* Copy most fields and adapt others */
4821 memcpy( obuf, in, 25 );
4822 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4823 obuf[25] = 0xfe;
4824 obuf[26] = 0xff;
4825
4826 /* Generate and write actual cookie */
4827 p = obuf + 28;
4828 if( f_cookie_write( p_cookie,
4829 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4830 {
4831 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4832 }
4833
4834 *olen = p - obuf;
4835
4836 /* Go back and fill length fields */
4837 obuf[27] = (unsigned char)( *olen - 28 );
4838
4839 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4840 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4841 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4842
4843 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4844 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4845
4846 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4847}
4848
4849/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004850 * Handle possible client reconnect with the same UDP quadruplet
4851 * (RFC 6347 Section 4.2.8).
4852 *
4853 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4854 * that looks like a ClientHello.
4855 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004856 * - if the input looks like a ClientHello without cookies,
4857 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004858 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004859 * - if the input looks like a ClientHello with a valid cookie,
4860 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004861 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004862 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004863 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004864 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004865 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4866 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004867 */
4868static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4869{
4870 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004871 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004872
Hanno Becker2fddd372019-07-10 14:37:41 +01004873 if( ssl->conf->f_cookie_write == NULL ||
4874 ssl->conf->f_cookie_check == NULL )
4875 {
4876 /* If we can't use cookies to verify reachability of the peer,
4877 * drop the record. */
4878 return( 0 );
4879 }
4880
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004881 ret = ssl_check_dtls_clihlo_cookie(
4882 ssl->conf->f_cookie_write,
4883 ssl->conf->f_cookie_check,
4884 ssl->conf->p_cookie,
4885 ssl->cli_id, ssl->cli_id_len,
4886 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004887 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004888
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004889 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4890
4891 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004892 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004893 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004894 * If the error is permanent we'll catch it later,
4895 * if it's not, then hopefully it'll work next time. */
4896 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004897 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004898 }
4899
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004900 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004901 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004902 /* Got a valid cookie, partially reset context */
4903 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4904 {
4905 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4906 return( ret );
4907 }
4908
4909 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004910 }
4911
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004912 return( ret );
4913}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004914#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004915
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004916static int ssl_check_record_type( uint8_t record_type )
4917{
4918 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4919 record_type != MBEDTLS_SSL_MSG_ALERT &&
4920 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4921 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4922 {
4923 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4924 }
4925
4926 return( 0 );
4927}
4928
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004929/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004930 * ContentType type;
4931 * ProtocolVersion version;
4932 * uint16 epoch; // DTLS only
4933 * uint48 sequence_number; // DTLS only
4934 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004935 *
4936 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004937 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004938 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4939 *
4940 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004941 * 1. proceed with the record if this function returns 0
4942 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4943 * 3. return CLIENT_RECONNECT if this function return that value
4944 * 4. drop the whole datagram if this function returns anything else.
4945 * Point 2 is needed when the peer is resending, and we have already received
4946 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004947 */
Hanno Becker331de3d2019-07-12 11:10:16 +01004948static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01004949 unsigned char *buf,
4950 size_t len,
4951 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00004952{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004953 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004954
Hanno Beckere5e7e782019-07-11 12:29:35 +01004955 size_t const rec_hdr_type_offset = 0;
4956 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004957
Hanno Beckere5e7e782019-07-11 12:29:35 +01004958 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
4959 rec_hdr_type_len;
4960 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00004961
Hanno Beckere5e7e782019-07-11 12:29:35 +01004962 size_t const rec_hdr_ctr_len = 8;
4963#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01004964 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004965 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
4966 rec_hdr_version_len;
4967
Hanno Beckera0e20d02019-05-15 14:03:01 +01004968#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01004969 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
4970 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01004971 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004972#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4973#endif /* MBEDTLS_SSL_PROTO_DTLS */
4974
4975 size_t rec_hdr_len_offset; /* To be determined */
4976 size_t const rec_hdr_len_len = 2;
4977
4978 /*
4979 * Check minimum lengths for record header.
4980 */
4981
4982#if defined(MBEDTLS_SSL_PROTO_DTLS)
4983 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4984 {
4985 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
4986 }
4987 else
4988#endif /* MBEDTLS_SSL_PROTO_DTLS */
4989 {
4990 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
4991 }
4992
4993 if( len < rec_hdr_len_offset + rec_hdr_len_len )
4994 {
4995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
4996 (unsigned) len,
4997 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
4998 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4999 }
5000
5001 /*
5002 * Parse and validate record content type
5003 */
5004
5005 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005006
5007 /* Check record content type */
5008#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5009 rec->cid_len = 0;
5010
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005011 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005012 ssl->conf->cid_len != 0 &&
5013 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005014 {
5015 /* Shift pointers to account for record header including CID
5016 * struct {
5017 * ContentType special_type = tls12_cid;
5018 * ProtocolVersion version;
5019 * uint16 epoch;
5020 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005021 * opaque cid[cid_length]; // Additional field compared to
5022 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005023 * uint16 length;
5024 * opaque enc_content[DTLSCiphertext.length];
5025 * } DTLSCiphertext;
5026 */
5027
5028 /* So far, we only support static CID lengths
5029 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005030 rec_hdr_cid_len = ssl->conf->cid_len;
5031 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005032
Hanno Beckere5e7e782019-07-11 12:29:35 +01005033 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005034 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5036 (unsigned) len,
5037 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005038 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005039 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005040
5041 rec->cid_len = rec_hdr_cid_len;
5042 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005043 }
5044 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005045#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005046 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005047 if( ssl_check_record_type( rec->type ) )
5048 {
Hanno Becker54229812019-07-12 14:40:00 +01005049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5050 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005051 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5052 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005053 }
5054
Hanno Beckere5e7e782019-07-11 12:29:35 +01005055 /*
5056 * Parse and validate record version
5057 */
5058
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005059 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5060 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005061 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5062 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005063 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005064
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005065 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5068 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005069 }
5070
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005071 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5074 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005075 }
5076
Hanno Beckere5e7e782019-07-11 12:29:35 +01005077 /*
5078 * Parse/Copy record sequence number.
5079 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005080
Hanno Beckere5e7e782019-07-11 12:29:35 +01005081#if defined(MBEDTLS_SSL_PROTO_DTLS)
5082 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5083 {
5084 /* Copy explicit record sequence number from input buffer. */
5085 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5086 rec_hdr_ctr_len );
5087 }
5088 else
5089#endif /* MBEDTLS_SSL_PROTO_DTLS */
5090 {
5091 /* Copy implicit record sequence number from SSL context structure. */
5092 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5093 }
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005094
Hanno Beckere5e7e782019-07-11 12:29:35 +01005095 /*
5096 * Parse record length.
5097 */
5098
Hanno Beckere5e7e782019-07-11 12:29:35 +01005099 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005100 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5101 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005102 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5103
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005105 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005106 rec->type,
5107 major_ver, minor_ver, rec->data_len ) );
5108
5109 rec->buf = buf;
5110 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005111
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005112 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005113 * DTLS-related tests.
5114 * Check epoch before checking length constraint because
5115 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5116 * message gets duplicated before the corresponding Finished message,
5117 * the second ChangeCipherSpec should be discarded because it belongs
5118 * to an old epoch, but not because its length is shorter than
5119 * the minimum record length for packets using the new record transform.
5120 * Note that these two kinds of failures are handled differently,
5121 * as an unexpected record is silently skipped but an invalid
5122 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005123 */
5124#if defined(MBEDTLS_SSL_PROTO_DTLS)
5125 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5126 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005127 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005128
Hanno Becker955a5c92019-07-10 17:12:07 +01005129 /* Check that the datagram is large enough to contain a record
5130 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005131 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005132 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5134 (unsigned) len,
5135 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005136 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5137 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005138
Hanno Becker37cfe732019-07-10 17:20:01 +01005139 /* Records from other, non-matching epochs are silently discarded.
5140 * (The case of same-port Client reconnects must be considered in
5141 * the caller). */
Hanno Becker552f7472019-07-19 10:59:12 +01005142 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005143 {
5144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5145 "expected %d, received %d",
5146 ssl->in_epoch, rec_epoch ) );
Hanno Becker552f7472019-07-19 10:59:12 +01005147
5148 /* Records from the next epoch are considered for buffering
5149 * (concretely: early Finished messages). */
5150 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5151 {
5152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5153 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5154 }
5155
Hanno Becker2fddd372019-07-10 14:37:41 +01005156 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005157 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005158#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005159 /* For records from the correct epoch, check whether their
5160 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005161 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005162 {
5163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5164 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5165 }
5166#endif
5167 }
5168#endif /* MBEDTLS_SSL_PROTO_DTLS */
5169
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005170 return( 0 );
5171}
Paul Bakker5121ce52009-01-03 21:22:43 +00005172
Hanno Becker2fddd372019-07-10 14:37:41 +01005173
5174#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5175static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5176{
5177 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5178
5179 /*
5180 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5181 * access the first byte of record content (handshake type), as we
5182 * have an active transform (possibly iv_len != 0), so use the
5183 * fact that the record header len is 13 instead.
5184 */
5185 if( rec_epoch == 0 &&
5186 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5187 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5188 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5189 ssl->in_left > 13 &&
5190 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5191 {
5192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5193 "from the same port" ) );
5194 return( ssl_handle_possible_reconnect( ssl ) );
5195 }
5196
5197 return( 0 );
5198}
5199#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5200
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005201/*
5202 * If applicable, decrypt (and decompress) record content
5203 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005204static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5205 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005206{
5207 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005210 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5213 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217 ret = mbedtls_ssl_hw_record_read( ssl );
5218 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5221 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005222 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005223
5224 if( ret == 0 )
5225 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005226 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005227#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005228 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005229 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005230 unsigned char const old_msg_type = rec->type;
5231
Hanno Beckera18d1322018-01-03 14:27:32 +00005232 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005233 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005236
Hanno Beckera0e20d02019-05-15 14:03:01 +01005237#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005238 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5239 ssl->conf->ignore_unexpected_cid
5240 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5241 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005242 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005243 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005244 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005245#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005246
Paul Bakker5121ce52009-01-03 21:22:43 +00005247 return( ret );
5248 }
5249
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005250 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005251 {
5252 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005253 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005254 }
5255
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005256 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005257 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005258
Hanno Beckera0e20d02019-05-15 14:03:01 +01005259#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005260 /* We have already checked the record content type
5261 * in ssl_parse_record_header(), failing or silently
5262 * dropping the record in the case of an unknown type.
5263 *
5264 * Since with the use of CIDs, the record content type
5265 * might change during decryption, re-check the record
5266 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005267 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005268 {
5269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5270 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5271 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005272#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005273
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005274 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005275 {
5276#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5277 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005278 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005279 {
5280 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5282 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5283 }
5284#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5285
5286 ssl->nb_zero++;
5287
5288 /*
5289 * Three or more empty messages may be a DoS attack
5290 * (excessive CPU consumption).
5291 */
5292 if( ssl->nb_zero > 3 )
5293 {
5294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005295 "messages, possible DoS attack" ) );
5296 /* Treat the records as if they were not properly authenticated,
5297 * thereby failing the connection if we see more than allowed
5298 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005299 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5300 }
5301 }
5302 else
5303 ssl->nb_zero = 0;
5304
5305#if defined(MBEDTLS_SSL_PROTO_DTLS)
5306 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5307 {
5308 ; /* in_ctr read from peer, not maintained internally */
5309 }
5310 else
5311#endif
5312 {
5313 unsigned i;
5314 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5315 if( ++ssl->in_ctr[i - 1] != 0 )
5316 break;
5317
5318 /* The loop goes to its end iff the counter is wrapping */
5319 if( i == ssl_ep_len( ssl ) )
5320 {
5321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5322 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5323 }
5324 }
5325
Paul Bakker5121ce52009-01-03 21:22:43 +00005326 }
5327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005329 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005331 {
5332 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005335 return( ret );
5336 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005337 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005341 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005344 }
5345#endif
5346
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005347 /* Check actual (decrypted) record content length against
5348 * configured maximum. */
5349 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5350 {
5351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5352 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5353 }
5354
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005355 return( 0 );
5356}
5357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005359
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005360/*
5361 * Read a record.
5362 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005363 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5364 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5365 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005366 */
Hanno Becker1097b342018-08-15 14:09:41 +01005367
5368/* Helper functions for mbedtls_ssl_read_record(). */
5369static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005370static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5371static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005372
Hanno Becker327c93b2018-08-15 13:56:18 +01005373int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005374 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005375{
5376 int ret;
5377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005379
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005380 if( ssl->keep_current_message == 0 )
5381 {
5382 do {
Simon Butcher99000142016-10-13 17:21:01 +01005383
Hanno Becker26994592018-08-15 14:14:59 +01005384 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005385 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005386 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005387
Hanno Beckere74d5562018-08-15 14:26:08 +01005388 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005389 {
Hanno Becker40f50842018-08-15 14:48:01 +01005390#if defined(MBEDTLS_SSL_PROTO_DTLS)
5391 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005392
Hanno Becker40f50842018-08-15 14:48:01 +01005393 /* We only check for buffered messages if the
5394 * current datagram is fully consumed. */
5395 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005396 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005397 {
Hanno Becker40f50842018-08-15 14:48:01 +01005398 if( ssl_load_buffered_message( ssl ) == 0 )
5399 have_buffered = 1;
5400 }
5401
5402 if( have_buffered == 0 )
5403#endif /* MBEDTLS_SSL_PROTO_DTLS */
5404 {
5405 ret = ssl_get_next_record( ssl );
5406 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5407 continue;
5408
5409 if( ret != 0 )
5410 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005411 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005412 return( ret );
5413 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005414 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005415 }
5416
5417 ret = mbedtls_ssl_handle_message_type( ssl );
5418
Hanno Becker40f50842018-08-15 14:48:01 +01005419#if defined(MBEDTLS_SSL_PROTO_DTLS)
5420 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5421 {
5422 /* Buffer future message */
5423 ret = ssl_buffer_message( ssl );
5424 if( ret != 0 )
5425 return( ret );
5426
5427 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5428 }
5429#endif /* MBEDTLS_SSL_PROTO_DTLS */
5430
Hanno Becker90333da2017-10-10 11:27:13 +01005431 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5432 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005433
5434 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005435 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005436 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005437 return( ret );
5438 }
5439
Hanno Becker327c93b2018-08-15 13:56:18 +01005440 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005441 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005442 {
5443 mbedtls_ssl_update_handshake_status( ssl );
5444 }
Simon Butcher99000142016-10-13 17:21:01 +01005445 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005446 else
Simon Butcher99000142016-10-13 17:21:01 +01005447 {
Hanno Becker02f59072018-08-15 14:00:24 +01005448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005449 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005450 }
5451
5452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5453
5454 return( 0 );
5455}
5456
Hanno Becker40f50842018-08-15 14:48:01 +01005457#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005458static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005459{
Hanno Becker40f50842018-08-15 14:48:01 +01005460 if( ssl->in_left > ssl->next_record_offset )
5461 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005462
Hanno Becker40f50842018-08-15 14:48:01 +01005463 return( 0 );
5464}
5465
5466static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5467{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005468 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005469 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005470 int ret = 0;
5471
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005472 if( hs == NULL )
5473 return( -1 );
5474
Hanno Beckere00ae372018-08-20 09:39:42 +01005475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5476
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005477 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5478 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5479 {
5480 /* Check if we have seen a ChangeCipherSpec before.
5481 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005482 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005483 {
5484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5485 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005486 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005487 }
5488
Hanno Becker39b8bc92018-08-28 17:17:13 +01005489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005490 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5491 ssl->in_msglen = 1;
5492 ssl->in_msg[0] = 1;
5493
5494 /* As long as they are equal, the exact value doesn't matter. */
5495 ssl->in_left = 0;
5496 ssl->next_record_offset = 0;
5497
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005498 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005499 goto exit;
5500 }
Hanno Becker37f95322018-08-16 13:55:32 +01005501
Hanno Beckerb8f50142018-08-28 10:01:34 +01005502#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005503 /* Debug only */
5504 {
5505 unsigned offset;
5506 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5507 {
5508 hs_buf = &hs->buffering.hs[offset];
5509 if( hs_buf->is_valid == 1 )
5510 {
5511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5512 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005513 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005514 }
5515 }
5516 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005517#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005518
5519 /* Check if we have buffered and/or fully reassembled the
5520 * next handshake message. */
5521 hs_buf = &hs->buffering.hs[0];
5522 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5523 {
5524 /* Synthesize a record containing the buffered HS message. */
5525 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5526 ( hs_buf->data[2] << 8 ) |
5527 hs_buf->data[3];
5528
5529 /* Double-check that we haven't accidentally buffered
5530 * a message that doesn't fit into the input buffer. */
5531 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5532 {
5533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5535 }
5536
5537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5538 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5539 hs_buf->data, msg_len + 12 );
5540
5541 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5542 ssl->in_hslen = msg_len + 12;
5543 ssl->in_msglen = msg_len + 12;
5544 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5545
5546 ret = 0;
5547 goto exit;
5548 }
5549 else
5550 {
5551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5552 hs->in_msg_seq ) );
5553 }
5554
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005555 ret = -1;
5556
5557exit:
5558
5559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5560 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005561}
5562
Hanno Beckera02b0b42018-08-21 17:20:27 +01005563static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5564 size_t desired )
5565{
5566 int offset;
5567 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5569 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005570
Hanno Becker01315ea2018-08-21 17:22:17 +01005571 /* Get rid of future records epoch first, if such exist. */
5572 ssl_free_buffered_record( ssl );
5573
5574 /* Check if we have enough space available now. */
5575 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5576 hs->buffering.total_bytes_buffered ) )
5577 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005579 return( 0 );
5580 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005581
Hanno Becker4f432ad2018-08-28 10:02:32 +01005582 /* We don't have enough space to buffer the next expected handshake
5583 * message. Remove buffers used for future messages to gain space,
5584 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005585 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5586 offset >= 0; offset-- )
5587 {
5588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5589 offset ) );
5590
Hanno Beckerb309b922018-08-23 13:18:05 +01005591 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005592
5593 /* Check if we have enough space available now. */
5594 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5595 hs->buffering.total_bytes_buffered ) )
5596 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005598 return( 0 );
5599 }
5600 }
5601
5602 return( -1 );
5603}
5604
Hanno Becker40f50842018-08-15 14:48:01 +01005605static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5606{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005607 int ret = 0;
5608 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5609
5610 if( hs == NULL )
5611 return( 0 );
5612
5613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5614
5615 switch( ssl->in_msgtype )
5616 {
5617 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005619
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005620 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005621 break;
5622
5623 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005624 {
5625 unsigned recv_msg_seq_offset;
5626 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5627 mbedtls_ssl_hs_buffer *hs_buf;
5628 size_t msg_len = ssl->in_hslen - 12;
5629
5630 /* We should never receive an old handshake
5631 * message - double-check nonetheless. */
5632 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5633 {
5634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5635 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5636 }
5637
5638 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5639 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5640 {
5641 /* Silently ignore -- message too far in the future */
5642 MBEDTLS_SSL_DEBUG_MSG( 2,
5643 ( "Ignore future HS message with sequence number %u, "
5644 "buffering window %u - %u",
5645 recv_msg_seq, ssl->handshake->in_msg_seq,
5646 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5647
5648 goto exit;
5649 }
5650
5651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5652 recv_msg_seq, recv_msg_seq_offset ) );
5653
5654 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5655
5656 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005657 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005658 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005659 size_t reassembly_buf_sz;
5660
Hanno Becker37f95322018-08-16 13:55:32 +01005661 hs_buf->is_fragmented =
5662 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5663
5664 /* We copy the message back into the input buffer
5665 * after reassembly, so check that it's not too large.
5666 * This is an implementation-specific limitation
5667 * and not one from the standard, hence it is not
5668 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005669 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005670 {
5671 /* Ignore message */
5672 goto exit;
5673 }
5674
Hanno Beckere0b150f2018-08-21 15:51:03 +01005675 /* Check if we have enough space to buffer the message. */
5676 if( hs->buffering.total_bytes_buffered >
5677 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5678 {
5679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5680 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5681 }
5682
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005683 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5684 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005685
5686 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5687 hs->buffering.total_bytes_buffered ) )
5688 {
5689 if( recv_msg_seq_offset > 0 )
5690 {
5691 /* If we can't buffer a future message because
5692 * of space limitations -- ignore. */
5693 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",
5694 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5695 (unsigned) hs->buffering.total_bytes_buffered ) );
5696 goto exit;
5697 }
Hanno Beckere1801392018-08-21 16:51:05 +01005698 else
5699 {
5700 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",
5701 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5702 (unsigned) hs->buffering.total_bytes_buffered ) );
5703 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005704
Hanno Beckera02b0b42018-08-21 17:20:27 +01005705 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005706 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005707 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",
5708 (unsigned) msg_len,
5709 (unsigned) reassembly_buf_sz,
5710 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005711 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005712 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5713 goto exit;
5714 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005715 }
5716
5717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5718 msg_len ) );
5719
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005720 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5721 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005722 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005723 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005724 goto exit;
5725 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005726 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005727
5728 /* Prepare final header: copy msg_type, length and message_seq,
5729 * then add standardised fragment_offset and fragment_length */
5730 memcpy( hs_buf->data, ssl->in_msg, 6 );
5731 memset( hs_buf->data + 6, 0, 3 );
5732 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5733
5734 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005735
5736 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005737 }
5738 else
5739 {
5740 /* Make sure msg_type and length are consistent */
5741 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5742 {
5743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5744 /* Ignore */
5745 goto exit;
5746 }
5747 }
5748
Hanno Becker4422bbb2018-08-20 09:40:19 +01005749 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005750 {
5751 size_t frag_len, frag_off;
5752 unsigned char * const msg = hs_buf->data + 12;
5753
5754 /*
5755 * Check and copy current fragment
5756 */
5757
5758 /* Validation of header fields already done in
5759 * mbedtls_ssl_prepare_handshake_record(). */
5760 frag_off = ssl_get_hs_frag_off( ssl );
5761 frag_len = ssl_get_hs_frag_len( ssl );
5762
5763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5764 frag_off, frag_len ) );
5765 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5766
5767 if( hs_buf->is_fragmented )
5768 {
5769 unsigned char * const bitmask = msg + msg_len;
5770 ssl_bitmask_set( bitmask, frag_off, frag_len );
5771 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5772 msg_len ) == 0 );
5773 }
5774 else
5775 {
5776 hs_buf->is_complete = 1;
5777 }
5778
5779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5780 hs_buf->is_complete ? "" : "not yet " ) );
5781 }
5782
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005783 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005784 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005785
5786 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005787 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005788 break;
5789 }
5790
5791exit:
5792
5793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5794 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005795}
5796#endif /* MBEDTLS_SSL_PROTO_DTLS */
5797
Hanno Becker1097b342018-08-15 14:09:41 +01005798static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005799{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005800 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005801 * Consume last content-layer message and potentially
5802 * update in_msglen which keeps track of the contents'
5803 * consumption state.
5804 *
5805 * (1) Handshake messages:
5806 * Remove last handshake message, move content
5807 * and adapt in_msglen.
5808 *
5809 * (2) Alert messages:
5810 * Consume whole record content, in_msglen = 0.
5811 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005812 * (3) Change cipher spec:
5813 * Consume whole record content, in_msglen = 0.
5814 *
5815 * (4) Application data:
5816 * Don't do anything - the record layer provides
5817 * the application data as a stream transport
5818 * and consumes through mbedtls_ssl_read only.
5819 *
5820 */
5821
5822 /* Case (1): Handshake messages */
5823 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005824 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005825 /* Hard assertion to be sure that no application data
5826 * is in flight, as corrupting ssl->in_msglen during
5827 * ssl->in_offt != NULL is fatal. */
5828 if( ssl->in_offt != NULL )
5829 {
5830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5831 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5832 }
5833
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005834 /*
5835 * Get next Handshake message in the current record
5836 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005837
Hanno Becker4a810fb2017-05-24 16:27:30 +01005838 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005839 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005840 * current handshake content: If DTLS handshake
5841 * fragmentation is used, that's the fragment
5842 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005843 * size here is faulty and should be changed at
5844 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005845 * (2) While it doesn't seem to cause problems, one
5846 * has to be very careful not to assume that in_hslen
5847 * is always <= in_msglen in a sensible communication.
5848 * Again, it's wrong for DTLS handshake fragmentation.
5849 * The following check is therefore mandatory, and
5850 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005851 * Additionally, ssl->in_hslen might be arbitrarily out of
5852 * bounds after handling a DTLS message with an unexpected
5853 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005854 */
5855 if( ssl->in_hslen < ssl->in_msglen )
5856 {
5857 ssl->in_msglen -= ssl->in_hslen;
5858 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5859 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005860
Hanno Becker4a810fb2017-05-24 16:27:30 +01005861 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5862 ssl->in_msg, ssl->in_msglen );
5863 }
5864 else
5865 {
5866 ssl->in_msglen = 0;
5867 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005868
Hanno Becker4a810fb2017-05-24 16:27:30 +01005869 ssl->in_hslen = 0;
5870 }
5871 /* Case (4): Application data */
5872 else if( ssl->in_offt != NULL )
5873 {
5874 return( 0 );
5875 }
5876 /* Everything else (CCS & Alerts) */
5877 else
5878 {
5879 ssl->in_msglen = 0;
5880 }
5881
Hanno Becker1097b342018-08-15 14:09:41 +01005882 return( 0 );
5883}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005884
Hanno Beckere74d5562018-08-15 14:26:08 +01005885static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5886{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005887 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005888 return( 1 );
5889
5890 return( 0 );
5891}
5892
Hanno Becker5f066e72018-08-16 14:56:31 +01005893#if defined(MBEDTLS_SSL_PROTO_DTLS)
5894
5895static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5896{
5897 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5898 if( hs == NULL )
5899 return;
5900
Hanno Becker01315ea2018-08-21 17:22:17 +01005901 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005902 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005903 hs->buffering.total_bytes_buffered -=
5904 hs->buffering.future_record.len;
5905
5906 mbedtls_free( hs->buffering.future_record.data );
5907 hs->buffering.future_record.data = NULL;
5908 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005909}
5910
5911static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5912{
5913 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5914 unsigned char * rec;
5915 size_t rec_len;
5916 unsigned rec_epoch;
5917
5918 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5919 return( 0 );
5920
5921 if( hs == NULL )
5922 return( 0 );
5923
Hanno Becker5f066e72018-08-16 14:56:31 +01005924 rec = hs->buffering.future_record.data;
5925 rec_len = hs->buffering.future_record.len;
5926 rec_epoch = hs->buffering.future_record.epoch;
5927
5928 if( rec == NULL )
5929 return( 0 );
5930
Hanno Becker4cb782d2018-08-20 11:19:05 +01005931 /* Only consider loading future records if the
5932 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005933 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005934 return( 0 );
5935
Hanno Becker5f066e72018-08-16 14:56:31 +01005936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5937
5938 if( rec_epoch != ssl->in_epoch )
5939 {
5940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5941 goto exit;
5942 }
5943
5944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5945
5946 /* Double-check that the record is not too large */
5947 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5948 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5949 {
5950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5951 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5952 }
5953
5954 memcpy( ssl->in_hdr, rec, rec_len );
5955 ssl->in_left = rec_len;
5956 ssl->next_record_offset = 0;
5957
5958 ssl_free_buffered_record( ssl );
5959
5960exit:
5961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5962 return( 0 );
5963}
5964
Hanno Becker519f15d2019-07-11 12:43:20 +01005965static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
5966 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01005967{
5968 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01005969
5970 /* Don't buffer future records outside handshakes. */
5971 if( hs == NULL )
5972 return( 0 );
5973
5974 /* Only buffer handshake records (we are only interested
5975 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01005976 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01005977 return( 0 );
5978
5979 /* Don't buffer more than one future epoch record. */
5980 if( hs->buffering.future_record.data != NULL )
5981 return( 0 );
5982
Hanno Becker01315ea2018-08-21 17:22:17 +01005983 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01005984 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01005985 hs->buffering.total_bytes_buffered ) )
5986 {
5987 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 +01005988 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01005989 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005990 return( 0 );
5991 }
5992
Hanno Becker5f066e72018-08-16 14:56:31 +01005993 /* Buffer record */
5994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5995 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01005996 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01005997
5998 /* ssl_parse_record_header() only considers records
5999 * of the next epoch as candidates for buffering. */
6000 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006001 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006002
6003 hs->buffering.future_record.data =
6004 mbedtls_calloc( 1, hs->buffering.future_record.len );
6005 if( hs->buffering.future_record.data == NULL )
6006 {
6007 /* If we run out of RAM trying to buffer a
6008 * record from the next epoch, just ignore. */
6009 return( 0 );
6010 }
6011
Hanno Becker519f15d2019-07-11 12:43:20 +01006012 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006013
Hanno Becker519f15d2019-07-11 12:43:20 +01006014 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006015 return( 0 );
6016}
6017
6018#endif /* MBEDTLS_SSL_PROTO_DTLS */
6019
Hanno Beckere74d5562018-08-15 14:26:08 +01006020static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006021{
6022 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006023 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006024
Hanno Becker5f066e72018-08-16 14:56:31 +01006025#if defined(MBEDTLS_SSL_PROTO_DTLS)
6026 /* We might have buffered a future record; if so,
6027 * and if the epoch matches now, load it.
6028 * On success, this call will set ssl->in_left to
6029 * the length of the buffered record, so that
6030 * the calls to ssl_fetch_input() below will
6031 * essentially be no-ops. */
6032 ret = ssl_load_buffered_record( ssl );
6033 if( ret != 0 )
6034 return( ret );
6035#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006036
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006037 /* Ensure that we have enough space available for the default form
6038 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6039 * with no space for CIDs counted in). */
6040 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6041 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006044 return( ret );
6045 }
6046
Hanno Beckere5e7e782019-07-11 12:29:35 +01006047 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6048 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006051 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006052 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006053 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6054 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006055 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006056 if( ret != 0 )
6057 return( ret );
6058
6059 /* Fall through to handling of unexpected records */
6060 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6061 }
6062
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006063 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6064 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006065#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006066 /* Reset in pointers to default state for TLS/DTLS records,
6067 * assuming no CID and no offset between record content and
6068 * record plaintext. */
6069 ssl_update_in_pointers( ssl );
6070
Hanno Becker7ae20e02019-07-12 08:33:49 +01006071 /* Setup internal message pointers from record structure. */
6072 ssl->in_msgtype = rec.type;
6073#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6074 ssl->in_len = ssl->in_cid + rec.cid_len;
6075#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6076 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6077 ssl->in_msglen = rec.data_len;
6078
Hanno Becker2fddd372019-07-10 14:37:41 +01006079 ret = ssl_check_client_reconnect( ssl );
6080 if( ret != 0 )
6081 return( ret );
6082#endif
6083
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006084 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006085 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006086
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6088 "(header)" ) );
6089 }
6090 else
6091 {
6092 /* Skip invalid record and the rest of the datagram */
6093 ssl->next_record_offset = 0;
6094 ssl->in_left = 0;
6095
6096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6097 "(header)" ) );
6098 }
6099
6100 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006101 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006102 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006103 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006104#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006105 {
6106 return( ret );
6107 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006108 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006111 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006112 {
Hanno Beckera8814792019-07-10 15:01:45 +01006113 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006114 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006115 if( ssl->next_record_offset < ssl->in_left )
6116 {
6117 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6118 }
6119 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006120 else
6121#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006122 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006123 /*
6124 * Fetch record contents from underlying transport.
6125 */
Hanno Beckera3175662019-07-11 12:50:29 +01006126 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006127 if( ret != 0 )
6128 {
6129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6130 return( ret );
6131 }
6132
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006133 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006134 }
6135
6136 /*
6137 * Decrypt record contents.
6138 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006139
Hanno Beckerfdf66042019-07-11 13:07:45 +01006140 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006143 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006144 {
6145 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006146 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006147 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006148 /* Except when waiting for Finished as a bad mac here
6149 * probably means something went wrong in the handshake
6150 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6151 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6152 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6153 {
6154#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6155 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6156 {
6157 mbedtls_ssl_send_alert_message( ssl,
6158 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6159 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6160 }
6161#endif
6162 return( ret );
6163 }
6164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006166 if( ssl->conf->badmac_limit != 0 &&
6167 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6170 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006171 }
6172#endif
6173
Hanno Becker4a810fb2017-05-24 16:27:30 +01006174 /* As above, invalid records cause
6175 * dismissal of the whole datagram. */
6176
6177 ssl->next_record_offset = 0;
6178 ssl->in_left = 0;
6179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006181 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006182 }
6183
6184 return( ret );
6185 }
6186 else
6187#endif
6188 {
6189 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6191 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 mbedtls_ssl_send_alert_message( ssl,
6194 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6195 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006196 }
6197#endif
6198 return( ret );
6199 }
6200 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006201
Hanno Becker44d89b22019-07-12 09:40:44 +01006202
6203 /* Reset in pointers to default state for TLS/DTLS records,
6204 * assuming no CID and no offset between record content and
6205 * record plaintext. */
6206 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006207#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6208 ssl->in_len = ssl->in_cid + rec.cid_len;
6209#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6210 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006211
Hanno Becker8685c822019-07-12 09:37:30 +01006212 /* The record content type may change during decryption,
6213 * so re-read it. */
6214 ssl->in_msgtype = rec.type;
6215 /* Also update the input buffer, because unfortunately
6216 * the server-side ssl_parse_client_hello() reparses the
6217 * record header when receiving a ClientHello initiating
6218 * a renegotiation. */
6219 ssl->in_hdr[0] = rec.type;
6220 ssl->in_msg = rec.buf + rec.data_offset;
6221 ssl->in_msglen = rec.data_len;
6222 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6223 ssl->in_len[1] = (unsigned char)( rec.data_len );
6224
Simon Butcher99000142016-10-13 17:21:01 +01006225 return( 0 );
6226}
6227
6228int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6229{
6230 int ret;
6231
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006232 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006233 * Handle particular types of records
6234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006236 {
Simon Butcher99000142016-10-13 17:21:01 +01006237 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6238 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006239 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006241 }
6242
Hanno Beckere678eaa2018-08-21 14:57:46 +01006243 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006244 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006245 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006246 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6248 ssl->in_msglen ) );
6249 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006250 }
6251
Hanno Beckere678eaa2018-08-21 14:57:46 +01006252 if( ssl->in_msg[0] != 1 )
6253 {
6254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6255 ssl->in_msg[0] ) );
6256 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6257 }
6258
6259#if defined(MBEDTLS_SSL_PROTO_DTLS)
6260 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6261 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6262 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6263 {
6264 if( ssl->handshake == NULL )
6265 {
6266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6267 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6268 }
6269
6270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6271 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6272 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006273#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006274 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006277 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006278 if( ssl->in_msglen != 2 )
6279 {
6280 /* Note: Standard allows for more than one 2 byte alert
6281 to be packed in a single message, but Mbed TLS doesn't
6282 currently support this. */
6283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6284 ssl->in_msglen ) );
6285 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6286 }
6287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006289 ssl->in_msg[0], ssl->in_msg[1] ) );
6290
6291 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006292 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006297 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006299 }
6300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6302 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6305 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006306 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006307
6308#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6309 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6310 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6311 {
Hanno Becker90333da2017-10-10 11:27:13 +01006312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006313 /* Will be handled when trying to parse ServerHello */
6314 return( 0 );
6315 }
6316#endif
6317
6318#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6319 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6320 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6321 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6322 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6323 {
6324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6325 /* Will be handled in mbedtls_ssl_parse_certificate() */
6326 return( 0 );
6327 }
6328#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6329
6330 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006331 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006332 }
6333
Hanno Beckerc76c6192017-06-06 10:03:17 +01006334#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006335 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006336 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006337 /* Drop unexpected ApplicationData records,
6338 * except at the beginning of renegotiations */
6339 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6340 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6341#if defined(MBEDTLS_SSL_RENEGOTIATION)
6342 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6343 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006344#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006345 )
6346 {
6347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6348 return( MBEDTLS_ERR_SSL_NON_FATAL );
6349 }
6350
6351 if( ssl->handshake != NULL &&
6352 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6353 {
6354 ssl_handshake_wrapup_free_hs_transform( ssl );
6355 }
6356 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006357#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006358
Paul Bakker5121ce52009-01-03 21:22:43 +00006359 return( 0 );
6360}
6361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006363{
6364 int ret;
6365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6367 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6368 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006369 {
6370 return( ret );
6371 }
6372
6373 return( 0 );
6374}
6375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006377 unsigned char level,
6378 unsigned char message )
6379{
6380 int ret;
6381
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006382 if( ssl == NULL || ssl->conf == NULL )
6383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006386 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006389 ssl->out_msglen = 2;
6390 ssl->out_msg[0] = level;
6391 ssl->out_msg[1] = message;
6392
Hanno Becker67bc7c32018-08-06 11:33:50 +01006393 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006396 return( ret );
6397 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006399
6400 return( 0 );
6401}
6402
Hanno Beckerb9d44792019-02-08 07:19:04 +00006403#if defined(MBEDTLS_X509_CRT_PARSE_C)
6404static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6405{
6406#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6407 if( session->peer_cert != NULL )
6408 {
6409 mbedtls_x509_crt_free( session->peer_cert );
6410 mbedtls_free( session->peer_cert );
6411 session->peer_cert = NULL;
6412 }
6413#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6414 if( session->peer_cert_digest != NULL )
6415 {
6416 /* Zeroization is not necessary. */
6417 mbedtls_free( session->peer_cert_digest );
6418 session->peer_cert_digest = NULL;
6419 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6420 session->peer_cert_digest_len = 0;
6421 }
6422#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6423}
6424#endif /* MBEDTLS_X509_CRT_PARSE_C */
6425
Paul Bakker5121ce52009-01-03 21:22:43 +00006426/*
6427 * Handshake functions
6428 */
Hanno Becker21489932019-02-05 13:20:55 +00006429#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006430/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006431int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006432{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006433 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6434 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006437
Hanno Becker7177a882019-02-05 13:36:46 +00006438 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006441 ssl->state++;
6442 return( 0 );
6443 }
6444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6446 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006447}
6448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006450{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006451 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6452 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006455
Hanno Becker7177a882019-02-05 13:36:46 +00006456 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006459 ssl->state++;
6460 return( 0 );
6461 }
6462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6464 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006465}
Gilles Peskinef9828522017-05-03 12:28:43 +02006466
Hanno Becker21489932019-02-05 13:20:55 +00006467#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006468/* Some certificate support -> implement write and parse */
6469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006473 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006475 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6476 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006479
Hanno Becker7177a882019-02-05 13:36:46 +00006480 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006483 ssl->state++;
6484 return( 0 );
6485 }
6486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006488 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006489 {
6490 if( ssl->client_auth == 0 )
6491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006493 ssl->state++;
6494 return( 0 );
6495 }
6496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006498 /*
6499 * If using SSLv3 and got no cert, send an Alert message
6500 * (otherwise an empty Certificate message will be sent).
6501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6503 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006504 {
6505 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6507 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6508 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006511 goto write_msg;
6512 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006514 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515#endif /* MBEDTLS_SSL_CLI_C */
6516#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006517 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6522 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006523 }
6524 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006525#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006528
6529 /*
6530 * 0 . 0 handshake type
6531 * 1 . 3 handshake length
6532 * 4 . 6 length of all certs
6533 * 7 . 9 length of cert. 1
6534 * 10 . n-1 peer certificate
6535 * n . n+2 length of cert. 2
6536 * n+3 . ... upper level cert, etc.
6537 */
6538 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006540
Paul Bakker29087132010-03-21 21:03:34 +00006541 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006542 {
6543 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006544 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006547 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006549 }
6550
6551 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6552 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6553 ssl->out_msg[i + 2] = (unsigned char)( n );
6554
6555 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6556 i += n; crt = crt->next;
6557 }
6558
6559 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6560 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6561 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6562
6563 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6565 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006566
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006567#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006568write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006569#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006570
6571 ssl->state++;
6572
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006573 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006574 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006576 return( ret );
6577 }
6578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006580
Paul Bakkered27a042013-04-18 22:46:23 +02006581 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006582}
6583
Hanno Becker84879e32019-01-31 07:44:03 +00006584#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006585
6586#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006587static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6588 unsigned char *crt_buf,
6589 size_t crt_buf_len )
6590{
6591 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6592
6593 if( peer_crt == NULL )
6594 return( -1 );
6595
6596 if( peer_crt->raw.len != crt_buf_len )
6597 return( -1 );
6598
Hanno Becker46f34d02019-02-08 14:00:04 +00006599 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006600}
Hanno Becker177475a2019-02-05 17:02:46 +00006601#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6602static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6603 unsigned char *crt_buf,
6604 size_t crt_buf_len )
6605{
6606 int ret;
6607 unsigned char const * const peer_cert_digest =
6608 ssl->session->peer_cert_digest;
6609 mbedtls_md_type_t const peer_cert_digest_type =
6610 ssl->session->peer_cert_digest_type;
6611 mbedtls_md_info_t const * const digest_info =
6612 mbedtls_md_info_from_type( peer_cert_digest_type );
6613 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6614 size_t digest_len;
6615
6616 if( peer_cert_digest == NULL || digest_info == NULL )
6617 return( -1 );
6618
6619 digest_len = mbedtls_md_get_size( digest_info );
6620 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6621 return( -1 );
6622
6623 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6624 if( ret != 0 )
6625 return( -1 );
6626
6627 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6628}
6629#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006630#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006631
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006632/*
6633 * Once the certificate message is read, parse it into a cert chain and
6634 * perform basic checks, but leave actual verification to the caller
6635 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006636static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6637 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006638{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006639 int ret;
6640#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6641 int crt_cnt=0;
6642#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006643 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006644 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006649 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6650 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006652 }
6653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6655 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006658 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6659 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006661 }
6662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006664
Paul Bakker5121ce52009-01-03 21:22:43 +00006665 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006667 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006668 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006669
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006670 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006674 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6675 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006677 }
6678
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006679 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6680 i += 3;
6681
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006682 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006683 while( i < ssl->in_hslen )
6684 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006685 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006686 if ( i + 3 > ssl->in_hslen ) {
6687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006688 mbedtls_ssl_send_alert_message( ssl,
6689 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6690 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006691 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6692 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006693 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6694 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006695 if( ssl->in_msg[i] != 0 )
6696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006698 mbedtls_ssl_send_alert_message( ssl,
6699 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6700 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006702 }
6703
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006704 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006705 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6706 | (unsigned int) ssl->in_msg[i + 2];
6707 i += 3;
6708
6709 if( n < 128 || i + n > ssl->in_hslen )
6710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006712 mbedtls_ssl_send_alert_message( ssl,
6713 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6714 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006716 }
6717
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006718 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006719#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6720 if( crt_cnt++ == 0 &&
6721 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6722 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006723 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006724 /* During client-side renegotiation, check that the server's
6725 * end-CRTs hasn't changed compared to the initial handshake,
6726 * mitigating the triple handshake attack. On success, reuse
6727 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6729 if( ssl_check_peer_crt_unchanged( ssl,
6730 &ssl->in_msg[i],
6731 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006732 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6734 mbedtls_ssl_send_alert_message( ssl,
6735 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6736 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6737 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006738 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006739
6740 /* Now we can safely free the original chain. */
6741 ssl_clear_peer_cert( ssl->session );
6742 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006743#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6744
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006745 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006746#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006747 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006748#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006749 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006750 * it in-place from the input buffer instead of making a copy. */
6751 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6752#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006753 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006754 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006755 case 0: /*ok*/
6756 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6757 /* Ignore certificate with an unknown algorithm: maybe a
6758 prior certificate was already trusted. */
6759 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006760
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006761 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6762 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6763 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006764
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006765 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6766 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6767 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006768
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006769 default:
6770 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6771 crt_parse_der_failed:
6772 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6773 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6774 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006775 }
6776
6777 i += n;
6778 }
6779
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006780 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006781 return( 0 );
6782}
6783
Hanno Becker4a55f632019-02-05 12:49:06 +00006784#if defined(MBEDTLS_SSL_SRV_C)
6785static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6786{
6787 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6788 return( -1 );
6789
6790#if defined(MBEDTLS_SSL_PROTO_SSL3)
6791 /*
6792 * Check if the client sent an empty certificate
6793 */
6794 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6795 {
6796 if( ssl->in_msglen == 2 &&
6797 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6798 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6799 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6800 {
6801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6802 return( 0 );
6803 }
6804
6805 return( -1 );
6806 }
6807#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6808
6809#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6810 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6811 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6812 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6813 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6814 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6815 {
6816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6817 return( 0 );
6818 }
6819
6820 return( -1 );
6821#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6822 MBEDTLS_SSL_PROTO_TLS1_2 */
6823}
6824#endif /* MBEDTLS_SSL_SRV_C */
6825
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006826/* Check if a certificate message is expected.
6827 * Return either
6828 * - SSL_CERTIFICATE_EXPECTED, or
6829 * - SSL_CERTIFICATE_SKIP
6830 * indicating whether a Certificate message is expected or not.
6831 */
6832#define SSL_CERTIFICATE_EXPECTED 0
6833#define SSL_CERTIFICATE_SKIP 1
6834static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6835 int authmode )
6836{
6837 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006838 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006839
6840 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6841 return( SSL_CERTIFICATE_SKIP );
6842
6843#if defined(MBEDTLS_SSL_SRV_C)
6844 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6845 {
6846 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6847 return( SSL_CERTIFICATE_SKIP );
6848
6849 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6850 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006851 ssl->session_negotiate->verify_result =
6852 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6853 return( SSL_CERTIFICATE_SKIP );
6854 }
6855 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006856#else
6857 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006858#endif /* MBEDTLS_SSL_SRV_C */
6859
6860 return( SSL_CERTIFICATE_EXPECTED );
6861}
6862
Hanno Becker68636192019-02-05 14:36:34 +00006863static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6864 int authmode,
6865 mbedtls_x509_crt *chain,
6866 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006867{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006868 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006869 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006870 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006871 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006872
Hanno Becker8927c832019-04-03 12:52:50 +01006873 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6874 void *p_vrfy;
6875
Hanno Becker68636192019-02-05 14:36:34 +00006876 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6877 return( 0 );
6878
Hanno Becker8927c832019-04-03 12:52:50 +01006879 if( ssl->f_vrfy != NULL )
6880 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006881 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006882 f_vrfy = ssl->f_vrfy;
6883 p_vrfy = ssl->p_vrfy;
6884 }
6885 else
6886 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006888 f_vrfy = ssl->conf->f_vrfy;
6889 p_vrfy = ssl->conf->p_vrfy;
6890 }
6891
Hanno Becker68636192019-02-05 14:36:34 +00006892 /*
6893 * Main check: verify certificate
6894 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006895#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6896 if( ssl->conf->f_ca_cb != NULL )
6897 {
6898 ((void) rs_ctx);
6899 have_ca_chain = 1;
6900
6901 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006902 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006903 chain,
6904 ssl->conf->f_ca_cb,
6905 ssl->conf->p_ca_cb,
6906 ssl->conf->cert_profile,
6907 ssl->hostname,
6908 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006909 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006910 }
6911 else
6912#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6913 {
6914 mbedtls_x509_crt *ca_chain;
6915 mbedtls_x509_crl *ca_crl;
6916
6917#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6918 if( ssl->handshake->sni_ca_chain != NULL )
6919 {
6920 ca_chain = ssl->handshake->sni_ca_chain;
6921 ca_crl = ssl->handshake->sni_ca_crl;
6922 }
6923 else
6924#endif
6925 {
6926 ca_chain = ssl->conf->ca_chain;
6927 ca_crl = ssl->conf->ca_crl;
6928 }
6929
6930 if( ca_chain != NULL )
6931 have_ca_chain = 1;
6932
6933 ret = mbedtls_x509_crt_verify_restartable(
6934 chain,
6935 ca_chain, ca_crl,
6936 ssl->conf->cert_profile,
6937 ssl->hostname,
6938 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006939 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006940 }
Hanno Becker68636192019-02-05 14:36:34 +00006941
6942 if( ret != 0 )
6943 {
6944 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6945 }
6946
6947#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6948 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6949 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6950#endif
6951
6952 /*
6953 * Secondary checks: always done, but change 'ret' only if it was 0
6954 */
6955
6956#if defined(MBEDTLS_ECP_C)
6957 {
6958 const mbedtls_pk_context *pk = &chain->pk;
6959
6960 /* If certificate uses an EC key, make sure the curve is OK */
6961 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6962 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6963 {
6964 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6965
6966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6967 if( ret == 0 )
6968 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6969 }
6970 }
6971#endif /* MBEDTLS_ECP_C */
6972
6973 if( mbedtls_ssl_check_cert_usage( chain,
6974 ciphersuite_info,
6975 ! ssl->conf->endpoint,
6976 &ssl->session_negotiate->verify_result ) != 0 )
6977 {
6978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6979 if( ret == 0 )
6980 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6981 }
6982
6983 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6984 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6985 * with details encoded in the verification flags. All other kinds
6986 * of error codes, including those from the user provided f_vrfy
6987 * functions, are treated as fatal and lead to a failure of
6988 * ssl_parse_certificate even if verification was optional. */
6989 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6990 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6991 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6992 {
6993 ret = 0;
6994 }
6995
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006996 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006997 {
6998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6999 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7000 }
7001
7002 if( ret != 0 )
7003 {
7004 uint8_t alert;
7005
7006 /* The certificate may have been rejected for several reasons.
7007 Pick one and send the corresponding alert. Which alert to send
7008 may be a subject of debate in some cases. */
7009 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7010 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7011 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7012 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7013 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7014 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7015 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7016 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7017 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7018 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7019 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7020 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7021 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7022 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7023 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7024 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7025 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7026 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7027 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7028 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7029 else
7030 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7031 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7032 alert );
7033 }
7034
7035#if defined(MBEDTLS_DEBUG_C)
7036 if( ssl->session_negotiate->verify_result != 0 )
7037 {
7038 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7039 ssl->session_negotiate->verify_result ) );
7040 }
7041 else
7042 {
7043 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7044 }
7045#endif /* MBEDTLS_DEBUG_C */
7046
7047 return( ret );
7048}
7049
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007050#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7051static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7052 unsigned char *start, size_t len )
7053{
7054 int ret;
7055 /* Remember digest of the peer's end-CRT. */
7056 ssl->session_negotiate->peer_cert_digest =
7057 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7058 if( ssl->session_negotiate->peer_cert_digest == NULL )
7059 {
7060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7061 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7062 mbedtls_ssl_send_alert_message( ssl,
7063 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7064 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7065
7066 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7067 }
7068
7069 ret = mbedtls_md( mbedtls_md_info_from_type(
7070 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7071 start, len,
7072 ssl->session_negotiate->peer_cert_digest );
7073
7074 ssl->session_negotiate->peer_cert_digest_type =
7075 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7076 ssl->session_negotiate->peer_cert_digest_len =
7077 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7078
7079 return( ret );
7080}
7081
7082static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7083 unsigned char *start, size_t len )
7084{
7085 unsigned char *end = start + len;
7086 int ret;
7087
7088 /* Make a copy of the peer's raw public key. */
7089 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7090 ret = mbedtls_pk_parse_subpubkey( &start, end,
7091 &ssl->handshake->peer_pubkey );
7092 if( ret != 0 )
7093 {
7094 /* We should have parsed the public key before. */
7095 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7096 }
7097
7098 return( 0 );
7099}
7100#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7101
Hanno Becker68636192019-02-05 14:36:34 +00007102int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7103{
7104 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007105 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007106#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7107 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7108 ? ssl->handshake->sni_authmode
7109 : ssl->conf->authmode;
7110#else
7111 const int authmode = ssl->conf->authmode;
7112#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007113 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007114 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007115
7116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7117
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007118 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7119 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007120 {
7121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007122 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007123 }
7124
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007125#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7126 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007127 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007128 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007129 chain = ssl->handshake->ecrs_peer_cert;
7130 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007131 goto crt_verify;
7132 }
7133#endif
7134
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007135 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007136 {
7137 /* mbedtls_ssl_read_record may have sent an alert already. We
7138 let it decide whether to alert. */
7139 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007140 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007141 }
7142
Hanno Becker4a55f632019-02-05 12:49:06 +00007143#if defined(MBEDTLS_SSL_SRV_C)
7144 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7145 {
7146 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007147
7148 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007149 ret = 0;
7150 else
7151 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007152
Hanno Becker6bdfab22019-02-05 13:11:17 +00007153 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007154 }
7155#endif /* MBEDTLS_SSL_SRV_C */
7156
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007157 /* Clear existing peer CRT structure in case we tried to
7158 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007159 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007160
7161 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7162 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007163 {
7164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7165 sizeof( mbedtls_x509_crt ) ) );
7166 mbedtls_ssl_send_alert_message( ssl,
7167 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7168 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007169
Hanno Becker3dad3112019-02-05 17:19:52 +00007170 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7171 goto exit;
7172 }
7173 mbedtls_x509_crt_init( chain );
7174
7175 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007176 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007177 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007178
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007179#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7180 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007181 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007182
7183crt_verify:
7184 if( ssl->handshake->ecrs_enabled)
7185 rs_ctx = &ssl->handshake->ecrs_ctx;
7186#endif
7187
Hanno Becker68636192019-02-05 14:36:34 +00007188 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007189 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007190 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007191 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007192
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007193#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007194 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007195 unsigned char *crt_start, *pk_start;
7196 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007197
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007198 /* We parse the CRT chain without copying, so
7199 * these pointers point into the input buffer,
7200 * and are hence still valid after freeing the
7201 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007202
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007203 crt_start = chain->raw.p;
7204 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007205
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007206 pk_start = chain->pk_raw.p;
7207 pk_len = chain->pk_raw.len;
7208
7209 /* Free the CRT structures before computing
7210 * digest and copying the peer's public key. */
7211 mbedtls_x509_crt_free( chain );
7212 mbedtls_free( chain );
7213 chain = NULL;
7214
7215 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007216 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007217 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007218
7219 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7220 if( ret != 0 )
7221 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007222 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007223#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7224 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007225 ssl->session_negotiate->peer_cert = chain;
7226 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007227#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007230
Hanno Becker6bdfab22019-02-05 13:11:17 +00007231exit:
7232
Hanno Becker3dad3112019-02-05 17:19:52 +00007233 if( ret == 0 )
7234 ssl->state++;
7235
7236#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7237 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7238 {
7239 ssl->handshake->ecrs_peer_cert = chain;
7240 chain = NULL;
7241 }
7242#endif
7243
7244 if( chain != NULL )
7245 {
7246 mbedtls_x509_crt_free( chain );
7247 mbedtls_free( chain );
7248 }
7249
Paul Bakker5121ce52009-01-03 21:22:43 +00007250 return( ret );
7251}
Hanno Becker21489932019-02-05 13:20:55 +00007252#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007255{
7256 int ret;
7257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007261 ssl->out_msglen = 1;
7262 ssl->out_msg[0] = 1;
7263
Paul Bakker5121ce52009-01-03 21:22:43 +00007264 ssl->state++;
7265
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007266 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007267 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007268 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007269 return( ret );
7270 }
7271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007273
7274 return( 0 );
7275}
7276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007278{
7279 int ret;
7280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007282
Hanno Becker327c93b2018-08-15 13:56:18 +01007283 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007286 return( ret );
7287 }
7288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007292 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7293 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007295 }
7296
Hanno Beckere678eaa2018-08-21 14:57:46 +01007297 /* CCS records are only accepted if they have length 1 and content '1',
7298 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007299
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007300 /*
7301 * Switch to our negotiated transform and session parameters for inbound
7302 * data.
7303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007305 ssl->transform_in = ssl->transform_negotiate;
7306 ssl->session_in = ssl->session_negotiate;
7307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007309 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007312 ssl_dtls_replay_reset( ssl );
7313#endif
7314
7315 /* Increment epoch */
7316 if( ++ssl->in_epoch == 0 )
7317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007319 /* This is highly unlikely to happen for legitimate reasons, so
7320 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007322 }
7323 }
7324 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007326 memset( ssl->in_ctr, 0, 8 );
7327
Hanno Becker79594fd2019-05-08 09:38:41 +01007328 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7331 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007336 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7337 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007339 }
7340 }
7341#endif
7342
Paul Bakker5121ce52009-01-03 21:22:43 +00007343 ssl->state++;
7344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007346
7347 return( 0 );
7348}
7349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7351 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007352{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007353 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7356 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7357 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007358 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007359 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7362#if defined(MBEDTLS_SHA512_C)
7363 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007364 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7365 else
7366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367#if defined(MBEDTLS_SHA256_C)
7368 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007369 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007370 else
7371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007375 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007376 }
Paul Bakker380da532012-04-18 16:10:25 +00007377}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7382 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007383 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7384 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007385#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7387#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007388#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007389 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007390 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7391#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007392 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007393#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007396#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007397 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007398 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007399#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007400 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007401#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007402#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007404}
7405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007407 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7410 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007411 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7412 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007413#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7415#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007416#if defined(MBEDTLS_USE_PSA_CRYPTO)
7417 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7418#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007419 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007420#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007421#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007423#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007424 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007425#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007426 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007427#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007428#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007430}
7431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7433 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7434static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007435 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007436{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007437 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7438 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007439}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007440#endif
Paul Bakker380da532012-04-18 16:10:25 +00007441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7443#if defined(MBEDTLS_SHA256_C)
7444static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007445 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007446{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007447#if defined(MBEDTLS_USE_PSA_CRYPTO)
7448 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7449#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007450 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007451#endif
Paul Bakker380da532012-04-18 16:10:25 +00007452}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007453#endif
Paul Bakker380da532012-04-18 16:10:25 +00007454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455#if defined(MBEDTLS_SHA512_C)
7456static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007457 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007458{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007459#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007460 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007461#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007462 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007463#endif
Paul Bakker380da532012-04-18 16:10:25 +00007464}
Paul Bakker769075d2012-11-24 11:26:46 +01007465#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007469static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007471{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007472 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007473 mbedtls_md5_context md5;
7474 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007475
Paul Bakker5121ce52009-01-03 21:22:43 +00007476 unsigned char padbuf[48];
7477 unsigned char md5sum[16];
7478 unsigned char sha1sum[20];
7479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007481 if( !session )
7482 session = ssl->session;
7483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007485
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007486 mbedtls_md5_init( &md5 );
7487 mbedtls_sha1_init( &sha1 );
7488
7489 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7490 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007491
7492 /*
7493 * SSLv3:
7494 * hash =
7495 * MD5( master + pad2 +
7496 * MD5( handshake + sender + master + pad1 ) )
7497 * + SHA1( master + pad2 +
7498 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007499 */
7500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007502 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7503 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007504#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007507 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7508 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007512 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007513
Paul Bakker1ef83d62012-04-11 12:09:53 +00007514 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007515
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007516 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7517 mbedtls_md5_update_ret( &md5, session->master, 48 );
7518 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7519 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007520
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007521 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7522 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7523 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7524 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007525
Paul Bakker1ef83d62012-04-11 12:09:53 +00007526 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007527
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007528 mbedtls_md5_starts_ret( &md5 );
7529 mbedtls_md5_update_ret( &md5, session->master, 48 );
7530 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7531 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7532 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007533
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007534 mbedtls_sha1_starts_ret( &sha1 );
7535 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7536 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7537 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7538 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007541
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007542 mbedtls_md5_free( &md5 );
7543 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007544
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007545 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7546 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7547 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007554static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007556{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007557 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007558 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007559 mbedtls_md5_context md5;
7560 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007561 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007564 if( !session )
7565 session = ssl->session;
7566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007568
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007569 mbedtls_md5_init( &md5 );
7570 mbedtls_sha1_init( &sha1 );
7571
7572 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7573 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007574
Paul Bakker1ef83d62012-04-11 12:09:53 +00007575 /*
7576 * TLSv1:
7577 * hash = PRF( master, finished_label,
7578 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7579 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007582 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7583 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007584#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007587 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7588 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007589#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007592 ? "client finished"
7593 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007594
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007595 mbedtls_md5_finish_ret( &md5, padbuf );
7596 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007597
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007598 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007599 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007602
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007603 mbedtls_md5_free( &md5 );
7604 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007605
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007606 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007609}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7613#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007614static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007616{
7617 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007618 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007619 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007620#if defined(MBEDTLS_USE_PSA_CRYPTO)
7621 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007622 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007623 psa_status_t status;
7624#else
7625 mbedtls_sha256_context sha256;
7626#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007629 if( !session )
7630 session = ssl->session;
7631
Andrzej Kurekeb342242019-01-29 09:14:33 -05007632 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7633 ? "client finished"
7634 : "server finished";
7635
7636#if defined(MBEDTLS_USE_PSA_CRYPTO)
7637 sha256_psa = psa_hash_operation_init();
7638
7639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7640
7641 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7642 if( status != PSA_SUCCESS )
7643 {
7644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7645 return;
7646 }
7647
7648 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7649 if( status != PSA_SUCCESS )
7650 {
7651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7652 return;
7653 }
7654 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7655#else
7656
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007657 mbedtls_sha256_init( &sha256 );
7658
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007660
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007661 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007662
7663 /*
7664 * TLSv1.2:
7665 * hash = PRF( master, finished_label,
7666 * Hash( handshake ) )[0.11]
7667 */
7668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669#if !defined(MBEDTLS_SHA256_ALT)
7670 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007671 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007672#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007673
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007674 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007675 mbedtls_sha256_free( &sha256 );
7676#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007677
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007678 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007679 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007682
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007683 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007690static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007692{
7693 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007694 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007695 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007696#if defined(MBEDTLS_USE_PSA_CRYPTO)
7697 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007698 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007699 psa_status_t status;
7700#else
7701 mbedtls_sha512_context sha512;
7702#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007705 if( !session )
7706 session = ssl->session;
7707
Andrzej Kurekeb342242019-01-29 09:14:33 -05007708 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7709 ? "client finished"
7710 : "server finished";
7711
7712#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007713 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007714
7715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7716
Andrzej Kurek972fba52019-01-30 03:29:12 -05007717 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007718 if( status != PSA_SUCCESS )
7719 {
7720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7721 return;
7722 }
7723
Andrzej Kurek972fba52019-01-30 03:29:12 -05007724 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007725 if( status != PSA_SUCCESS )
7726 {
7727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7728 return;
7729 }
7730 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7731#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007732 mbedtls_sha512_init( &sha512 );
7733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007735
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007736 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007737
7738 /*
7739 * TLSv1.2:
7740 * hash = PRF( master, finished_label,
7741 * Hash( handshake ) )[0.11]
7742 */
7743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007745 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7746 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007747#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007748
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007749 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007750 mbedtls_sha512_free( &sha512 );
7751#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007752
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007753 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007754 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007757
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007758 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007761}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762#endif /* MBEDTLS_SHA512_C */
7763#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007768
7769 /*
7770 * Free our handshake params
7771 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007772 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007774 ssl->handshake = NULL;
7775
7776 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007777 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007778 */
7779 if( ssl->transform )
7780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781 mbedtls_ssl_transform_free( ssl->transform );
7782 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007783 }
7784 ssl->transform = ssl->transform_negotiate;
7785 ssl->transform_negotiate = NULL;
7786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007788}
7789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007791{
7792 int resume = ssl->handshake->resume;
7793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796#if defined(MBEDTLS_SSL_RENEGOTIATION)
7797 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007800 ssl->renego_records_seen = 0;
7801 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007802#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007803
7804 /*
7805 * Free the previous session and switch in the current one
7806 */
Paul Bakker0a597072012-09-25 21:55:46 +00007807 if( ssl->session )
7808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007810 /* RFC 7366 3.1: keep the EtM state */
7811 ssl->session_negotiate->encrypt_then_mac =
7812 ssl->session->encrypt_then_mac;
7813#endif
7814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 mbedtls_ssl_session_free( ssl->session );
7816 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007817 }
7818 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007819 ssl->session_negotiate = NULL;
7820
Paul Bakker0a597072012-09-25 21:55:46 +00007821 /*
7822 * Add cache entry
7823 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007824 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007825 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007826 resume == 0 )
7827 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007828 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007830 }
Paul Bakker0a597072012-09-25 21:55:46 +00007831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007833 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007834 ssl->handshake->flight != NULL )
7835 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007836 /* Cancel handshake timer */
7837 ssl_set_timer( ssl, 0 );
7838
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007839 /* Keep last flight around in case we need to resend it:
7840 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007842 }
7843 else
7844#endif
7845 ssl_handshake_wrapup_free_hs_transform( ssl );
7846
Paul Bakker48916f92012-09-16 19:57:18 +00007847 ssl->state++;
7848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007850}
7851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007853{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007854 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007857
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007858 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007859
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007860 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007861
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007862 /*
7863 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7864 * may define some other value. Currently (early 2016), no defined
7865 * ciphersuite does this (and this is unlikely to change as activity has
7866 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007871 ssl->verify_data_len = hash_len;
7872 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007873#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007874
Paul Bakker5121ce52009-01-03 21:22:43 +00007875 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7877 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007878
7879 /*
7880 * In case of session resuming, invert the client and server
7881 * ChangeCipherSpec messages order.
7882 */
Paul Bakker0a597072012-09-25 21:55:46 +00007883 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007886 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007888#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007890 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007892#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007893 }
7894 else
7895 ssl->state++;
7896
Paul Bakker48916f92012-09-16 19:57:18 +00007897 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007898 * Switch to our negotiated transform and session parameters for outbound
7899 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007904 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007905 {
7906 unsigned char i;
7907
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007908 /* Remember current epoch settings for resending */
7909 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007910 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007911
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007912 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007913 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007914
7915 /* Increment epoch */
7916 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007917 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007918 break;
7919
7920 /* The loop goes to its end iff the counter is wrapping */
7921 if( i == 0 )
7922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7924 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007925 }
7926 }
7927 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007929 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007930
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007931 ssl->transform_out = ssl->transform_negotiate;
7932 ssl->session_out = ssl->session_negotiate;
7933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7935 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7940 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007941 }
7942 }
7943#endif
7944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007946 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007948#endif
7949
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007950 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007951 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007953 return( ret );
7954 }
7955
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007956#if defined(MBEDTLS_SSL_PROTO_DTLS)
7957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7958 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7959 {
7960 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7961 return( ret );
7962 }
7963#endif
7964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007966
7967 return( 0 );
7968}
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007971#define SSL_MAX_HASH_LEN 36
7972#else
7973#define SSL_MAX_HASH_LEN 12
7974#endif
7975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007977{
Paul Bakker23986e52011-04-24 08:57:21 +00007978 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007979 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007980 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007983
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007984 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007985
Hanno Becker327c93b2018-08-15 13:56:18 +01007986 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007989 return( ret );
7990 }
7991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007995 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7996 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007998 }
7999
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008000 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001#if defined(MBEDTLS_SSL_PROTO_SSL3)
8002 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008003 hash_len = 36;
8004 else
8005#endif
8006 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008008 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8009 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008012 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8013 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008014 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008015 }
8016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008018 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008021 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8022 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008024 }
8025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008027 ssl->verify_data_len = hash_len;
8028 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008029#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008030
Paul Bakker0a597072012-09-25 21:55:46 +00008031 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008034 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008038 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008040#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008041 }
8042 else
8043 ssl->state++;
8044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008046 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008047 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008048#endif
8049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008051
8052 return( 0 );
8053}
8054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008057 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8060 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8061 mbedtls_md5_init( &handshake->fin_md5 );
8062 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008063 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8064 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008066#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8067#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008068#if defined(MBEDTLS_USE_PSA_CRYPTO)
8069 handshake->fin_sha256_psa = psa_hash_operation_init();
8070 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8071#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008073 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008074#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008075#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008077#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008078 handshake->fin_sha384_psa = psa_hash_operation_init();
8079 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008080#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008082 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008083#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008086
8087 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008088
8089#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8090 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8091 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8092#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094#if defined(MBEDTLS_DHM_C)
8095 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008096#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097#if defined(MBEDTLS_ECDH_C)
8098 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008099#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008100#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008101 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008102#if defined(MBEDTLS_SSL_CLI_C)
8103 handshake->ecjpake_cache = NULL;
8104 handshake->ecjpake_cache_len = 0;
8105#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008106#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008107
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008108#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008109 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008110#endif
8111
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008112#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8113 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8114#endif
Hanno Becker75173122019-02-06 16:18:31 +00008115
8116#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8117 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8118 mbedtls_pk_init( &handshake->peer_pubkey );
8119#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008120}
8121
Hanno Beckera18d1322018-01-03 14:27:32 +00008122void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008124 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8127 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008128
Hanno Beckerd56ed242018-01-03 15:32:51 +00008129#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008130 mbedtls_md_init( &transform->md_ctx_enc );
8131 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008132#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008133}
8134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008136{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008138}
8139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008141{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008142 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008143 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008145 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008147 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008148 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008149
8150 /*
8151 * Either the pointers are now NULL or cleared properly and can be freed.
8152 * Now allocate missing structures.
8153 */
8154 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008155 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008156 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008157 }
Paul Bakker48916f92012-09-16 19:57:18 +00008158
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008159 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008160 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008161 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008162 }
Paul Bakker48916f92012-09-16 19:57:18 +00008163
Paul Bakker82788fb2014-10-20 13:59:19 +02008164 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008165 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008166 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008167 }
Paul Bakker48916f92012-09-16 19:57:18 +00008168
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008169 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008170 if( ssl->handshake == NULL ||
8171 ssl->transform_negotiate == NULL ||
8172 ssl->session_negotiate == NULL )
8173 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176 mbedtls_free( ssl->handshake );
8177 mbedtls_free( ssl->transform_negotiate );
8178 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008179
8180 ssl->handshake = NULL;
8181 ssl->transform_negotiate = NULL;
8182 ssl->session_negotiate = NULL;
8183
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008184 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008185 }
8186
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008187 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008189 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008190 ssl_handshake_params_init( ssl->handshake );
8191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008193 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8194 {
8195 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008196
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008197 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8198 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8199 else
8200 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008201
8202 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008203 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008204#endif
8205
Paul Bakker48916f92012-09-16 19:57:18 +00008206 return( 0 );
8207}
8208
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008209#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008210/* Dummy cookie callbacks for defaults */
8211static int ssl_cookie_write_dummy( void *ctx,
8212 unsigned char **p, unsigned char *end,
8213 const unsigned char *cli_id, size_t cli_id_len )
8214{
8215 ((void) ctx);
8216 ((void) p);
8217 ((void) end);
8218 ((void) cli_id);
8219 ((void) cli_id_len);
8220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008222}
8223
8224static int ssl_cookie_check_dummy( void *ctx,
8225 const unsigned char *cookie, size_t cookie_len,
8226 const unsigned char *cli_id, size_t cli_id_len )
8227{
8228 ((void) ctx);
8229 ((void) cookie);
8230 ((void) cookie_len);
8231 ((void) cli_id);
8232 ((void) cli_id_len);
8233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008234 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008235}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008236#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008237
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008238/* Once ssl->out_hdr as the address of the beginning of the
8239 * next outgoing record is set, deduce the other pointers.
8240 *
8241 * Note: For TLS, we save the implicit record sequence number
8242 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8243 * and the caller has to make sure there's space for this.
8244 */
8245
8246static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8247 mbedtls_ssl_transform *transform )
8248{
8249#if defined(MBEDTLS_SSL_PROTO_DTLS)
8250 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8251 {
8252 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008253#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008254 ssl->out_cid = ssl->out_ctr + 8;
8255 ssl->out_len = ssl->out_cid;
8256 if( transform != NULL )
8257 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008258#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008259 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008260#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008261 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008262 }
8263 else
8264#endif
8265 {
8266 ssl->out_ctr = ssl->out_hdr - 8;
8267 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008268#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008269 ssl->out_cid = ssl->out_len;
8270#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008271 ssl->out_iv = ssl->out_hdr + 5;
8272 }
8273
8274 /* Adjust out_msg to make space for explicit IV, if used. */
8275 if( transform != NULL &&
8276 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8277 {
8278 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8279 }
8280 else
8281 ssl->out_msg = ssl->out_iv;
8282}
8283
8284/* Once ssl->in_hdr as the address of the beginning of the
8285 * next incoming record is set, deduce the other pointers.
8286 *
8287 * Note: For TLS, we save the implicit record sequence number
8288 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8289 * and the caller has to make sure there's space for this.
8290 */
8291
Hanno Becker79594fd2019-05-08 09:38:41 +01008292static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008293{
Hanno Becker79594fd2019-05-08 09:38:41 +01008294 /* This function sets the pointers to match the case
8295 * of unprotected TLS/DTLS records, with both ssl->in_iv
8296 * and ssl->in_msg pointing to the beginning of the record
8297 * content.
8298 *
8299 * When decrypting a protected record, ssl->in_msg
8300 * will be shifted to point to the beginning of the
8301 * record plaintext.
8302 */
8303
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008304#if defined(MBEDTLS_SSL_PROTO_DTLS)
8305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8306 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008307 /* This sets the header pointers to match records
8308 * without CID. When we receive a record containing
8309 * a CID, the fields are shifted accordingly in
8310 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008311 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008312#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008313 ssl->in_cid = ssl->in_ctr + 8;
8314 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008315#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008316 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008317#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008318 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008319 }
8320 else
8321#endif
8322 {
8323 ssl->in_ctr = ssl->in_hdr - 8;
8324 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008325#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008326 ssl->in_cid = ssl->in_len;
8327#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008328 ssl->in_iv = ssl->in_hdr + 5;
8329 }
8330
Hanno Becker79594fd2019-05-08 09:38:41 +01008331 /* This will be adjusted at record decryption time. */
8332 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008333}
8334
Paul Bakker5121ce52009-01-03 21:22:43 +00008335/*
8336 * Initialize an SSL context
8337 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008338void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8339{
8340 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8341}
8342
8343/*
8344 * Setup an SSL context
8345 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008346
8347static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8348{
8349 /* Set the incoming and outgoing record pointers. */
8350#if defined(MBEDTLS_SSL_PROTO_DTLS)
8351 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8352 {
8353 ssl->out_hdr = ssl->out_buf;
8354 ssl->in_hdr = ssl->in_buf;
8355 }
8356 else
8357#endif /* MBEDTLS_SSL_PROTO_DTLS */
8358 {
8359 ssl->out_hdr = ssl->out_buf + 8;
8360 ssl->in_hdr = ssl->in_buf + 8;
8361 }
8362
8363 /* Derive other internal pointers. */
8364 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008365 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008366}
8367
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008368int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008369 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008370{
Paul Bakker48916f92012-09-16 19:57:18 +00008371 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008372
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008373 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008374
8375 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008376 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008377 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008378
8379 /* Set to NULL in case of an error condition */
8380 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008381
Angus Grattond8213d02016-05-25 20:56:48 +10008382 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8383 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008384 {
Angus Grattond8213d02016-05-25 20:56:48 +10008385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008386 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008387 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008388 }
8389
8390 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8391 if( ssl->out_buf == NULL )
8392 {
8393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008394 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008395 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008396 }
8397
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008398 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008399
Paul Bakker48916f92012-09-16 19:57:18 +00008400 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008401 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008402
8403 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008404
8405error:
8406 mbedtls_free( ssl->in_buf );
8407 mbedtls_free( ssl->out_buf );
8408
8409 ssl->conf = NULL;
8410
8411 ssl->in_buf = NULL;
8412 ssl->out_buf = NULL;
8413
8414 ssl->in_hdr = NULL;
8415 ssl->in_ctr = NULL;
8416 ssl->in_len = NULL;
8417 ssl->in_iv = NULL;
8418 ssl->in_msg = NULL;
8419
8420 ssl->out_hdr = NULL;
8421 ssl->out_ctr = NULL;
8422 ssl->out_len = NULL;
8423 ssl->out_iv = NULL;
8424 ssl->out_msg = NULL;
8425
k-stachowiak9f7798e2018-07-31 16:52:32 +02008426 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008427}
8428
8429/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008430 * Reset an initialized and used SSL context for re-use while retaining
8431 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008432 *
8433 * If partial is non-zero, keep data in the input buffer and client ID.
8434 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008435 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008436static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008437{
Paul Bakker48916f92012-09-16 19:57:18 +00008438 int ret;
8439
Hanno Becker7e772132018-08-10 12:38:21 +01008440#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8441 !defined(MBEDTLS_SSL_SRV_C)
8442 ((void) partial);
8443#endif
8444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008445 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008446
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008447 /* Cancel any possibly running timer */
8448 ssl_set_timer( ssl, 0 );
8449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008450#if defined(MBEDTLS_SSL_RENEGOTIATION)
8451 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008452 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008453
8454 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8456 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008457#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008459
Paul Bakker7eb013f2011-10-06 12:37:39 +00008460 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008461 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008462
8463 ssl->in_msgtype = 0;
8464 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008466 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008467 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008468#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008469#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008470 ssl_dtls_replay_reset( ssl );
8471#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008472
8473 ssl->in_hslen = 0;
8474 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008475
8476 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008477
8478 ssl->out_msgtype = 0;
8479 ssl->out_msglen = 0;
8480 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8482 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008483 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008484#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008485
Hanno Becker19859472018-08-06 09:40:20 +01008486 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8487
Paul Bakker48916f92012-09-16 19:57:18 +00008488 ssl->transform_in = NULL;
8489 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008490
Hanno Becker78640902018-08-13 16:35:15 +01008491 ssl->session_in = NULL;
8492 ssl->session_out = NULL;
8493
Angus Grattond8213d02016-05-25 20:56:48 +10008494 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008495
8496#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008497 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008498#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8499 {
8500 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008501 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008502 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008504#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8505 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8508 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8511 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008512 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008513 }
8514#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008515
Paul Bakker48916f92012-09-16 19:57:18 +00008516 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518 mbedtls_ssl_transform_free( ssl->transform );
8519 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008520 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008521 }
Paul Bakker48916f92012-09-16 19:57:18 +00008522
Paul Bakkerc0463502013-02-14 11:19:38 +01008523 if( ssl->session )
8524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525 mbedtls_ssl_session_free( ssl->session );
8526 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008527 ssl->session = NULL;
8528 }
8529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008531 ssl->alpn_chosen = NULL;
8532#endif
8533
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008534#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008535#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008536 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008537#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008538 {
8539 mbedtls_free( ssl->cli_id );
8540 ssl->cli_id = NULL;
8541 ssl->cli_id_len = 0;
8542 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008543#endif
8544
Paul Bakker48916f92012-09-16 19:57:18 +00008545 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8546 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008547
8548 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008549}
8550
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008551/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008552 * Reset an initialized and used SSL context for re-use while retaining
8553 * all application-set variables, function pointers and data.
8554 */
8555int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8556{
8557 return( ssl_session_reset_int( ssl, 0 ) );
8558}
8559
8560/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008561 * SSL set accessors
8562 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008563void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008564{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008565 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008566}
8567
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008568void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008569{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008570 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008571}
8572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008573#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008574void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008575{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008576 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008577}
8578#endif
8579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008581void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008582{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008583 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008584}
8585#endif
8586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008588
Hanno Becker1841b0a2018-08-24 11:13:57 +01008589void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8590 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008591{
8592 ssl->disable_datagram_packing = !allow_packing;
8593}
8594
8595void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8596 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008597{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008598 conf->hs_timeout_min = min;
8599 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008600}
8601#endif
8602
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008603void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008604{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008605 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008606}
8607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008609void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008610 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008611 void *p_vrfy )
8612{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008613 conf->f_vrfy = f_vrfy;
8614 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008617
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008618void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008619 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008620 void *p_rng )
8621{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008622 conf->f_rng = f_rng;
8623 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008624}
8625
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008626void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008627 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008628 void *p_dbg )
8629{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008630 conf->f_dbg = f_dbg;
8631 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008632}
8633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008635 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008636 mbedtls_ssl_send_t *f_send,
8637 mbedtls_ssl_recv_t *f_recv,
8638 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008639{
8640 ssl->p_bio = p_bio;
8641 ssl->f_send = f_send;
8642 ssl->f_recv = f_recv;
8643 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008644}
8645
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008646#if defined(MBEDTLS_SSL_PROTO_DTLS)
8647void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8648{
8649 ssl->mtu = mtu;
8650}
8651#endif
8652
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008653void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008654{
8655 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008656}
8657
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008658void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8659 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008660 mbedtls_ssl_set_timer_t *f_set_timer,
8661 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008662{
8663 ssl->p_timer = p_timer;
8664 ssl->f_set_timer = f_set_timer;
8665 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008666
8667 /* Make sure we start with no timer running */
8668 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008669}
8670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008672void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008673 void *p_cache,
8674 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8675 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008676{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008677 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008678 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008679 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008680}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008683#if defined(MBEDTLS_SSL_CLI_C)
8684int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008685{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008686 int ret;
8687
8688 if( ssl == NULL ||
8689 session == NULL ||
8690 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008691 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008693 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008694 }
8695
Hanno Becker52055ae2019-02-06 14:30:46 +00008696 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8697 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008698 return( ret );
8699
Paul Bakker0a597072012-09-25 21:55:46 +00008700 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008701
8702 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008705
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008706void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008707 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008708{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008709 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8710 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8711 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8712 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008713}
8714
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008715void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008716 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008717 int major, int minor )
8718{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008719 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008720 return;
8721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008722 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008723 return;
8724
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008725 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008726}
8727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008728#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008729void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008730 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008731{
8732 conf->cert_profile = profile;
8733}
8734
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008735/* Append a new keycert entry to a (possibly empty) list */
8736static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8737 mbedtls_x509_crt *cert,
8738 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008739{
niisato8ee24222018-06-25 19:05:48 +09008740 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008741
niisato8ee24222018-06-25 19:05:48 +09008742 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8743 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008744 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008745
niisato8ee24222018-06-25 19:05:48 +09008746 new_cert->cert = cert;
8747 new_cert->key = key;
8748 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008749
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008750 /* Update head is the list was null, else add to the end */
8751 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008752 {
niisato8ee24222018-06-25 19:05:48 +09008753 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008754 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008755 else
8756 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008757 mbedtls_ssl_key_cert *cur = *head;
8758 while( cur->next != NULL )
8759 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008760 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008761 }
8762
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008763 return( 0 );
8764}
8765
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008766int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008767 mbedtls_x509_crt *own_cert,
8768 mbedtls_pk_context *pk_key )
8769{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008770 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008771}
8772
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008773void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008774 mbedtls_x509_crt *ca_chain,
8775 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008776{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008777 conf->ca_chain = ca_chain;
8778 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008779
8780#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8781 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8782 * cannot be used together. */
8783 conf->f_ca_cb = NULL;
8784 conf->p_ca_cb = NULL;
8785#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008786}
Hanno Becker5adaad92019-03-27 16:54:37 +00008787
8788#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8789void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008790 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008791 void *p_ca_cb )
8792{
8793 conf->f_ca_cb = f_ca_cb;
8794 conf->p_ca_cb = p_ca_cb;
8795
8796 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8797 * cannot be used together. */
8798 conf->ca_chain = NULL;
8799 conf->ca_crl = NULL;
8800}
8801#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008803
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008804#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8805int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8806 mbedtls_x509_crt *own_cert,
8807 mbedtls_pk_context *pk_key )
8808{
8809 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8810 own_cert, pk_key ) );
8811}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008812
8813void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8814 mbedtls_x509_crt *ca_chain,
8815 mbedtls_x509_crl *ca_crl )
8816{
8817 ssl->handshake->sni_ca_chain = ca_chain;
8818 ssl->handshake->sni_ca_crl = ca_crl;
8819}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008820
8821void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8822 int authmode )
8823{
8824 ssl->handshake->sni_authmode = authmode;
8825}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008826#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8827
Hanno Becker8927c832019-04-03 12:52:50 +01008828#if defined(MBEDTLS_X509_CRT_PARSE_C)
8829void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8830 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8831 void *p_vrfy )
8832{
8833 ssl->f_vrfy = f_vrfy;
8834 ssl->p_vrfy = p_vrfy;
8835}
8836#endif
8837
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008838#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008839/*
8840 * Set EC J-PAKE password for current handshake
8841 */
8842int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8843 const unsigned char *pw,
8844 size_t pw_len )
8845{
8846 mbedtls_ecjpake_role role;
8847
Janos Follath8eb64132016-06-03 15:40:57 +01008848 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008849 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8850
8851 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8852 role = MBEDTLS_ECJPAKE_SERVER;
8853 else
8854 role = MBEDTLS_ECJPAKE_CLIENT;
8855
8856 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8857 role,
8858 MBEDTLS_MD_SHA256,
8859 MBEDTLS_ECP_DP_SECP256R1,
8860 pw, pw_len ) );
8861}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008862#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008864#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008865
8866static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8867{
8868 /* Remove reference to existing PSK, if any. */
8869#if defined(MBEDTLS_USE_PSA_CRYPTO)
8870 if( conf->psk_opaque != 0 )
8871 {
8872 /* The maintenance of the PSK key slot is the
8873 * user's responsibility. */
8874 conf->psk_opaque = 0;
8875 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008876 /* This and the following branch should never
8877 * be taken simultaenously as we maintain the
8878 * invariant that raw and opaque PSKs are never
8879 * configured simultaneously. As a safeguard,
8880 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008881#endif /* MBEDTLS_USE_PSA_CRYPTO */
8882 if( conf->psk != NULL )
8883 {
8884 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8885
8886 mbedtls_free( conf->psk );
8887 conf->psk = NULL;
8888 conf->psk_len = 0;
8889 }
8890
8891 /* Remove reference to PSK identity, if any. */
8892 if( conf->psk_identity != NULL )
8893 {
8894 mbedtls_free( conf->psk_identity );
8895 conf->psk_identity = NULL;
8896 conf->psk_identity_len = 0;
8897 }
8898}
8899
Hanno Becker7390c712018-11-15 13:33:04 +00008900/* This function assumes that PSK identity in the SSL config is unset.
8901 * It checks that the provided identity is well-formed and attempts
8902 * to make a copy of it in the SSL config.
8903 * On failure, the PSK identity in the config remains unset. */
8904static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8905 unsigned char const *psk_identity,
8906 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008907{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008908 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008909 if( psk_identity == NULL ||
8910 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008911 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008912 {
8913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8914 }
8915
Hanno Becker7390c712018-11-15 13:33:04 +00008916 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8917 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008918 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008919
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008920 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008921 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008922
8923 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008924}
8925
Hanno Becker7390c712018-11-15 13:33:04 +00008926int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8927 const unsigned char *psk, size_t psk_len,
8928 const unsigned char *psk_identity, size_t psk_identity_len )
8929{
8930 int ret;
8931 /* Remove opaque/raw PSK + PSK Identity */
8932 ssl_conf_remove_psk( conf );
8933
8934 /* Check and set raw PSK */
8935 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8936 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8937 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8938 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8939 conf->psk_len = psk_len;
8940 memcpy( conf->psk, psk, conf->psk_len );
8941
8942 /* Check and set PSK Identity */
8943 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8944 if( ret != 0 )
8945 ssl_conf_remove_psk( conf );
8946
8947 return( ret );
8948}
8949
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008950static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8951{
8952#if defined(MBEDTLS_USE_PSA_CRYPTO)
8953 if( ssl->handshake->psk_opaque != 0 )
8954 {
8955 ssl->handshake->psk_opaque = 0;
8956 }
8957 else
8958#endif /* MBEDTLS_USE_PSA_CRYPTO */
8959 if( ssl->handshake->psk != NULL )
8960 {
8961 mbedtls_platform_zeroize( ssl->handshake->psk,
8962 ssl->handshake->psk_len );
8963 mbedtls_free( ssl->handshake->psk );
8964 ssl->handshake->psk_len = 0;
8965 }
8966}
8967
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008968int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8969 const unsigned char *psk, size_t psk_len )
8970{
8971 if( psk == NULL || ssl->handshake == NULL )
8972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8973
8974 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8976
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008977 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008978
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008979 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008980 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008981
8982 ssl->handshake->psk_len = psk_len;
8983 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8984
8985 return( 0 );
8986}
8987
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008988#if defined(MBEDTLS_USE_PSA_CRYPTO)
8989int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008990 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008991 const unsigned char *psk_identity,
8992 size_t psk_identity_len )
8993{
Hanno Becker7390c712018-11-15 13:33:04 +00008994 int ret;
8995 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008996 ssl_conf_remove_psk( conf );
8997
Hanno Becker7390c712018-11-15 13:33:04 +00008998 /* Check and set opaque PSK */
8999 if( psk_slot == 0 )
9000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009001 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009002
9003 /* Check and set PSK Identity */
9004 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9005 psk_identity_len );
9006 if( ret != 0 )
9007 ssl_conf_remove_psk( conf );
9008
9009 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009010}
9011
9012int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009013 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009014{
9015 if( psk_slot == 0 || ssl->handshake == NULL )
9016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9017
9018 ssl_remove_psk( ssl );
9019 ssl->handshake->psk_opaque = psk_slot;
9020 return( 0 );
9021}
9022#endif /* MBEDTLS_USE_PSA_CRYPTO */
9023
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009024void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009026 size_t),
9027 void *p_psk )
9028{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009029 conf->f_psk = f_psk;
9030 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009031}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009032#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009033
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009034#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009035
9036#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009037int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009038{
9039 int ret;
9040
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009041 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9042 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9043 {
9044 mbedtls_mpi_free( &conf->dhm_P );
9045 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009046 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009047 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009048
9049 return( 0 );
9050}
Hanno Becker470a8c42017-10-04 15:28:46 +01009051#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009052
Hanno Beckera90658f2017-10-04 15:29:08 +01009053int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9054 const unsigned char *dhm_P, size_t P_len,
9055 const unsigned char *dhm_G, size_t G_len )
9056{
9057 int ret;
9058
9059 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9060 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9061 {
9062 mbedtls_mpi_free( &conf->dhm_P );
9063 mbedtls_mpi_free( &conf->dhm_G );
9064 return( ret );
9065 }
9066
9067 return( 0 );
9068}
Paul Bakker5121ce52009-01-03 21:22:43 +00009069
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009070int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009071{
9072 int ret;
9073
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009074 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9075 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9076 {
9077 mbedtls_mpi_free( &conf->dhm_P );
9078 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009079 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009080 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009081
9082 return( 0 );
9083}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009084#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009085
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009086#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9087/*
9088 * Set the minimum length for Diffie-Hellman parameters
9089 */
9090void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9091 unsigned int bitlen )
9092{
9093 conf->dhm_min_bitlen = bitlen;
9094}
9095#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9096
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009097#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009098/*
9099 * Set allowed/preferred hashes for handshake signatures
9100 */
9101void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9102 const int *hashes )
9103{
9104 conf->sig_hashes = hashes;
9105}
Hanno Becker947194e2017-04-07 13:25:49 +01009106#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009107
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009108#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009109/*
9110 * Set the allowed elliptic curves
9111 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009112void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009113 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009114{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009115 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009116}
Hanno Becker947194e2017-04-07 13:25:49 +01009117#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009118
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009119#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009121{
Hanno Becker947194e2017-04-07 13:25:49 +01009122 /* Initialize to suppress unnecessary compiler warning */
9123 size_t hostname_len = 0;
9124
9125 /* Check if new hostname is valid before
9126 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009127 if( hostname != NULL )
9128 {
9129 hostname_len = strlen( hostname );
9130
9131 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9133 }
9134
9135 /* Now it's clear that we will overwrite the old hostname,
9136 * so we can free it safely */
9137
9138 if( ssl->hostname != NULL )
9139 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009140 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009141 mbedtls_free( ssl->hostname );
9142 }
9143
9144 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009145
Paul Bakker5121ce52009-01-03 21:22:43 +00009146 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009147 {
9148 ssl->hostname = NULL;
9149 }
9150 else
9151 {
9152 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009153 if( ssl->hostname == NULL )
9154 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009155
Hanno Becker947194e2017-04-07 13:25:49 +01009156 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009157
Hanno Becker947194e2017-04-07 13:25:49 +01009158 ssl->hostname[hostname_len] = '\0';
9159 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009160
9161 return( 0 );
9162}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009163#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009164
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009165#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009166void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009168 const unsigned char *, size_t),
9169 void *p_sni )
9170{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009171 conf->f_sni = f_sni;
9172 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009174#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009177int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009178{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009179 size_t cur_len, tot_len;
9180 const char **p;
9181
9182 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009183 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9184 * MUST NOT be truncated."
9185 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009186 */
9187 tot_len = 0;
9188 for( p = protos; *p != NULL; p++ )
9189 {
9190 cur_len = strlen( *p );
9191 tot_len += cur_len;
9192
9193 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009195 }
9196
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009197 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009198
9199 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009200}
9201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009202const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009203{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009204 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009207
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009208void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009209{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009210 conf->max_major_ver = major;
9211 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009212}
9213
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009214void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009215{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009216 conf->min_major_ver = major;
9217 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009218}
9219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009220#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009221void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009222{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009223 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009224}
9225#endif
9226
Janos Follath088ce432017-04-10 12:42:31 +01009227#if defined(MBEDTLS_SSL_SRV_C)
9228void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9229 char cert_req_ca_list )
9230{
9231 conf->cert_req_ca_list = cert_req_ca_list;
9232}
9233#endif
9234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009235#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009236void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009237{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009238 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009239}
9240#endif
9241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009242#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009243void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009244{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009245 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009246}
9247#endif
9248
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009249#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009250void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009251{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009252 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009253}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009254#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009256#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009257int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009258{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009259 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009260 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009262 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009263 }
9264
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009265 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009266
9267 return( 0 );
9268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009269#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009271#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009272void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009273{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009274 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009275}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009276#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009279void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009280{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009281 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009282}
9283#endif
9284
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009285void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009286{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009287 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009288}
9289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009291void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009292{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009293 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009294}
9295
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009296void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009297{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009298 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009299}
9300
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009301void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009302 const unsigned char period[8] )
9303{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009304 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009305}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009309#if defined(MBEDTLS_SSL_CLI_C)
9310void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009311{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009312 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009313}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009314#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009315
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009316#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009317void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9318 mbedtls_ssl_ticket_write_t *f_ticket_write,
9319 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9320 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009321{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009322 conf->f_ticket_write = f_ticket_write;
9323 conf->f_ticket_parse = f_ticket_parse;
9324 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009325}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009326#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009328
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009329#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9330void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9331 mbedtls_ssl_export_keys_t *f_export_keys,
9332 void *p_export_keys )
9333{
9334 conf->f_export_keys = f_export_keys;
9335 conf->p_export_keys = p_export_keys;
9336}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009337
9338void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9339 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9340 void *p_export_keys )
9341{
9342 conf->f_export_keys_ext = f_export_keys_ext;
9343 conf->p_export_keys = p_export_keys;
9344}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009345#endif
9346
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009347#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009348void mbedtls_ssl_conf_async_private_cb(
9349 mbedtls_ssl_config *conf,
9350 mbedtls_ssl_async_sign_t *f_async_sign,
9351 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9352 mbedtls_ssl_async_resume_t *f_async_resume,
9353 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009354 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009355{
9356 conf->f_async_sign_start = f_async_sign;
9357 conf->f_async_decrypt_start = f_async_decrypt;
9358 conf->f_async_resume = f_async_resume;
9359 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009360 conf->p_async_config_data = async_config_data;
9361}
9362
Gilles Peskine8f97af72018-04-26 11:46:10 +02009363void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9364{
9365 return( conf->p_async_config_data );
9366}
9367
Gilles Peskine1febfef2018-04-30 11:54:39 +02009368void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009369{
9370 if( ssl->handshake == NULL )
9371 return( NULL );
9372 else
9373 return( ssl->handshake->user_async_ctx );
9374}
9375
Gilles Peskine1febfef2018-04-30 11:54:39 +02009376void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009377 void *ctx )
9378{
9379 if( ssl->handshake != NULL )
9380 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009381}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009382#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009383
Paul Bakker5121ce52009-01-03 21:22:43 +00009384/*
9385 * SSL get accessors
9386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009388{
9389 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9390}
9391
Hanno Becker8b170a02017-10-10 11:51:19 +01009392int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9393{
9394 /*
9395 * Case A: We're currently holding back
9396 * a message for further processing.
9397 */
9398
9399 if( ssl->keep_current_message == 1 )
9400 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009402 return( 1 );
9403 }
9404
9405 /*
9406 * Case B: Further records are pending in the current datagram.
9407 */
9408
9409#if defined(MBEDTLS_SSL_PROTO_DTLS)
9410 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9411 ssl->in_left > ssl->next_record_offset )
9412 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009413 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009414 return( 1 );
9415 }
9416#endif /* MBEDTLS_SSL_PROTO_DTLS */
9417
9418 /*
9419 * Case C: A handshake message is being processed.
9420 */
9421
Hanno Becker8b170a02017-10-10 11:51:19 +01009422 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9423 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009424 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009425 return( 1 );
9426 }
9427
9428 /*
9429 * Case D: An application data message is being processed
9430 */
9431 if( ssl->in_offt != NULL )
9432 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009433 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009434 return( 1 );
9435 }
9436
9437 /*
9438 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009439 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009440 * we implement support for multiple alerts in single records.
9441 */
9442
9443 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9444 return( 0 );
9445}
9446
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009447uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009448{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009449 if( ssl->session != NULL )
9450 return( ssl->session->verify_result );
9451
9452 if( ssl->session_negotiate != NULL )
9453 return( ssl->session_negotiate->verify_result );
9454
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009455 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009456}
9457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009459{
Paul Bakker926c8e42013-03-06 10:23:34 +01009460 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009461 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009464}
9465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009467{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009469 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009470 {
9471 switch( ssl->minor_ver )
9472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009474 return( "DTLSv1.0" );
9475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009476 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009477 return( "DTLSv1.2" );
9478
9479 default:
9480 return( "unknown (DTLS)" );
9481 }
9482 }
9483#endif
9484
Paul Bakker43ca69c2011-01-15 17:35:19 +00009485 switch( ssl->minor_ver )
9486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009488 return( "SSLv3.0" );
9489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009491 return( "TLSv1.0" );
9492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009494 return( "TLSv1.1" );
9495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009497 return( "TLSv1.2" );
9498
Paul Bakker43ca69c2011-01-15 17:35:19 +00009499 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009500 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009501 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009502}
9503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009505{
Hanno Becker3136ede2018-08-17 15:28:19 +01009506 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009508 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009509
Hanno Becker5903de42019-05-03 14:46:38 +01009510 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9511
Hanno Becker78640902018-08-13 16:35:15 +01009512 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009513 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515#if defined(MBEDTLS_ZLIB_SUPPORT)
9516 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9517 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009518#endif
9519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 case MBEDTLS_MODE_GCM:
9523 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009524 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009526 transform_expansion = transform->minlen;
9527 break;
9528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009530
9531 block_size = mbedtls_cipher_get_block_size(
9532 &transform->cipher_ctx_enc );
9533
Hanno Becker3136ede2018-08-17 15:28:19 +01009534 /* Expansion due to the addition of the MAC. */
9535 transform_expansion += transform->maclen;
9536
9537 /* Expansion due to the addition of CBC padding;
9538 * Theoretically up to 256 bytes, but we never use
9539 * more than the block size of the underlying cipher. */
9540 transform_expansion += block_size;
9541
9542 /* For TLS 1.1 or higher, an explicit IV is added
9543 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009544#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9545 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009546 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009547#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009548
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009549 break;
9550
9551 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009554 }
9555
Hanno Beckera0e20d02019-05-15 14:03:01 +01009556#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009557 if( transform->out_cid_len != 0 )
9558 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009559#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009560
Hanno Becker5903de42019-05-03 14:46:38 +01009561 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009562}
9563
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009564#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9565size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9566{
9567 size_t max_len;
9568
9569 /*
9570 * Assume mfl_code is correct since it was checked when set
9571 */
Angus Grattond8213d02016-05-25 20:56:48 +10009572 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009573
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009574 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009575 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009576 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009577 {
Angus Grattond8213d02016-05-25 20:56:48 +10009578 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009579 }
9580
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009581 /* During a handshake, use the value being negotiated */
9582 if( ssl->session_negotiate != NULL &&
9583 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9584 {
9585 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9586 }
9587
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009588 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009589}
9590#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9591
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009592#if defined(MBEDTLS_SSL_PROTO_DTLS)
9593static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9594{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009595 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9596 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9597 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9598 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9599 return ( 0 );
9600
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009601 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9602 return( ssl->mtu );
9603
9604 if( ssl->mtu == 0 )
9605 return( ssl->handshake->mtu );
9606
9607 return( ssl->mtu < ssl->handshake->mtu ?
9608 ssl->mtu : ssl->handshake->mtu );
9609}
9610#endif /* MBEDTLS_SSL_PROTO_DTLS */
9611
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009612int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9613{
9614 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9615
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009616#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9617 !defined(MBEDTLS_SSL_PROTO_DTLS)
9618 (void) ssl;
9619#endif
9620
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009621#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9622 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9623
9624 if( max_len > mfl )
9625 max_len = mfl;
9626#endif
9627
9628#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009629 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009630 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009631 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009632 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9633 const size_t overhead = (size_t) ret;
9634
9635 if( ret < 0 )
9636 return( ret );
9637
9638 if( mtu <= overhead )
9639 {
9640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9641 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9642 }
9643
9644 if( max_len > mtu - overhead )
9645 max_len = mtu - overhead;
9646 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009647#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009648
Hanno Becker0defedb2018-08-10 12:35:02 +01009649#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9650 !defined(MBEDTLS_SSL_PROTO_DTLS)
9651 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009652#endif
9653
9654 return( (int) max_len );
9655}
9656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657#if defined(MBEDTLS_X509_CRT_PARSE_C)
9658const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009659{
9660 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009661 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009662
Hanno Beckere6824572019-02-07 13:18:46 +00009663#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009664 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009665#else
9666 return( NULL );
9667#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009668}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009672int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9673 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009674{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009675 if( ssl == NULL ||
9676 dst == NULL ||
9677 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009678 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009681 }
9682
Hanno Becker52055ae2019-02-06 14:30:46 +00009683 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009684}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009686
Paul Bakker5121ce52009-01-03 21:22:43 +00009687/*
Paul Bakker1961b702013-01-25 14:49:24 +01009688 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009691{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009692 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009693
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009694 if( ssl == NULL || ssl->conf == NULL )
9695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009697#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009698 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009702 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009704#endif
9705
Paul Bakker1961b702013-01-25 14:49:24 +01009706 return( ret );
9707}
9708
9709/*
9710 * Perform the SSL handshake
9711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009713{
9714 int ret = 0;
9715
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009716 if( ssl == NULL || ssl->conf == NULL )
9717 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009721 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009723 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009724
9725 if( ret != 0 )
9726 break;
9727 }
9728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009730
9731 return( ret );
9732}
9733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734#if defined(MBEDTLS_SSL_RENEGOTIATION)
9735#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009736/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009737 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009740{
9741 int ret;
9742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009744
9745 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9747 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009748
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009749 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009750 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009752 return( ret );
9753 }
9754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009756
9757 return( 0 );
9758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009760
9761/*
9762 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 * - any side: calling mbedtls_ssl_renegotiate(),
9764 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9765 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009766 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009767 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009771{
9772 int ret;
9773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009775
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009776 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9777 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009778
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009779 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9780 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009782 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009784 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009785 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009786 ssl->handshake->out_msg_seq = 1;
9787 else
9788 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009789 }
9790#endif
9791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9793 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009798 return( ret );
9799 }
9800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009802
9803 return( 0 );
9804}
9805
9806/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009807 * Renegotiate current connection on client,
9808 * or request renegotiation on server
9809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009811{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009813
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009814 if( ssl == NULL || ssl->conf == NULL )
9815 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009818 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009819 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009825
9826 /* Did we already try/start sending HelloRequest? */
9827 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009829
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009830 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009831 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009835 /*
9836 * On client, either start the renegotiation process or,
9837 * if already in progress, continue the handshake
9838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9842 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009843
9844 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009847 return( ret );
9848 }
9849 }
9850 else
9851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009855 return( ret );
9856 }
9857 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009859
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009860 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009861}
9862
9863/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009864 * Check record counters and renegotiate if they're above the limit.
9865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009867{
Andres AG2196c7f2016-12-15 17:01:16 +00009868 size_t ep_len = ssl_ep_len( ssl );
9869 int in_ctr_cmp;
9870 int out_ctr_cmp;
9871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9873 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009874 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009875 {
9876 return( 0 );
9877 }
9878
Andres AG2196c7f2016-12-15 17:01:16 +00009879 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9880 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009881 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009882 ssl->conf->renego_period + ep_len, 8 - ep_len );
9883
9884 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009885 {
9886 return( 0 );
9887 }
9888
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009891}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009893
9894/*
9895 * Receive application data decrypted from the SSL layer
9896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009898{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009899 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009900 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009901
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009902 if( ssl == NULL || ssl->conf == NULL )
9903 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009908 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009910 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009911 return( ret );
9912
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009913 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009914 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009915 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009916 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009917 return( ret );
9918 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009919 }
9920#endif
9921
Hanno Becker4a810fb2017-05-24 16:27:30 +01009922 /*
9923 * Check if renegotiation is necessary and/or handshake is
9924 * in process. If yes, perform/continue, and fall through
9925 * if an unexpected packet is received while the client
9926 * is waiting for the ServerHello.
9927 *
9928 * (There is no equivalent to the last condition on
9929 * the server-side as it is not treated as within
9930 * a handshake while waiting for the ClientHello
9931 * after a renegotiation request.)
9932 */
9933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009935 ret = ssl_check_ctr_renegotiate( ssl );
9936 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9937 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009940 return( ret );
9941 }
9942#endif
9943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009946 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009947 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9948 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009951 return( ret );
9952 }
9953 }
9954
Hanno Beckere41158b2017-10-23 13:30:32 +01009955 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009956 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009957 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009958 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009959 if( ssl->f_get_timer != NULL &&
9960 ssl->f_get_timer( ssl->p_timer ) == -1 )
9961 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009962 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009963 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009964
Hanno Becker327c93b2018-08-15 13:56:18 +01009965 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009966 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009967 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9968 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009969
Hanno Becker4a810fb2017-05-24 16:27:30 +01009970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9971 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009972 }
9973
9974 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009976 {
9977 /*
9978 * OpenSSL sends empty messages to randomize the IV
9979 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009980 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009983 return( 0 );
9984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009986 return( ret );
9987 }
9988 }
9989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009993
Hanno Becker4a810fb2017-05-24 16:27:30 +01009994 /*
9995 * - For client-side, expect SERVER_HELLO_REQUEST.
9996 * - For server-side, expect CLIENT_HELLO.
9997 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9998 */
9999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010001 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010002 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010003 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010006
10007 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010008#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010009 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010010 {
10011 continue;
10012 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010013#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010014 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010015 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010016#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010017
Hanno Becker4a810fb2017-05-24 16:27:30 +010010018#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010019 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010020 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010023
10024 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010027 {
10028 continue;
10029 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010031 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010032 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010033#endif /* MBEDTLS_SSL_SRV_C */
10034
Hanno Becker21df7f92017-10-17 11:03:26 +010010035#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010036 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010037 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10038 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10039 ssl->conf->allow_legacy_renegotiation ==
10040 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10041 {
10042 /*
10043 * Accept renegotiation request
10044 */
Paul Bakker48916f92012-09-16 19:57:18 +000010045
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010046 /* DTLS clients need to know renego is server-initiated */
10047#if defined(MBEDTLS_SSL_PROTO_DTLS)
10048 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10049 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10050 {
10051 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10052 }
10053#endif
10054 ret = ssl_start_renegotiation( ssl );
10055 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10056 ret != 0 )
10057 {
10058 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10059 return( ret );
10060 }
10061 }
10062 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010063#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010064 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010065 /*
10066 * Refuse renegotiation
10067 */
10068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071#if defined(MBEDTLS_SSL_PROTO_SSL3)
10072 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010073 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010074 /* SSLv3 does not have a "no_renegotiation" warning, so
10075 we send a fatal alert and abort the connection. */
10076 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10077 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10078 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010079 }
10080 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10082#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10083 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10084 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10087 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10088 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010089 {
10090 return( ret );
10091 }
Paul Bakker48916f92012-09-16 19:57:18 +000010092 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010093 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10095 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10098 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010099 }
Paul Bakker48916f92012-09-16 19:57:18 +000010100 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010101
Hanno Becker90333da2017-10-10 11:27:13 +010010102 /* At this point, we don't know whether the renegotiation has been
10103 * completed or not. The cases to consider are the following:
10104 * 1) The renegotiation is complete. In this case, no new record
10105 * has been read yet.
10106 * 2) The renegotiation is incomplete because the client received
10107 * an application data record while awaiting the ServerHello.
10108 * 3) The renegotiation is incomplete because the client received
10109 * a non-handshake, non-application data message while awaiting
10110 * the ServerHello.
10111 * In each of these case, looping will be the proper action:
10112 * - For 1), the next iteration will read a new record and check
10113 * if it's application data.
10114 * - For 2), the loop condition isn't satisfied as application data
10115 * is present, hence continue is the same as break
10116 * - For 3), the loop condition is satisfied and read_record
10117 * will re-deliver the message that was held back by the client
10118 * when expecting the ServerHello.
10119 */
10120 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010121 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010122#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010123 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010124 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010125 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010126 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010127 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010130 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010131 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010132 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010133 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010134 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010135#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010137 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10138 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010141 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010142 }
10143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10147 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010148 }
10149
10150 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010151
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010152 /* We're going to return something now, cancel timer,
10153 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010154 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010155 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010156
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010158 /* If we requested renego but received AppData, resend HelloRequest.
10159 * Do it now, after setting in_offt, to avoid taking this branch
10160 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010161#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010162 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010163 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010164 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010165 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010167 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010168 return( ret );
10169 }
10170 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010172#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010173 }
10174
10175 n = ( len < ssl->in_msglen )
10176 ? len : ssl->in_msglen;
10177
10178 memcpy( buf, ssl->in_offt, n );
10179 ssl->in_msglen -= n;
10180
10181 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010182 {
10183 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010184 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010185 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010186 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010187 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010188 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010189 /* more data available */
10190 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010191 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010194
Paul Bakker23986e52011-04-24 08:57:21 +000010195 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010196}
10197
10198/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010199 * Send application data to be encrypted by the SSL layer, taking care of max
10200 * fragment length and buffer size.
10201 *
10202 * According to RFC 5246 Section 6.2.1:
10203 *
10204 * Zero-length fragments of Application data MAY be sent as they are
10205 * potentially useful as a traffic analysis countermeasure.
10206 *
10207 * Therefore, it is possible that the input message length is 0 and the
10208 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010209 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010210static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010211 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010212{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010213 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10214 const size_t max_len = (size_t) ret;
10215
10216 if( ret < 0 )
10217 {
10218 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10219 return( ret );
10220 }
10221
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010222 if( len > max_len )
10223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010225 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010228 "maximum fragment length: %d > %d",
10229 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010231 }
10232 else
10233#endif
10234 len = max_len;
10235 }
Paul Bakker887bd502011-06-08 13:10:54 +000010236
Paul Bakker5121ce52009-01-03 21:22:43 +000010237 if( ssl->out_left != 0 )
10238 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010239 /*
10240 * The user has previously tried to send the data and
10241 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10242 * written. In this case, we expect the high-level write function
10243 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010248 return( ret );
10249 }
10250 }
Paul Bakker887bd502011-06-08 13:10:54 +000010251 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010252 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010253 /*
10254 * The user is trying to send a message the first time, so we need to
10255 * copy the data into the internal buffers and setup the data structure
10256 * to keep track of partial writes
10257 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010258 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010259 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010260 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010261
Hanno Becker67bc7c32018-08-06 11:33:50 +010010262 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010265 return( ret );
10266 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010267 }
10268
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010269 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010270}
10271
10272/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010273 * Write application data, doing 1/n-1 splitting if necessary.
10274 *
10275 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010276 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010277 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010279#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010280static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010281 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010282{
10283 int ret;
10284
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010285 if( ssl->conf->cbc_record_splitting ==
10286 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010287 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010288 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10289 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10290 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010291 {
10292 return( ssl_write_real( ssl, buf, len ) );
10293 }
10294
10295 if( ssl->split_done == 0 )
10296 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010297 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010298 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010299 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010300 }
10301
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010302 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10303 return( ret );
10304 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010305
10306 return( ret + 1 );
10307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010308#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010309
10310/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010311 * Write application data (public-facing wrapper)
10312 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010313int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010314{
10315 int ret;
10316
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010318
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010319 if( ssl == NULL || ssl->conf == NULL )
10320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10321
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010322#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010323 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10324 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010325 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010326 return( ret );
10327 }
10328#endif
10329
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010330 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010331 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010332 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010333 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010335 return( ret );
10336 }
10337 }
10338
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010339#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010340 ret = ssl_write_split( ssl, buf, len );
10341#else
10342 ret = ssl_write_real( ssl, buf, len );
10343#endif
10344
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010346
10347 return( ret );
10348}
10349
10350/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010351 * Notify the peer that the connection is being closed
10352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010353int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010354{
10355 int ret;
10356
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010357 if( ssl == NULL || ssl->conf == NULL )
10358 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010361
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010362 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10368 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10369 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 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_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010372 return( ret );
10373 }
10374 }
10375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010377
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010378 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010379}
10380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010382{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010383 if( transform == NULL )
10384 return;
10385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010386#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010387 deflateEnd( &transform->ctx_deflate );
10388 inflateEnd( &transform->ctx_inflate );
10389#endif
10390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010391 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10392 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010393
Hanno Beckerd56ed242018-01-03 15:32:51 +000010394#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010395 mbedtls_md_free( &transform->md_ctx_enc );
10396 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010397#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010398
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010399 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010400}
10401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010402#if defined(MBEDTLS_X509_CRT_PARSE_C)
10403static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010404{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010405 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010406
10407 while( cur != NULL )
10408 {
10409 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010410 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010411 cur = next;
10412 }
10413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010414#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010415
Hanno Becker0271f962018-08-16 13:23:47 +010010416#if defined(MBEDTLS_SSL_PROTO_DTLS)
10417
10418static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10419{
10420 unsigned offset;
10421 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10422
10423 if( hs == NULL )
10424 return;
10425
Hanno Becker283f5ef2018-08-24 09:34:47 +010010426 ssl_free_buffered_record( ssl );
10427
Hanno Becker0271f962018-08-16 13:23:47 +010010428 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010429 ssl_buffering_free_slot( ssl, offset );
10430}
10431
10432static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10433 uint8_t slot )
10434{
10435 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10436 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010437
10438 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10439 return;
10440
Hanno Beckere605b192018-08-21 15:59:07 +010010441 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010442 {
Hanno Beckere605b192018-08-21 15:59:07 +010010443 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010444 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010445 mbedtls_free( hs_buf->data );
10446 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010447 }
10448}
10449
10450#endif /* MBEDTLS_SSL_PROTO_DTLS */
10451
Gilles Peskine9b562d52018-04-25 20:32:43 +020010452void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010453{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010454 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10455
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010456 if( handshake == NULL )
10457 return;
10458
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010459#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10460 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10461 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010462 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010463 handshake->async_in_progress = 0;
10464 }
10465#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10466
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010467#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10468 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10469 mbedtls_md5_free( &handshake->fin_md5 );
10470 mbedtls_sha1_free( &handshake->fin_sha1 );
10471#endif
10472#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10473#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010474#if defined(MBEDTLS_USE_PSA_CRYPTO)
10475 psa_hash_abort( &handshake->fin_sha256_psa );
10476#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010477 mbedtls_sha256_free( &handshake->fin_sha256 );
10478#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010479#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010480#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010481#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010482 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010483#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010484 mbedtls_sha512_free( &handshake->fin_sha512 );
10485#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010486#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010487#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010489#if defined(MBEDTLS_DHM_C)
10490 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010491#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492#if defined(MBEDTLS_ECDH_C)
10493 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010494#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010495#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010496 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010497#if defined(MBEDTLS_SSL_CLI_C)
10498 mbedtls_free( handshake->ecjpake_cache );
10499 handshake->ecjpake_cache = NULL;
10500 handshake->ecjpake_cache_len = 0;
10501#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010502#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010503
Janos Follath4ae5c292016-02-10 11:27:43 +000010504#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10505 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010506 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010507 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010508#endif
10509
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010510#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10511 if( handshake->psk != NULL )
10512 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010513 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010514 mbedtls_free( handshake->psk );
10515 }
10516#endif
10517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010518#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10519 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010520 /*
10521 * Free only the linked list wrapper, not the keys themselves
10522 * since the belong to the SNI callback
10523 */
10524 if( handshake->sni_key_cert != NULL )
10525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010526 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010527
10528 while( cur != NULL )
10529 {
10530 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010531 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010532 cur = next;
10533 }
10534 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010535#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010536
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010537#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010538 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010539 if( handshake->ecrs_peer_cert != NULL )
10540 {
10541 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10542 mbedtls_free( handshake->ecrs_peer_cert );
10543 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010544#endif
10545
Hanno Becker75173122019-02-06 16:18:31 +000010546#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10547 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10548 mbedtls_pk_free( &handshake->peer_pubkey );
10549#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010551#if defined(MBEDTLS_SSL_PROTO_DTLS)
10552 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010553 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010554 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010555#endif
10556
Hanno Becker4a63ed42019-01-08 11:39:35 +000010557#if defined(MBEDTLS_ECDH_C) && \
10558 defined(MBEDTLS_USE_PSA_CRYPTO)
10559 psa_destroy_key( handshake->ecdh_psa_privkey );
10560#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10561
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010562 mbedtls_platform_zeroize( handshake,
10563 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010564}
10565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010566void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010567{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010568 if( session == NULL )
10569 return;
10570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010571#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010572 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010573#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010574
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010575#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010576 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010577#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010578
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010579 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010580}
10581
Paul Bakker5121ce52009-01-03 21:22:43 +000010582/*
10583 * Free an SSL context
10584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010585void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010586{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010587 if( ssl == NULL )
10588 return;
10589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010591
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010592 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010593 {
Angus Grattond8213d02016-05-25 20:56:48 +100010594 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010595 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010596 }
10597
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010598 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010599 {
Angus Grattond8213d02016-05-25 20:56:48 +100010600 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010601 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010602 }
10603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010604#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010605 if( ssl->compress_buf != NULL )
10606 {
Angus Grattond8213d02016-05-25 20:56:48 +100010607 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010608 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010609 }
10610#endif
10611
Paul Bakker48916f92012-09-16 19:57:18 +000010612 if( ssl->transform )
10613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010614 mbedtls_ssl_transform_free( ssl->transform );
10615 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010616 }
10617
10618 if( ssl->handshake )
10619 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010620 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010621 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10622 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624 mbedtls_free( ssl->handshake );
10625 mbedtls_free( ssl->transform_negotiate );
10626 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010627 }
10628
Paul Bakkerc0463502013-02-14 11:19:38 +010010629 if( ssl->session )
10630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631 mbedtls_ssl_session_free( ssl->session );
10632 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010633 }
10634
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010635#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010636 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010637 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010638 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010639 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010640 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010641#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010643#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10644 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10647 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010648 }
10649#endif
10650
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010651#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010653#endif
10654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010656
Paul Bakker86f04f42013-02-14 11:20:09 +010010657 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010658 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010659}
10660
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010661/*
10662 * Initialze mbedtls_ssl_config
10663 */
10664void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10665{
10666 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10667}
10668
Simon Butcherc97b6972015-12-27 23:48:17 +000010669#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010670static int ssl_preset_default_hashes[] = {
10671#if defined(MBEDTLS_SHA512_C)
10672 MBEDTLS_MD_SHA512,
10673 MBEDTLS_MD_SHA384,
10674#endif
10675#if defined(MBEDTLS_SHA256_C)
10676 MBEDTLS_MD_SHA256,
10677 MBEDTLS_MD_SHA224,
10678#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010679#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010680 MBEDTLS_MD_SHA1,
10681#endif
10682 MBEDTLS_MD_NONE
10683};
Simon Butcherc97b6972015-12-27 23:48:17 +000010684#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010685
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010686static int ssl_preset_suiteb_ciphersuites[] = {
10687 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10688 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10689 0
10690};
10691
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010692#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010693static int ssl_preset_suiteb_hashes[] = {
10694 MBEDTLS_MD_SHA256,
10695 MBEDTLS_MD_SHA384,
10696 MBEDTLS_MD_NONE
10697};
10698#endif
10699
10700#if defined(MBEDTLS_ECP_C)
10701static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010702#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010703 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010704#endif
10705#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010706 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010707#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010708 MBEDTLS_ECP_DP_NONE
10709};
10710#endif
10711
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010712/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010713 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010714 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010715int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010716 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010717{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010718#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010719 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010720#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010721
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010722 /* Use the functions here so that they are covered in tests,
10723 * but otherwise access member directly for efficiency */
10724 mbedtls_ssl_conf_endpoint( conf, endpoint );
10725 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010726
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010727 /*
10728 * Things that are common to all presets
10729 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010730#if defined(MBEDTLS_SSL_CLI_C)
10731 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10732 {
10733 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10734#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10735 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10736#endif
10737 }
10738#endif
10739
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010740#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010741 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010742#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010743
10744#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10745 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10746#endif
10747
10748#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10749 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10750#endif
10751
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010752#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10753 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10754#endif
10755
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010756#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010757 conf->f_cookie_write = ssl_cookie_write_dummy;
10758 conf->f_cookie_check = ssl_cookie_check_dummy;
10759#endif
10760
10761#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10762 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10763#endif
10764
Janos Follath088ce432017-04-10 12:42:31 +010010765#if defined(MBEDTLS_SSL_SRV_C)
10766 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10767#endif
10768
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010769#if defined(MBEDTLS_SSL_PROTO_DTLS)
10770 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10771 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10772#endif
10773
10774#if defined(MBEDTLS_SSL_RENEGOTIATION)
10775 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010776 memset( conf->renego_period, 0x00, 2 );
10777 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010778#endif
10779
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010780#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10781 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10782 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010783 const unsigned char dhm_p[] =
10784 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10785 const unsigned char dhm_g[] =
10786 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10787
Hanno Beckera90658f2017-10-04 15:29:08 +010010788 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10789 dhm_p, sizeof( dhm_p ),
10790 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010791 {
10792 return( ret );
10793 }
10794 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010795#endif
10796
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010797 /*
10798 * Preset-specific defaults
10799 */
10800 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010801 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010802 /*
10803 * NSA Suite B
10804 */
10805 case MBEDTLS_SSL_PRESET_SUITEB:
10806 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10807 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10808 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10809 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10810
10811 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10812 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10813 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10814 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10815 ssl_preset_suiteb_ciphersuites;
10816
10817#if defined(MBEDTLS_X509_CRT_PARSE_C)
10818 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010819#endif
10820
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010821#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010822 conf->sig_hashes = ssl_preset_suiteb_hashes;
10823#endif
10824
10825#if defined(MBEDTLS_ECP_C)
10826 conf->curve_list = ssl_preset_suiteb_curves;
10827#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010828 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010829
10830 /*
10831 * Default
10832 */
10833 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010834 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10835 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10836 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10837 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10838 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10839 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10840 MBEDTLS_SSL_MIN_MINOR_VERSION :
10841 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010842 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10843 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10844
10845#if defined(MBEDTLS_SSL_PROTO_DTLS)
10846 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10847 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10848#endif
10849
10850 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10851 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10852 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10853 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10854 mbedtls_ssl_list_ciphersuites();
10855
10856#if defined(MBEDTLS_X509_CRT_PARSE_C)
10857 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10858#endif
10859
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010860#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010861 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010862#endif
10863
10864#if defined(MBEDTLS_ECP_C)
10865 conf->curve_list = mbedtls_ecp_grp_id_list();
10866#endif
10867
10868#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10869 conf->dhm_min_bitlen = 1024;
10870#endif
10871 }
10872
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010873 return( 0 );
10874}
10875
10876/*
10877 * Free mbedtls_ssl_config
10878 */
10879void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10880{
10881#if defined(MBEDTLS_DHM_C)
10882 mbedtls_mpi_free( &conf->dhm_P );
10883 mbedtls_mpi_free( &conf->dhm_G );
10884#endif
10885
10886#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10887 if( conf->psk != NULL )
10888 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010889 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010890 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010891 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010892 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010893 }
10894
10895 if( conf->psk_identity != NULL )
10896 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010897 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010898 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010899 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010900 conf->psk_identity_len = 0;
10901 }
10902#endif
10903
10904#if defined(MBEDTLS_X509_CRT_PARSE_C)
10905 ssl_key_cert_free( conf->key_cert );
10906#endif
10907
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010908 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010909}
10910
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010911#if defined(MBEDTLS_PK_C) && \
10912 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010913/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010917{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010918#if defined(MBEDTLS_RSA_C)
10919 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10920 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010921#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010922#if defined(MBEDTLS_ECDSA_C)
10923 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10924 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010925#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010926 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010927}
10928
Hanno Becker7e5437a2017-04-28 17:15:26 +010010929unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10930{
10931 switch( type ) {
10932 case MBEDTLS_PK_RSA:
10933 return( MBEDTLS_SSL_SIG_RSA );
10934 case MBEDTLS_PK_ECDSA:
10935 case MBEDTLS_PK_ECKEY:
10936 return( MBEDTLS_SSL_SIG_ECDSA );
10937 default:
10938 return( MBEDTLS_SSL_SIG_ANON );
10939 }
10940}
10941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010942mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010943{
10944 switch( sig )
10945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010946#if defined(MBEDTLS_RSA_C)
10947 case MBEDTLS_SSL_SIG_RSA:
10948 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010949#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010950#if defined(MBEDTLS_ECDSA_C)
10951 case MBEDTLS_SSL_SIG_ECDSA:
10952 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010953#endif
10954 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010955 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010956 }
10957}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010958#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010959
Hanno Becker7e5437a2017-04-28 17:15:26 +010010960#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10961 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10962
10963/* Find an entry in a signature-hash set matching a given hash algorithm. */
10964mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10965 mbedtls_pk_type_t sig_alg )
10966{
10967 switch( sig_alg )
10968 {
10969 case MBEDTLS_PK_RSA:
10970 return( set->rsa );
10971 case MBEDTLS_PK_ECDSA:
10972 return( set->ecdsa );
10973 default:
10974 return( MBEDTLS_MD_NONE );
10975 }
10976}
10977
10978/* Add a signature-hash-pair to a signature-hash set */
10979void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10980 mbedtls_pk_type_t sig_alg,
10981 mbedtls_md_type_t md_alg )
10982{
10983 switch( sig_alg )
10984 {
10985 case MBEDTLS_PK_RSA:
10986 if( set->rsa == MBEDTLS_MD_NONE )
10987 set->rsa = md_alg;
10988 break;
10989
10990 case MBEDTLS_PK_ECDSA:
10991 if( set->ecdsa == MBEDTLS_MD_NONE )
10992 set->ecdsa = md_alg;
10993 break;
10994
10995 default:
10996 break;
10997 }
10998}
10999
11000/* Allow exactly one hash algorithm for each signature. */
11001void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11002 mbedtls_md_type_t md_alg )
11003{
11004 set->rsa = md_alg;
11005 set->ecdsa = md_alg;
11006}
11007
11008#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11009 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11010
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011011/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011012 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011014mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011015{
11016 switch( hash )
11017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011018#if defined(MBEDTLS_MD5_C)
11019 case MBEDTLS_SSL_HASH_MD5:
11020 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011021#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011022#if defined(MBEDTLS_SHA1_C)
11023 case MBEDTLS_SSL_HASH_SHA1:
11024 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011025#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011026#if defined(MBEDTLS_SHA256_C)
11027 case MBEDTLS_SSL_HASH_SHA224:
11028 return( MBEDTLS_MD_SHA224 );
11029 case MBEDTLS_SSL_HASH_SHA256:
11030 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011032#if defined(MBEDTLS_SHA512_C)
11033 case MBEDTLS_SSL_HASH_SHA384:
11034 return( MBEDTLS_MD_SHA384 );
11035 case MBEDTLS_SSL_HASH_SHA512:
11036 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011037#endif
11038 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011039 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011040 }
11041}
11042
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011043/*
11044 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11045 */
11046unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11047{
11048 switch( md )
11049 {
11050#if defined(MBEDTLS_MD5_C)
11051 case MBEDTLS_MD_MD5:
11052 return( MBEDTLS_SSL_HASH_MD5 );
11053#endif
11054#if defined(MBEDTLS_SHA1_C)
11055 case MBEDTLS_MD_SHA1:
11056 return( MBEDTLS_SSL_HASH_SHA1 );
11057#endif
11058#if defined(MBEDTLS_SHA256_C)
11059 case MBEDTLS_MD_SHA224:
11060 return( MBEDTLS_SSL_HASH_SHA224 );
11061 case MBEDTLS_MD_SHA256:
11062 return( MBEDTLS_SSL_HASH_SHA256 );
11063#endif
11064#if defined(MBEDTLS_SHA512_C)
11065 case MBEDTLS_MD_SHA384:
11066 return( MBEDTLS_SSL_HASH_SHA384 );
11067 case MBEDTLS_MD_SHA512:
11068 return( MBEDTLS_SSL_HASH_SHA512 );
11069#endif
11070 default:
11071 return( MBEDTLS_SSL_HASH_NONE );
11072 }
11073}
11074
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011075#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011076/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011077 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011078 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011079 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011080int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011082 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011083
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011084 if( ssl->conf->curve_list == NULL )
11085 return( -1 );
11086
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011087 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011088 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011089 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011090
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011091 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011092}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011093#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011094
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011095#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011096/*
11097 * Check if a hash proposed by the peer is in our list.
11098 * Return 0 if we're willing to use it, -1 otherwise.
11099 */
11100int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11101 mbedtls_md_type_t md )
11102{
11103 const int *cur;
11104
11105 if( ssl->conf->sig_hashes == NULL )
11106 return( -1 );
11107
11108 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11109 if( *cur == (int) md )
11110 return( 0 );
11111
11112 return( -1 );
11113}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011114#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011116#if defined(MBEDTLS_X509_CRT_PARSE_C)
11117int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11118 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011119 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011120 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011121{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011122 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011123#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011124 int usage = 0;
11125#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011126#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011127 const char *ext_oid;
11128 size_t ext_len;
11129#endif
11130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011131#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11132 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011133 ((void) cert);
11134 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011135 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011136#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011138#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11139 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011140 {
11141 /* Server part of the key exchange */
11142 switch( ciphersuite->key_exchange )
11143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011144 case MBEDTLS_KEY_EXCHANGE_RSA:
11145 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011146 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011147 break;
11148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011149 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11150 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11151 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11152 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011153 break;
11154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011155 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11156 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011157 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011158 break;
11159
11160 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011161 case MBEDTLS_KEY_EXCHANGE_NONE:
11162 case MBEDTLS_KEY_EXCHANGE_PSK:
11163 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11164 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011165 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011166 usage = 0;
11167 }
11168 }
11169 else
11170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011171 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11172 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011173 }
11174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011175 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011176 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011177 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011178 ret = -1;
11179 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011180#else
11181 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011182#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011184#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11185 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011187 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11188 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011189 }
11190 else
11191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011192 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11193 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011194 }
11195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011196 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011197 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011198 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011199 ret = -1;
11200 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011201#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011202
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011203 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011205#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011206
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011207/*
11208 * Convert version numbers to/from wire format
11209 * and, for DTLS, to/from TLS equivalent.
11210 *
11211 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011212 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011213 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11214 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011216void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011217 unsigned char ver[2] )
11218{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011219#if defined(MBEDTLS_SSL_PROTO_DTLS)
11220 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011222 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011223 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11224
11225 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11226 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11227 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011228 else
11229#else
11230 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011231#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011232 {
11233 ver[0] = (unsigned char) major;
11234 ver[1] = (unsigned char) minor;
11235 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011236}
11237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011238void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011239 const unsigned char ver[2] )
11240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011241#if defined(MBEDTLS_SSL_PROTO_DTLS)
11242 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011243 {
11244 *major = 255 - ver[0] + 2;
11245 *minor = 255 - ver[1] + 1;
11246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011247 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011248 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11249 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011250 else
11251#else
11252 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011253#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011254 {
11255 *major = ver[0];
11256 *minor = ver[1];
11257 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011258}
11259
Simon Butcher99000142016-10-13 17:21:01 +010011260int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11261{
11262#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11263 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11264 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11265
11266 switch( md )
11267 {
11268#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11269#if defined(MBEDTLS_MD5_C)
11270 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011271 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011272#endif
11273#if defined(MBEDTLS_SHA1_C)
11274 case MBEDTLS_SSL_HASH_SHA1:
11275 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11276 break;
11277#endif
11278#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11279#if defined(MBEDTLS_SHA512_C)
11280 case MBEDTLS_SSL_HASH_SHA384:
11281 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11282 break;
11283#endif
11284#if defined(MBEDTLS_SHA256_C)
11285 case MBEDTLS_SSL_HASH_SHA256:
11286 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11287 break;
11288#endif
11289 default:
11290 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11291 }
11292
11293 return 0;
11294#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11295 (void) ssl;
11296 (void) md;
11297
11298 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11299#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11300}
11301
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011302#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11303 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11304int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11305 unsigned char *output,
11306 unsigned char *data, size_t data_len )
11307{
11308 int ret = 0;
11309 mbedtls_md5_context mbedtls_md5;
11310 mbedtls_sha1_context mbedtls_sha1;
11311
11312 mbedtls_md5_init( &mbedtls_md5 );
11313 mbedtls_sha1_init( &mbedtls_sha1 );
11314
11315 /*
11316 * digitally-signed struct {
11317 * opaque md5_hash[16];
11318 * opaque sha_hash[20];
11319 * };
11320 *
11321 * md5_hash
11322 * MD5(ClientHello.random + ServerHello.random
11323 * + ServerParams);
11324 * sha_hash
11325 * SHA(ClientHello.random + ServerHello.random
11326 * + ServerParams);
11327 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011328 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011329 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011331 goto exit;
11332 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011333 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011334 ssl->handshake->randbytes, 64 ) ) != 0 )
11335 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011337 goto exit;
11338 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011339 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011340 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011342 goto exit;
11343 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011344 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011345 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011347 goto exit;
11348 }
11349
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011350 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011351 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011353 goto exit;
11354 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011355 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011356 ssl->handshake->randbytes, 64 ) ) != 0 )
11357 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011359 goto exit;
11360 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011361 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011362 data_len ) ) != 0 )
11363 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011365 goto exit;
11366 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011367 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011368 output + 16 ) ) != 0 )
11369 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011371 goto exit;
11372 }
11373
11374exit:
11375 mbedtls_md5_free( &mbedtls_md5 );
11376 mbedtls_sha1_free( &mbedtls_sha1 );
11377
11378 if( ret != 0 )
11379 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11380 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11381
11382 return( ret );
11383
11384}
11385#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11386 MBEDTLS_SSL_PROTO_TLS1_1 */
11387
11388#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11389 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011390
11391#if defined(MBEDTLS_USE_PSA_CRYPTO)
11392int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11393 unsigned char *hash, size_t *hashlen,
11394 unsigned char *data, size_t data_len,
11395 mbedtls_md_type_t md_alg )
11396{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011397 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011398 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011399 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11400
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011402
11403 if( ( status = psa_hash_setup( &hash_operation,
11404 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011405 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011406 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011407 goto exit;
11408 }
11409
Andrzej Kurek814feff2019-01-14 04:35:19 -050011410 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11411 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011412 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011413 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011414 goto exit;
11415 }
11416
Andrzej Kurek814feff2019-01-14 04:35:19 -050011417 if( ( status = psa_hash_update( &hash_operation,
11418 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011419 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011420 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011421 goto exit;
11422 }
11423
Andrzej Kurek814feff2019-01-14 04:35:19 -050011424 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11425 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011426 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011427 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011428 goto exit;
11429 }
11430
11431exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011432 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011433 {
11434 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11435 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011436 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011437 {
11438 case PSA_ERROR_NOT_SUPPORTED:
11439 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011440 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011441 case PSA_ERROR_BUFFER_TOO_SMALL:
11442 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11443 case PSA_ERROR_INSUFFICIENT_MEMORY:
11444 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11445 default:
11446 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11447 }
11448 }
11449 return( 0 );
11450}
11451
11452#else
11453
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011454int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011455 unsigned char *hash, size_t *hashlen,
11456 unsigned char *data, size_t data_len,
11457 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011458{
11459 int ret = 0;
11460 mbedtls_md_context_t ctx;
11461 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011462 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011463
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011464 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011465
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011466 mbedtls_md_init( &ctx );
11467
11468 /*
11469 * digitally-signed struct {
11470 * opaque client_random[32];
11471 * opaque server_random[32];
11472 * ServerDHParams params;
11473 * };
11474 */
11475 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11476 {
11477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11478 goto exit;
11479 }
11480 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11481 {
11482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11483 goto exit;
11484 }
11485 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11486 {
11487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11488 goto exit;
11489 }
11490 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11491 {
11492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11493 goto exit;
11494 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011495 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011496 {
11497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11498 goto exit;
11499 }
11500
11501exit:
11502 mbedtls_md_free( &ctx );
11503
11504 if( ret != 0 )
11505 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11506 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11507
11508 return( ret );
11509}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011510#endif /* MBEDTLS_USE_PSA_CRYPTO */
11511
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011512#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11513 MBEDTLS_SSL_PROTO_TLS1_2 */
11514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011515#endif /* MBEDTLS_SSL_TLS_C */