blob: 56887f9664d70d021fb105ae4b6e6f65e7f26632 [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
5059 memcpy( &rec->ver[0],
5060 buf + rec_hdr_version_offset,
5061 rec_hdr_version_len );
5062
5063 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5064 ssl->conf->transport,
5065 buf + rec_hdr_version_offset );
5066
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005067 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5070 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005071 }
5072
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005073 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5076 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005077 }
5078
Hanno Beckere5e7e782019-07-11 12:29:35 +01005079 /*
5080 * Parse/Copy record sequence number.
5081 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005082
Hanno Beckere5e7e782019-07-11 12:29:35 +01005083#if defined(MBEDTLS_SSL_PROTO_DTLS)
5084 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5085 {
5086 /* Copy explicit record sequence number from input buffer. */
5087 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5088 rec_hdr_ctr_len );
5089 }
5090 else
5091#endif /* MBEDTLS_SSL_PROTO_DTLS */
5092 {
5093 /* Copy implicit record sequence number from SSL context structure. */
5094 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5095 }
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005096
Hanno Beckere5e7e782019-07-11 12:29:35 +01005097 /*
5098 * Parse record length.
5099 */
5100
5101#define READ_UINT16_BE( p ) \
5102 ( ( *( (unsigned char*)( p ) + 0 ) << 8 ) | \
5103 ( *( (unsigned char*)( p ) + 1 ) << 0 ) )
5104
5105 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
5106 rec->data_len = (size_t) READ_UINT16_BE( buf + rec_hdr_len_offset );
5107 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5108
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005109 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005110 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005111 rec->type,
5112 major_ver, minor_ver, rec->data_len ) );
5113
5114 rec->buf = buf;
5115 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005116
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005117 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005118 * DTLS-related tests.
5119 * Check epoch before checking length constraint because
5120 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5121 * message gets duplicated before the corresponding Finished message,
5122 * the second ChangeCipherSpec should be discarded because it belongs
5123 * to an old epoch, but not because its length is shorter than
5124 * the minimum record length for packets using the new record transform.
5125 * Note that these two kinds of failures are handled differently,
5126 * as an unexpected record is silently skipped but an invalid
5127 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005128 */
5129#if defined(MBEDTLS_SSL_PROTO_DTLS)
5130 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5131 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005132 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005133
Hanno Becker955a5c92019-07-10 17:12:07 +01005134 /* Check that the datagram is large enough to contain a record
5135 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005136 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005137 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5139 (unsigned) len,
5140 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005141 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5142 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005143
Hanno Becker37cfe732019-07-10 17:20:01 +01005144 /* Records from other, non-matching epochs are silently discarded.
5145 * (The case of same-port Client reconnects must be considered in
5146 * the caller). */
Hanno Becker552f7472019-07-19 10:59:12 +01005147 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005148 {
5149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5150 "expected %d, received %d",
5151 ssl->in_epoch, rec_epoch ) );
Hanno Becker552f7472019-07-19 10:59:12 +01005152
5153 /* Records from the next epoch are considered for buffering
5154 * (concretely: early Finished messages). */
5155 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5156 {
5157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5158 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5159 }
5160
Hanno Becker2fddd372019-07-10 14:37:41 +01005161 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005162 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005163#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005164 /* For records from the correct epoch, check whether their
5165 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005166 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005167 {
5168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5169 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5170 }
5171#endif
5172 }
5173#endif /* MBEDTLS_SSL_PROTO_DTLS */
5174
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005175 return( 0 );
5176}
Paul Bakker5121ce52009-01-03 21:22:43 +00005177
Hanno Becker2fddd372019-07-10 14:37:41 +01005178
5179#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5180static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5181{
5182 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5183
5184 /*
5185 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5186 * access the first byte of record content (handshake type), as we
5187 * have an active transform (possibly iv_len != 0), so use the
5188 * fact that the record header len is 13 instead.
5189 */
5190 if( rec_epoch == 0 &&
5191 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5192 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5193 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5194 ssl->in_left > 13 &&
5195 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5196 {
5197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5198 "from the same port" ) );
5199 return( ssl_handle_possible_reconnect( ssl ) );
5200 }
5201
5202 return( 0 );
5203}
5204#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5205
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005206/*
5207 * If applicable, decrypt (and decompress) record content
5208 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005209static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5210 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005211{
5212 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005215 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5218 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222 ret = mbedtls_ssl_hw_record_read( ssl );
5223 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5226 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005227 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005228
5229 if( ret == 0 )
5230 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005231 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005233 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005234 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005235 unsigned char const old_msg_type = rec->type;
5236
Hanno Beckera18d1322018-01-03 14:27:32 +00005237 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005238 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005241
Hanno Beckera0e20d02019-05-15 14:03:01 +01005242#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005243 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5244 ssl->conf->ignore_unexpected_cid
5245 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5246 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005247 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005248 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005249 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005250#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005251
Paul Bakker5121ce52009-01-03 21:22:43 +00005252 return( ret );
5253 }
5254
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005255 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005256 {
5257 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005258 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005259 }
5260
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005261 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005262 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005263
Hanno Beckera0e20d02019-05-15 14:03:01 +01005264#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005265 /* We have already checked the record content type
5266 * in ssl_parse_record_header(), failing or silently
5267 * dropping the record in the case of an unknown type.
5268 *
5269 * Since with the use of CIDs, the record content type
5270 * might change during decryption, re-check the record
5271 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005272 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005273 {
5274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5275 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5276 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005277#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005278
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005279 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005280 {
5281#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5282 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005283 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005284 {
5285 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5287 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5288 }
5289#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5290
5291 ssl->nb_zero++;
5292
5293 /*
5294 * Three or more empty messages may be a DoS attack
5295 * (excessive CPU consumption).
5296 */
5297 if( ssl->nb_zero > 3 )
5298 {
5299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005300 "messages, possible DoS attack" ) );
5301 /* Treat the records as if they were not properly authenticated,
5302 * thereby failing the connection if we see more than allowed
5303 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005304 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5305 }
5306 }
5307 else
5308 ssl->nb_zero = 0;
5309
5310#if defined(MBEDTLS_SSL_PROTO_DTLS)
5311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5312 {
5313 ; /* in_ctr read from peer, not maintained internally */
5314 }
5315 else
5316#endif
5317 {
5318 unsigned i;
5319 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5320 if( ++ssl->in_ctr[i - 1] != 0 )
5321 break;
5322
5323 /* The loop goes to its end iff the counter is wrapping */
5324 if( i == ssl_ep_len( ssl ) )
5325 {
5326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5327 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5328 }
5329 }
5330
Paul Bakker5121ce52009-01-03 21:22:43 +00005331 }
5332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005334 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005336 {
5337 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005340 return( ret );
5341 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005342 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005346 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005349 }
5350#endif
5351
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005352 /* Check actual (decrypted) record content length against
5353 * configured maximum. */
5354 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5355 {
5356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5357 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5358 }
5359
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005360 return( 0 );
5361}
5362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005364
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005365/*
5366 * Read a record.
5367 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005368 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5369 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5370 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005371 */
Hanno Becker1097b342018-08-15 14:09:41 +01005372
5373/* Helper functions for mbedtls_ssl_read_record(). */
5374static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005375static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5376static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005377
Hanno Becker327c93b2018-08-15 13:56:18 +01005378int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005379 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005380{
5381 int ret;
5382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005384
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005385 if( ssl->keep_current_message == 0 )
5386 {
5387 do {
Simon Butcher99000142016-10-13 17:21:01 +01005388
Hanno Becker26994592018-08-15 14:14:59 +01005389 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005390 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005391 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005392
Hanno Beckere74d5562018-08-15 14:26:08 +01005393 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005394 {
Hanno Becker40f50842018-08-15 14:48:01 +01005395#if defined(MBEDTLS_SSL_PROTO_DTLS)
5396 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005397
Hanno Becker40f50842018-08-15 14:48:01 +01005398 /* We only check for buffered messages if the
5399 * current datagram is fully consumed. */
5400 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005401 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005402 {
Hanno Becker40f50842018-08-15 14:48:01 +01005403 if( ssl_load_buffered_message( ssl ) == 0 )
5404 have_buffered = 1;
5405 }
5406
5407 if( have_buffered == 0 )
5408#endif /* MBEDTLS_SSL_PROTO_DTLS */
5409 {
5410 ret = ssl_get_next_record( ssl );
5411 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5412 continue;
5413
5414 if( ret != 0 )
5415 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005416 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005417 return( ret );
5418 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005419 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005420 }
5421
5422 ret = mbedtls_ssl_handle_message_type( ssl );
5423
Hanno Becker40f50842018-08-15 14:48:01 +01005424#if defined(MBEDTLS_SSL_PROTO_DTLS)
5425 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5426 {
5427 /* Buffer future message */
5428 ret = ssl_buffer_message( ssl );
5429 if( ret != 0 )
5430 return( ret );
5431
5432 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5433 }
5434#endif /* MBEDTLS_SSL_PROTO_DTLS */
5435
Hanno Becker90333da2017-10-10 11:27:13 +01005436 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5437 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005438
5439 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005440 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005441 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005442 return( ret );
5443 }
5444
Hanno Becker327c93b2018-08-15 13:56:18 +01005445 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005446 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005447 {
5448 mbedtls_ssl_update_handshake_status( ssl );
5449 }
Simon Butcher99000142016-10-13 17:21:01 +01005450 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005451 else
Simon Butcher99000142016-10-13 17:21:01 +01005452 {
Hanno Becker02f59072018-08-15 14:00:24 +01005453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005454 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005455 }
5456
5457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5458
5459 return( 0 );
5460}
5461
Hanno Becker40f50842018-08-15 14:48:01 +01005462#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005463static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005464{
Hanno Becker40f50842018-08-15 14:48:01 +01005465 if( ssl->in_left > ssl->next_record_offset )
5466 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005467
Hanno Becker40f50842018-08-15 14:48:01 +01005468 return( 0 );
5469}
5470
5471static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5472{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005473 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005474 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005475 int ret = 0;
5476
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005477 if( hs == NULL )
5478 return( -1 );
5479
Hanno Beckere00ae372018-08-20 09:39:42 +01005480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5481
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005482 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5483 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5484 {
5485 /* Check if we have seen a ChangeCipherSpec before.
5486 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005487 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005488 {
5489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5490 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005491 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005492 }
5493
Hanno Becker39b8bc92018-08-28 17:17:13 +01005494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005495 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5496 ssl->in_msglen = 1;
5497 ssl->in_msg[0] = 1;
5498
5499 /* As long as they are equal, the exact value doesn't matter. */
5500 ssl->in_left = 0;
5501 ssl->next_record_offset = 0;
5502
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005503 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005504 goto exit;
5505 }
Hanno Becker37f95322018-08-16 13:55:32 +01005506
Hanno Beckerb8f50142018-08-28 10:01:34 +01005507#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005508 /* Debug only */
5509 {
5510 unsigned offset;
5511 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5512 {
5513 hs_buf = &hs->buffering.hs[offset];
5514 if( hs_buf->is_valid == 1 )
5515 {
5516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5517 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005518 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005519 }
5520 }
5521 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005522#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005523
5524 /* Check if we have buffered and/or fully reassembled the
5525 * next handshake message. */
5526 hs_buf = &hs->buffering.hs[0];
5527 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5528 {
5529 /* Synthesize a record containing the buffered HS message. */
5530 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5531 ( hs_buf->data[2] << 8 ) |
5532 hs_buf->data[3];
5533
5534 /* Double-check that we haven't accidentally buffered
5535 * a message that doesn't fit into the input buffer. */
5536 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5537 {
5538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5540 }
5541
5542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5543 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5544 hs_buf->data, msg_len + 12 );
5545
5546 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5547 ssl->in_hslen = msg_len + 12;
5548 ssl->in_msglen = msg_len + 12;
5549 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5550
5551 ret = 0;
5552 goto exit;
5553 }
5554 else
5555 {
5556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5557 hs->in_msg_seq ) );
5558 }
5559
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005560 ret = -1;
5561
5562exit:
5563
5564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5565 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005566}
5567
Hanno Beckera02b0b42018-08-21 17:20:27 +01005568static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5569 size_t desired )
5570{
5571 int offset;
5572 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5574 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005575
Hanno Becker01315ea2018-08-21 17:22:17 +01005576 /* Get rid of future records epoch first, if such exist. */
5577 ssl_free_buffered_record( ssl );
5578
5579 /* Check if we have enough space available now. */
5580 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5581 hs->buffering.total_bytes_buffered ) )
5582 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005584 return( 0 );
5585 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005586
Hanno Becker4f432ad2018-08-28 10:02:32 +01005587 /* We don't have enough space to buffer the next expected handshake
5588 * message. Remove buffers used for future messages to gain space,
5589 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005590 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5591 offset >= 0; offset-- )
5592 {
5593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5594 offset ) );
5595
Hanno Beckerb309b922018-08-23 13:18:05 +01005596 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005597
5598 /* Check if we have enough space available now. */
5599 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5600 hs->buffering.total_bytes_buffered ) )
5601 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005603 return( 0 );
5604 }
5605 }
5606
5607 return( -1 );
5608}
5609
Hanno Becker40f50842018-08-15 14:48:01 +01005610static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5611{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005612 int ret = 0;
5613 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5614
5615 if( hs == NULL )
5616 return( 0 );
5617
5618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5619
5620 switch( ssl->in_msgtype )
5621 {
5622 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005624
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005625 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005626 break;
5627
5628 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005629 {
5630 unsigned recv_msg_seq_offset;
5631 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5632 mbedtls_ssl_hs_buffer *hs_buf;
5633 size_t msg_len = ssl->in_hslen - 12;
5634
5635 /* We should never receive an old handshake
5636 * message - double-check nonetheless. */
5637 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5638 {
5639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5641 }
5642
5643 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5644 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5645 {
5646 /* Silently ignore -- message too far in the future */
5647 MBEDTLS_SSL_DEBUG_MSG( 2,
5648 ( "Ignore future HS message with sequence number %u, "
5649 "buffering window %u - %u",
5650 recv_msg_seq, ssl->handshake->in_msg_seq,
5651 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5652
5653 goto exit;
5654 }
5655
5656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5657 recv_msg_seq, recv_msg_seq_offset ) );
5658
5659 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5660
5661 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005662 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005663 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005664 size_t reassembly_buf_sz;
5665
Hanno Becker37f95322018-08-16 13:55:32 +01005666 hs_buf->is_fragmented =
5667 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5668
5669 /* We copy the message back into the input buffer
5670 * after reassembly, so check that it's not too large.
5671 * This is an implementation-specific limitation
5672 * and not one from the standard, hence it is not
5673 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005674 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005675 {
5676 /* Ignore message */
5677 goto exit;
5678 }
5679
Hanno Beckere0b150f2018-08-21 15:51:03 +01005680 /* Check if we have enough space to buffer the message. */
5681 if( hs->buffering.total_bytes_buffered >
5682 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5683 {
5684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5686 }
5687
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005688 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5689 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005690
5691 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5692 hs->buffering.total_bytes_buffered ) )
5693 {
5694 if( recv_msg_seq_offset > 0 )
5695 {
5696 /* If we can't buffer a future message because
5697 * of space limitations -- ignore. */
5698 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",
5699 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5700 (unsigned) hs->buffering.total_bytes_buffered ) );
5701 goto exit;
5702 }
Hanno Beckere1801392018-08-21 16:51:05 +01005703 else
5704 {
5705 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",
5706 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5707 (unsigned) hs->buffering.total_bytes_buffered ) );
5708 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005709
Hanno Beckera02b0b42018-08-21 17:20:27 +01005710 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005711 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005712 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",
5713 (unsigned) msg_len,
5714 (unsigned) reassembly_buf_sz,
5715 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005716 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005717 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5718 goto exit;
5719 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005720 }
5721
5722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5723 msg_len ) );
5724
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005725 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5726 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005727 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005728 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005729 goto exit;
5730 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005731 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005732
5733 /* Prepare final header: copy msg_type, length and message_seq,
5734 * then add standardised fragment_offset and fragment_length */
5735 memcpy( hs_buf->data, ssl->in_msg, 6 );
5736 memset( hs_buf->data + 6, 0, 3 );
5737 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5738
5739 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005740
5741 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005742 }
5743 else
5744 {
5745 /* Make sure msg_type and length are consistent */
5746 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5747 {
5748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5749 /* Ignore */
5750 goto exit;
5751 }
5752 }
5753
Hanno Becker4422bbb2018-08-20 09:40:19 +01005754 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005755 {
5756 size_t frag_len, frag_off;
5757 unsigned char * const msg = hs_buf->data + 12;
5758
5759 /*
5760 * Check and copy current fragment
5761 */
5762
5763 /* Validation of header fields already done in
5764 * mbedtls_ssl_prepare_handshake_record(). */
5765 frag_off = ssl_get_hs_frag_off( ssl );
5766 frag_len = ssl_get_hs_frag_len( ssl );
5767
5768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5769 frag_off, frag_len ) );
5770 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5771
5772 if( hs_buf->is_fragmented )
5773 {
5774 unsigned char * const bitmask = msg + msg_len;
5775 ssl_bitmask_set( bitmask, frag_off, frag_len );
5776 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5777 msg_len ) == 0 );
5778 }
5779 else
5780 {
5781 hs_buf->is_complete = 1;
5782 }
5783
5784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5785 hs_buf->is_complete ? "" : "not yet " ) );
5786 }
5787
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005788 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005789 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005790
5791 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005792 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005793 break;
5794 }
5795
5796exit:
5797
5798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5799 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005800}
5801#endif /* MBEDTLS_SSL_PROTO_DTLS */
5802
Hanno Becker1097b342018-08-15 14:09:41 +01005803static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005804{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005805 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005806 * Consume last content-layer message and potentially
5807 * update in_msglen which keeps track of the contents'
5808 * consumption state.
5809 *
5810 * (1) Handshake messages:
5811 * Remove last handshake message, move content
5812 * and adapt in_msglen.
5813 *
5814 * (2) Alert messages:
5815 * Consume whole record content, in_msglen = 0.
5816 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005817 * (3) Change cipher spec:
5818 * Consume whole record content, in_msglen = 0.
5819 *
5820 * (4) Application data:
5821 * Don't do anything - the record layer provides
5822 * the application data as a stream transport
5823 * and consumes through mbedtls_ssl_read only.
5824 *
5825 */
5826
5827 /* Case (1): Handshake messages */
5828 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005829 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005830 /* Hard assertion to be sure that no application data
5831 * is in flight, as corrupting ssl->in_msglen during
5832 * ssl->in_offt != NULL is fatal. */
5833 if( ssl->in_offt != NULL )
5834 {
5835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5837 }
5838
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005839 /*
5840 * Get next Handshake message in the current record
5841 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005842
Hanno Becker4a810fb2017-05-24 16:27:30 +01005843 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005844 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005845 * current handshake content: If DTLS handshake
5846 * fragmentation is used, that's the fragment
5847 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005848 * size here is faulty and should be changed at
5849 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005850 * (2) While it doesn't seem to cause problems, one
5851 * has to be very careful not to assume that in_hslen
5852 * is always <= in_msglen in a sensible communication.
5853 * Again, it's wrong for DTLS handshake fragmentation.
5854 * The following check is therefore mandatory, and
5855 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005856 * Additionally, ssl->in_hslen might be arbitrarily out of
5857 * bounds after handling a DTLS message with an unexpected
5858 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005859 */
5860 if( ssl->in_hslen < ssl->in_msglen )
5861 {
5862 ssl->in_msglen -= ssl->in_hslen;
5863 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5864 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005865
Hanno Becker4a810fb2017-05-24 16:27:30 +01005866 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5867 ssl->in_msg, ssl->in_msglen );
5868 }
5869 else
5870 {
5871 ssl->in_msglen = 0;
5872 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005873
Hanno Becker4a810fb2017-05-24 16:27:30 +01005874 ssl->in_hslen = 0;
5875 }
5876 /* Case (4): Application data */
5877 else if( ssl->in_offt != NULL )
5878 {
5879 return( 0 );
5880 }
5881 /* Everything else (CCS & Alerts) */
5882 else
5883 {
5884 ssl->in_msglen = 0;
5885 }
5886
Hanno Becker1097b342018-08-15 14:09:41 +01005887 return( 0 );
5888}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005889
Hanno Beckere74d5562018-08-15 14:26:08 +01005890static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5891{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005892 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005893 return( 1 );
5894
5895 return( 0 );
5896}
5897
Hanno Becker5f066e72018-08-16 14:56:31 +01005898#if defined(MBEDTLS_SSL_PROTO_DTLS)
5899
5900static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5901{
5902 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5903 if( hs == NULL )
5904 return;
5905
Hanno Becker01315ea2018-08-21 17:22:17 +01005906 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005907 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005908 hs->buffering.total_bytes_buffered -=
5909 hs->buffering.future_record.len;
5910
5911 mbedtls_free( hs->buffering.future_record.data );
5912 hs->buffering.future_record.data = NULL;
5913 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005914}
5915
5916static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5917{
5918 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5919 unsigned char * rec;
5920 size_t rec_len;
5921 unsigned rec_epoch;
5922
5923 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5924 return( 0 );
5925
5926 if( hs == NULL )
5927 return( 0 );
5928
Hanno Becker5f066e72018-08-16 14:56:31 +01005929 rec = hs->buffering.future_record.data;
5930 rec_len = hs->buffering.future_record.len;
5931 rec_epoch = hs->buffering.future_record.epoch;
5932
5933 if( rec == NULL )
5934 return( 0 );
5935
Hanno Becker4cb782d2018-08-20 11:19:05 +01005936 /* Only consider loading future records if the
5937 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005938 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005939 return( 0 );
5940
Hanno Becker5f066e72018-08-16 14:56:31 +01005941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5942
5943 if( rec_epoch != ssl->in_epoch )
5944 {
5945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5946 goto exit;
5947 }
5948
5949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5950
5951 /* Double-check that the record is not too large */
5952 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5953 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5954 {
5955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5956 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5957 }
5958
5959 memcpy( ssl->in_hdr, rec, rec_len );
5960 ssl->in_left = rec_len;
5961 ssl->next_record_offset = 0;
5962
5963 ssl_free_buffered_record( ssl );
5964
5965exit:
5966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5967 return( 0 );
5968}
5969
Hanno Becker519f15d2019-07-11 12:43:20 +01005970static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
5971 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01005972{
5973 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01005974
5975 /* Don't buffer future records outside handshakes. */
5976 if( hs == NULL )
5977 return( 0 );
5978
5979 /* Only buffer handshake records (we are only interested
5980 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01005981 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01005982 return( 0 );
5983
5984 /* Don't buffer more than one future epoch record. */
5985 if( hs->buffering.future_record.data != NULL )
5986 return( 0 );
5987
Hanno Becker01315ea2018-08-21 17:22:17 +01005988 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01005989 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01005990 hs->buffering.total_bytes_buffered ) )
5991 {
5992 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 +01005993 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01005994 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005995 return( 0 );
5996 }
5997
Hanno Becker5f066e72018-08-16 14:56:31 +01005998 /* Buffer record */
5999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6000 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006001 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006002
6003 /* ssl_parse_record_header() only considers records
6004 * of the next epoch as candidates for buffering. */
6005 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006006 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006007
6008 hs->buffering.future_record.data =
6009 mbedtls_calloc( 1, hs->buffering.future_record.len );
6010 if( hs->buffering.future_record.data == NULL )
6011 {
6012 /* If we run out of RAM trying to buffer a
6013 * record from the next epoch, just ignore. */
6014 return( 0 );
6015 }
6016
Hanno Becker519f15d2019-07-11 12:43:20 +01006017 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006018
Hanno Becker519f15d2019-07-11 12:43:20 +01006019 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006020 return( 0 );
6021}
6022
6023#endif /* MBEDTLS_SSL_PROTO_DTLS */
6024
Hanno Beckere74d5562018-08-15 14:26:08 +01006025static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006026{
6027 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006028 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006029
Hanno Becker5f066e72018-08-16 14:56:31 +01006030#if defined(MBEDTLS_SSL_PROTO_DTLS)
6031 /* We might have buffered a future record; if so,
6032 * and if the epoch matches now, load it.
6033 * On success, this call will set ssl->in_left to
6034 * the length of the buffered record, so that
6035 * the calls to ssl_fetch_input() below will
6036 * essentially be no-ops. */
6037 ret = ssl_load_buffered_record( ssl );
6038 if( ret != 0 )
6039 return( ret );
6040#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006041
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006042 /* Ensure that we have enough space available for the default form
6043 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6044 * with no space for CIDs counted in). */
6045 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6046 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006049 return( ret );
6050 }
6051
Hanno Beckere5e7e782019-07-11 12:29:35 +01006052 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6053 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006056 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006057 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006058 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6059 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006060 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006061 if( ret != 0 )
6062 return( ret );
6063
6064 /* Fall through to handling of unexpected records */
6065 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6066 }
6067
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006068 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6069 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006070#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006071 /* Reset in pointers to default state for TLS/DTLS records,
6072 * assuming no CID and no offset between record content and
6073 * record plaintext. */
6074 ssl_update_in_pointers( ssl );
6075
Hanno Becker7ae20e02019-07-12 08:33:49 +01006076 /* Setup internal message pointers from record structure. */
6077 ssl->in_msgtype = rec.type;
6078#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6079 ssl->in_len = ssl->in_cid + rec.cid_len;
6080#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6081 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6082 ssl->in_msglen = rec.data_len;
6083
Hanno Becker2fddd372019-07-10 14:37:41 +01006084 ret = ssl_check_client_reconnect( ssl );
6085 if( ret != 0 )
6086 return( ret );
6087#endif
6088
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006089 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006090 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006091
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6093 "(header)" ) );
6094 }
6095 else
6096 {
6097 /* Skip invalid record and the rest of the datagram */
6098 ssl->next_record_offset = 0;
6099 ssl->in_left = 0;
6100
6101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6102 "(header)" ) );
6103 }
6104
6105 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006106 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006107 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006108 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006109#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006110 {
6111 return( ret );
6112 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006113 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006116 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006117 {
Hanno Beckera8814792019-07-10 15:01:45 +01006118 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006119 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006120 if( ssl->next_record_offset < ssl->in_left )
6121 {
6122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6123 }
6124 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006125 else
6126#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006127 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006128 /*
6129 * Fetch record contents from underlying transport.
6130 */
Hanno Beckera3175662019-07-11 12:50:29 +01006131 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006132 if( ret != 0 )
6133 {
6134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6135 return( ret );
6136 }
6137
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006138 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006139 }
6140
6141 /*
6142 * Decrypt record contents.
6143 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006144
Hanno Beckerfdf66042019-07-11 13:07:45 +01006145 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006148 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006149 {
6150 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006151 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006152 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006153 /* Except when waiting for Finished as a bad mac here
6154 * probably means something went wrong in the handshake
6155 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6156 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6157 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6158 {
6159#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6160 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6161 {
6162 mbedtls_ssl_send_alert_message( ssl,
6163 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6164 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6165 }
6166#endif
6167 return( ret );
6168 }
6169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006171 if( ssl->conf->badmac_limit != 0 &&
6172 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6175 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006176 }
6177#endif
6178
Hanno Becker4a810fb2017-05-24 16:27:30 +01006179 /* As above, invalid records cause
6180 * dismissal of the whole datagram. */
6181
6182 ssl->next_record_offset = 0;
6183 ssl->in_left = 0;
6184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006186 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006187 }
6188
6189 return( ret );
6190 }
6191 else
6192#endif
6193 {
6194 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6196 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198 mbedtls_ssl_send_alert_message( ssl,
6199 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6200 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006201 }
6202#endif
6203 return( ret );
6204 }
6205 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006206
Hanno Becker44d89b22019-07-12 09:40:44 +01006207
6208 /* Reset in pointers to default state for TLS/DTLS records,
6209 * assuming no CID and no offset between record content and
6210 * record plaintext. */
6211 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006212#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6213 ssl->in_len = ssl->in_cid + rec.cid_len;
6214#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6215 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006216
Hanno Becker8685c822019-07-12 09:37:30 +01006217 /* The record content type may change during decryption,
6218 * so re-read it. */
6219 ssl->in_msgtype = rec.type;
6220 /* Also update the input buffer, because unfortunately
6221 * the server-side ssl_parse_client_hello() reparses the
6222 * record header when receiving a ClientHello initiating
6223 * a renegotiation. */
6224 ssl->in_hdr[0] = rec.type;
6225 ssl->in_msg = rec.buf + rec.data_offset;
6226 ssl->in_msglen = rec.data_len;
6227 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6228 ssl->in_len[1] = (unsigned char)( rec.data_len );
6229
Simon Butcher99000142016-10-13 17:21:01 +01006230 return( 0 );
6231}
6232
6233int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6234{
6235 int ret;
6236
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006237 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006238 * Handle particular types of records
6239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006240 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006241 {
Simon Butcher99000142016-10-13 17:21:01 +01006242 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6243 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006244 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006245 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006246 }
6247
Hanno Beckere678eaa2018-08-21 14:57:46 +01006248 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006249 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006250 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006251 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6253 ssl->in_msglen ) );
6254 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006255 }
6256
Hanno Beckere678eaa2018-08-21 14:57:46 +01006257 if( ssl->in_msg[0] != 1 )
6258 {
6259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6260 ssl->in_msg[0] ) );
6261 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6262 }
6263
6264#if defined(MBEDTLS_SSL_PROTO_DTLS)
6265 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6266 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6267 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6268 {
6269 if( ssl->handshake == NULL )
6270 {
6271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6272 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6273 }
6274
6275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6276 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6277 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006278#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006279 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006282 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006283 if( ssl->in_msglen != 2 )
6284 {
6285 /* Note: Standard allows for more than one 2 byte alert
6286 to be packed in a single message, but Mbed TLS doesn't
6287 currently support this. */
6288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6289 ssl->in_msglen ) );
6290 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6291 }
6292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006294 ssl->in_msg[0], ssl->in_msg[1] ) );
6295
6296 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006297 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006302 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006304 }
6305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6307 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6310 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006311 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006312
6313#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6314 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6315 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6316 {
Hanno Becker90333da2017-10-10 11:27:13 +01006317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006318 /* Will be handled when trying to parse ServerHello */
6319 return( 0 );
6320 }
6321#endif
6322
6323#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6324 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6325 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6326 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6327 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6328 {
6329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6330 /* Will be handled in mbedtls_ssl_parse_certificate() */
6331 return( 0 );
6332 }
6333#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6334
6335 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006336 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006337 }
6338
Hanno Beckerc76c6192017-06-06 10:03:17 +01006339#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006340 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006341 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006342 /* Drop unexpected ApplicationData records,
6343 * except at the beginning of renegotiations */
6344 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6345 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6346#if defined(MBEDTLS_SSL_RENEGOTIATION)
6347 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6348 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006349#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006350 )
6351 {
6352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6353 return( MBEDTLS_ERR_SSL_NON_FATAL );
6354 }
6355
6356 if( ssl->handshake != NULL &&
6357 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6358 {
6359 ssl_handshake_wrapup_free_hs_transform( ssl );
6360 }
6361 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006362#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006363
Paul Bakker5121ce52009-01-03 21:22:43 +00006364 return( 0 );
6365}
6366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006367int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006368{
6369 int ret;
6370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6372 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6373 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006374 {
6375 return( ret );
6376 }
6377
6378 return( 0 );
6379}
6380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006382 unsigned char level,
6383 unsigned char message )
6384{
6385 int ret;
6386
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006387 if( ssl == NULL || ssl->conf == NULL )
6388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006391 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006393 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006394 ssl->out_msglen = 2;
6395 ssl->out_msg[0] = level;
6396 ssl->out_msg[1] = message;
6397
Hanno Becker67bc7c32018-08-06 11:33:50 +01006398 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006401 return( ret );
6402 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006404
6405 return( 0 );
6406}
6407
Hanno Beckerb9d44792019-02-08 07:19:04 +00006408#if defined(MBEDTLS_X509_CRT_PARSE_C)
6409static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6410{
6411#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6412 if( session->peer_cert != NULL )
6413 {
6414 mbedtls_x509_crt_free( session->peer_cert );
6415 mbedtls_free( session->peer_cert );
6416 session->peer_cert = NULL;
6417 }
6418#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6419 if( session->peer_cert_digest != NULL )
6420 {
6421 /* Zeroization is not necessary. */
6422 mbedtls_free( session->peer_cert_digest );
6423 session->peer_cert_digest = NULL;
6424 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6425 session->peer_cert_digest_len = 0;
6426 }
6427#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6428}
6429#endif /* MBEDTLS_X509_CRT_PARSE_C */
6430
Paul Bakker5121ce52009-01-03 21:22:43 +00006431/*
6432 * Handshake functions
6433 */
Hanno Becker21489932019-02-05 13:20:55 +00006434#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006435/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006437{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006438 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6439 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006442
Hanno Becker7177a882019-02-05 13:36:46 +00006443 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006446 ssl->state++;
6447 return( 0 );
6448 }
6449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6451 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006452}
6453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006455{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006456 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6457 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006460
Hanno Becker7177a882019-02-05 13:36:46 +00006461 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006464 ssl->state++;
6465 return( 0 );
6466 }
6467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006470}
Gilles Peskinef9828522017-05-03 12:28:43 +02006471
Hanno Becker21489932019-02-05 13:20:55 +00006472#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006473/* Some certificate support -> implement write and parse */
6474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006478 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006480 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6481 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006484
Hanno Becker7177a882019-02-05 13:36:46 +00006485 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006488 ssl->state++;
6489 return( 0 );
6490 }
6491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006493 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006494 {
6495 if( ssl->client_auth == 0 )
6496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006498 ssl->state++;
6499 return( 0 );
6500 }
6501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006503 /*
6504 * If using SSLv3 and got no cert, send an Alert message
6505 * (otherwise an empty Certificate message will be sent).
6506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6508 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006509 {
6510 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6512 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6513 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006516 goto write_msg;
6517 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006519 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520#endif /* MBEDTLS_SSL_CLI_C */
6521#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006522 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6527 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006528 }
6529 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006530#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006533
6534 /*
6535 * 0 . 0 handshake type
6536 * 1 . 3 handshake length
6537 * 4 . 6 length of all certs
6538 * 7 . 9 length of cert. 1
6539 * 10 . n-1 peer certificate
6540 * n . n+2 length of cert. 2
6541 * n+3 . ... upper level cert, etc.
6542 */
6543 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006545
Paul Bakker29087132010-03-21 21:03:34 +00006546 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006547 {
6548 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006549 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006552 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006554 }
6555
6556 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6557 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6558 ssl->out_msg[i + 2] = (unsigned char)( n );
6559
6560 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6561 i += n; crt = crt->next;
6562 }
6563
6564 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6565 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6566 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6567
6568 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6570 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006571
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006572#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006573write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006574#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006575
6576 ssl->state++;
6577
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006578 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006579 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006581 return( ret );
6582 }
6583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006585
Paul Bakkered27a042013-04-18 22:46:23 +02006586 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006587}
6588
Hanno Becker84879e32019-01-31 07:44:03 +00006589#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006590
6591#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006592static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6593 unsigned char *crt_buf,
6594 size_t crt_buf_len )
6595{
6596 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6597
6598 if( peer_crt == NULL )
6599 return( -1 );
6600
6601 if( peer_crt->raw.len != crt_buf_len )
6602 return( -1 );
6603
Hanno Becker46f34d02019-02-08 14:00:04 +00006604 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006605}
Hanno Becker177475a2019-02-05 17:02:46 +00006606#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6607static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6608 unsigned char *crt_buf,
6609 size_t crt_buf_len )
6610{
6611 int ret;
6612 unsigned char const * const peer_cert_digest =
6613 ssl->session->peer_cert_digest;
6614 mbedtls_md_type_t const peer_cert_digest_type =
6615 ssl->session->peer_cert_digest_type;
6616 mbedtls_md_info_t const * const digest_info =
6617 mbedtls_md_info_from_type( peer_cert_digest_type );
6618 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6619 size_t digest_len;
6620
6621 if( peer_cert_digest == NULL || digest_info == NULL )
6622 return( -1 );
6623
6624 digest_len = mbedtls_md_get_size( digest_info );
6625 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6626 return( -1 );
6627
6628 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6629 if( ret != 0 )
6630 return( -1 );
6631
6632 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6633}
6634#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006635#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006636
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006637/*
6638 * Once the certificate message is read, parse it into a cert chain and
6639 * perform basic checks, but leave actual verification to the caller
6640 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006641static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6642 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006643{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006644 int ret;
6645#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6646 int crt_cnt=0;
6647#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006648 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006649 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006654 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6655 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006657 }
6658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6660 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006663 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6664 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006666 }
6667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006669
Paul Bakker5121ce52009-01-03 21:22:43 +00006670 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006672 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006673 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006674
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006675 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006679 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6680 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006681 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006682 }
6683
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006684 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6685 i += 3;
6686
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006687 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006688 while( i < ssl->in_hslen )
6689 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006690 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006691 if ( i + 3 > ssl->in_hslen ) {
6692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006693 mbedtls_ssl_send_alert_message( ssl,
6694 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6695 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006696 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6697 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006698 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6699 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006700 if( ssl->in_msg[i] != 0 )
6701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006703 mbedtls_ssl_send_alert_message( ssl,
6704 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6705 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006707 }
6708
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006709 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006710 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6711 | (unsigned int) ssl->in_msg[i + 2];
6712 i += 3;
6713
6714 if( n < 128 || i + n > ssl->in_hslen )
6715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006717 mbedtls_ssl_send_alert_message( ssl,
6718 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6719 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006721 }
6722
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006723 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006724#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6725 if( crt_cnt++ == 0 &&
6726 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6727 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006728 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006729 /* During client-side renegotiation, check that the server's
6730 * end-CRTs hasn't changed compared to the initial handshake,
6731 * mitigating the triple handshake attack. On success, reuse
6732 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6734 if( ssl_check_peer_crt_unchanged( ssl,
6735 &ssl->in_msg[i],
6736 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006737 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6739 mbedtls_ssl_send_alert_message( ssl,
6740 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6741 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6742 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006743 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006744
6745 /* Now we can safely free the original chain. */
6746 ssl_clear_peer_cert( ssl->session );
6747 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006748#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6749
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006750 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006751#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006752 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006753#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006754 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006755 * it in-place from the input buffer instead of making a copy. */
6756 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6757#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006758 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006759 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006760 case 0: /*ok*/
6761 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6762 /* Ignore certificate with an unknown algorithm: maybe a
6763 prior certificate was already trusted. */
6764 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006765
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006766 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6767 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6768 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006769
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006770 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6771 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6772 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006773
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006774 default:
6775 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6776 crt_parse_der_failed:
6777 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6778 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6779 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 }
6781
6782 i += n;
6783 }
6784
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006785 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006786 return( 0 );
6787}
6788
Hanno Becker4a55f632019-02-05 12:49:06 +00006789#if defined(MBEDTLS_SSL_SRV_C)
6790static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6791{
6792 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6793 return( -1 );
6794
6795#if defined(MBEDTLS_SSL_PROTO_SSL3)
6796 /*
6797 * Check if the client sent an empty certificate
6798 */
6799 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6800 {
6801 if( ssl->in_msglen == 2 &&
6802 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6803 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6804 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6805 {
6806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6807 return( 0 );
6808 }
6809
6810 return( -1 );
6811 }
6812#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6813
6814#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6815 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6816 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6817 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6818 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6819 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6820 {
6821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6822 return( 0 );
6823 }
6824
6825 return( -1 );
6826#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6827 MBEDTLS_SSL_PROTO_TLS1_2 */
6828}
6829#endif /* MBEDTLS_SSL_SRV_C */
6830
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006831/* Check if a certificate message is expected.
6832 * Return either
6833 * - SSL_CERTIFICATE_EXPECTED, or
6834 * - SSL_CERTIFICATE_SKIP
6835 * indicating whether a Certificate message is expected or not.
6836 */
6837#define SSL_CERTIFICATE_EXPECTED 0
6838#define SSL_CERTIFICATE_SKIP 1
6839static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6840 int authmode )
6841{
6842 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006843 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006844
6845 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6846 return( SSL_CERTIFICATE_SKIP );
6847
6848#if defined(MBEDTLS_SSL_SRV_C)
6849 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6850 {
6851 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6852 return( SSL_CERTIFICATE_SKIP );
6853
6854 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6855 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006856 ssl->session_negotiate->verify_result =
6857 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6858 return( SSL_CERTIFICATE_SKIP );
6859 }
6860 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006861#else
6862 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006863#endif /* MBEDTLS_SSL_SRV_C */
6864
6865 return( SSL_CERTIFICATE_EXPECTED );
6866}
6867
Hanno Becker68636192019-02-05 14:36:34 +00006868static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6869 int authmode,
6870 mbedtls_x509_crt *chain,
6871 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006872{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006873 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006874 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006875 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006876 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006877
Hanno Becker8927c832019-04-03 12:52:50 +01006878 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6879 void *p_vrfy;
6880
Hanno Becker68636192019-02-05 14:36:34 +00006881 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6882 return( 0 );
6883
Hanno Becker8927c832019-04-03 12:52:50 +01006884 if( ssl->f_vrfy != NULL )
6885 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006886 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006887 f_vrfy = ssl->f_vrfy;
6888 p_vrfy = ssl->p_vrfy;
6889 }
6890 else
6891 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006893 f_vrfy = ssl->conf->f_vrfy;
6894 p_vrfy = ssl->conf->p_vrfy;
6895 }
6896
Hanno Becker68636192019-02-05 14:36:34 +00006897 /*
6898 * Main check: verify certificate
6899 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006900#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6901 if( ssl->conf->f_ca_cb != NULL )
6902 {
6903 ((void) rs_ctx);
6904 have_ca_chain = 1;
6905
6906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006907 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006908 chain,
6909 ssl->conf->f_ca_cb,
6910 ssl->conf->p_ca_cb,
6911 ssl->conf->cert_profile,
6912 ssl->hostname,
6913 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006914 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006915 }
6916 else
6917#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6918 {
6919 mbedtls_x509_crt *ca_chain;
6920 mbedtls_x509_crl *ca_crl;
6921
6922#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6923 if( ssl->handshake->sni_ca_chain != NULL )
6924 {
6925 ca_chain = ssl->handshake->sni_ca_chain;
6926 ca_crl = ssl->handshake->sni_ca_crl;
6927 }
6928 else
6929#endif
6930 {
6931 ca_chain = ssl->conf->ca_chain;
6932 ca_crl = ssl->conf->ca_crl;
6933 }
6934
6935 if( ca_chain != NULL )
6936 have_ca_chain = 1;
6937
6938 ret = mbedtls_x509_crt_verify_restartable(
6939 chain,
6940 ca_chain, ca_crl,
6941 ssl->conf->cert_profile,
6942 ssl->hostname,
6943 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006944 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006945 }
Hanno Becker68636192019-02-05 14:36:34 +00006946
6947 if( ret != 0 )
6948 {
6949 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6950 }
6951
6952#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6953 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6954 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6955#endif
6956
6957 /*
6958 * Secondary checks: always done, but change 'ret' only if it was 0
6959 */
6960
6961#if defined(MBEDTLS_ECP_C)
6962 {
6963 const mbedtls_pk_context *pk = &chain->pk;
6964
6965 /* If certificate uses an EC key, make sure the curve is OK */
6966 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6967 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6968 {
6969 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6970
6971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6972 if( ret == 0 )
6973 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6974 }
6975 }
6976#endif /* MBEDTLS_ECP_C */
6977
6978 if( mbedtls_ssl_check_cert_usage( chain,
6979 ciphersuite_info,
6980 ! ssl->conf->endpoint,
6981 &ssl->session_negotiate->verify_result ) != 0 )
6982 {
6983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6984 if( ret == 0 )
6985 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6986 }
6987
6988 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6989 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6990 * with details encoded in the verification flags. All other kinds
6991 * of error codes, including those from the user provided f_vrfy
6992 * functions, are treated as fatal and lead to a failure of
6993 * ssl_parse_certificate even if verification was optional. */
6994 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6995 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6996 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6997 {
6998 ret = 0;
6999 }
7000
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007001 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007002 {
7003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7004 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7005 }
7006
7007 if( ret != 0 )
7008 {
7009 uint8_t alert;
7010
7011 /* The certificate may have been rejected for several reasons.
7012 Pick one and send the corresponding alert. Which alert to send
7013 may be a subject of debate in some cases. */
7014 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7015 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7016 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7017 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7018 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7019 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7020 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7021 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7022 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7023 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7024 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7025 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7026 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7027 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7028 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7029 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7030 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7031 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7032 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7033 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7034 else
7035 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7036 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7037 alert );
7038 }
7039
7040#if defined(MBEDTLS_DEBUG_C)
7041 if( ssl->session_negotiate->verify_result != 0 )
7042 {
7043 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7044 ssl->session_negotiate->verify_result ) );
7045 }
7046 else
7047 {
7048 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7049 }
7050#endif /* MBEDTLS_DEBUG_C */
7051
7052 return( ret );
7053}
7054
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007055#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7056static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7057 unsigned char *start, size_t len )
7058{
7059 int ret;
7060 /* Remember digest of the peer's end-CRT. */
7061 ssl->session_negotiate->peer_cert_digest =
7062 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7063 if( ssl->session_negotiate->peer_cert_digest == NULL )
7064 {
7065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7066 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7067 mbedtls_ssl_send_alert_message( ssl,
7068 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7069 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7070
7071 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7072 }
7073
7074 ret = mbedtls_md( mbedtls_md_info_from_type(
7075 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7076 start, len,
7077 ssl->session_negotiate->peer_cert_digest );
7078
7079 ssl->session_negotiate->peer_cert_digest_type =
7080 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7081 ssl->session_negotiate->peer_cert_digest_len =
7082 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7083
7084 return( ret );
7085}
7086
7087static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7088 unsigned char *start, size_t len )
7089{
7090 unsigned char *end = start + len;
7091 int ret;
7092
7093 /* Make a copy of the peer's raw public key. */
7094 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7095 ret = mbedtls_pk_parse_subpubkey( &start, end,
7096 &ssl->handshake->peer_pubkey );
7097 if( ret != 0 )
7098 {
7099 /* We should have parsed the public key before. */
7100 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7101 }
7102
7103 return( 0 );
7104}
7105#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7106
Hanno Becker68636192019-02-05 14:36:34 +00007107int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7108{
7109 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007110 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007111#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7112 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7113 ? ssl->handshake->sni_authmode
7114 : ssl->conf->authmode;
7115#else
7116 const int authmode = ssl->conf->authmode;
7117#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007118 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007119 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007120
7121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7122
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007123 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7124 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007125 {
7126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007127 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007128 }
7129
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007130#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7131 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007132 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007133 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007134 chain = ssl->handshake->ecrs_peer_cert;
7135 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007136 goto crt_verify;
7137 }
7138#endif
7139
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007140 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007141 {
7142 /* mbedtls_ssl_read_record may have sent an alert already. We
7143 let it decide whether to alert. */
7144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007145 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007146 }
7147
Hanno Becker4a55f632019-02-05 12:49:06 +00007148#if defined(MBEDTLS_SSL_SRV_C)
7149 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7150 {
7151 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007152
7153 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007154 ret = 0;
7155 else
7156 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007157
Hanno Becker6bdfab22019-02-05 13:11:17 +00007158 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007159 }
7160#endif /* MBEDTLS_SSL_SRV_C */
7161
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007162 /* Clear existing peer CRT structure in case we tried to
7163 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007164 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007165
7166 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7167 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007168 {
7169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7170 sizeof( mbedtls_x509_crt ) ) );
7171 mbedtls_ssl_send_alert_message( ssl,
7172 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7173 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007174
Hanno Becker3dad3112019-02-05 17:19:52 +00007175 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7176 goto exit;
7177 }
7178 mbedtls_x509_crt_init( chain );
7179
7180 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007181 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007182 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007183
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007184#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7185 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007186 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007187
7188crt_verify:
7189 if( ssl->handshake->ecrs_enabled)
7190 rs_ctx = &ssl->handshake->ecrs_ctx;
7191#endif
7192
Hanno Becker68636192019-02-05 14:36:34 +00007193 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007194 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007195 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007196 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007197
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007198#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007199 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007200 unsigned char *crt_start, *pk_start;
7201 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007202
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007203 /* We parse the CRT chain without copying, so
7204 * these pointers point into the input buffer,
7205 * and are hence still valid after freeing the
7206 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007207
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007208 crt_start = chain->raw.p;
7209 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007210
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007211 pk_start = chain->pk_raw.p;
7212 pk_len = chain->pk_raw.len;
7213
7214 /* Free the CRT structures before computing
7215 * digest and copying the peer's public key. */
7216 mbedtls_x509_crt_free( chain );
7217 mbedtls_free( chain );
7218 chain = NULL;
7219
7220 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007221 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007222 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007223
7224 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7225 if( ret != 0 )
7226 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007227 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007228#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7229 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007230 ssl->session_negotiate->peer_cert = chain;
7231 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007232#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007235
Hanno Becker6bdfab22019-02-05 13:11:17 +00007236exit:
7237
Hanno Becker3dad3112019-02-05 17:19:52 +00007238 if( ret == 0 )
7239 ssl->state++;
7240
7241#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7242 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7243 {
7244 ssl->handshake->ecrs_peer_cert = chain;
7245 chain = NULL;
7246 }
7247#endif
7248
7249 if( chain != NULL )
7250 {
7251 mbedtls_x509_crt_free( chain );
7252 mbedtls_free( chain );
7253 }
7254
Paul Bakker5121ce52009-01-03 21:22:43 +00007255 return( ret );
7256}
Hanno Becker21489932019-02-05 13:20:55 +00007257#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007260{
7261 int ret;
7262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007266 ssl->out_msglen = 1;
7267 ssl->out_msg[0] = 1;
7268
Paul Bakker5121ce52009-01-03 21:22:43 +00007269 ssl->state++;
7270
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007271 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007272 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007274 return( ret );
7275 }
7276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007278
7279 return( 0 );
7280}
7281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007283{
7284 int ret;
7285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007287
Hanno Becker327c93b2018-08-15 13:56:18 +01007288 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007291 return( ret );
7292 }
7293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007297 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7298 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007300 }
7301
Hanno Beckere678eaa2018-08-21 14:57:46 +01007302 /* CCS records are only accepted if they have length 1 and content '1',
7303 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007304
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007305 /*
7306 * Switch to our negotiated transform and session parameters for inbound
7307 * data.
7308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007310 ssl->transform_in = ssl->transform_negotiate;
7311 ssl->session_in = ssl->session_negotiate;
7312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007314 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007317 ssl_dtls_replay_reset( ssl );
7318#endif
7319
7320 /* Increment epoch */
7321 if( ++ssl->in_epoch == 0 )
7322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007324 /* This is highly unlikely to happen for legitimate reasons, so
7325 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007327 }
7328 }
7329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007331 memset( ssl->in_ctr, 0, 8 );
7332
Hanno Becker79594fd2019-05-08 09:38:41 +01007333 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7336 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007341 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7342 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007344 }
7345 }
7346#endif
7347
Paul Bakker5121ce52009-01-03 21:22:43 +00007348 ssl->state++;
7349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007351
7352 return( 0 );
7353}
7354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7356 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007357{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007358 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7361 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7362 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007363 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007364 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007365#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7367#if defined(MBEDTLS_SHA512_C)
7368 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007369 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7370 else
7371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372#if defined(MBEDTLS_SHA256_C)
7373 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007374 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007375 else
7376#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007380 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007381 }
Paul Bakker380da532012-04-18 16:10:25 +00007382}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7387 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007388 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7389 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7392#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007393#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007394 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007395 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7396#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007397 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007398#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007399#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007401#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007402 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007403 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007404#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007405 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007406#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007407#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007409}
7410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007412 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007413{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7415 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007416 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7417 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7420#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007421#if defined(MBEDTLS_USE_PSA_CRYPTO)
7422 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7423#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007424 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007425#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007426#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007428#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007429 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007430#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007431 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007432#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007433#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007435}
7436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7438 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7439static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007440 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007441{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007442 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7443 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007444}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007445#endif
Paul Bakker380da532012-04-18 16:10:25 +00007446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7448#if defined(MBEDTLS_SHA256_C)
7449static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007450 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007451{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007452#if defined(MBEDTLS_USE_PSA_CRYPTO)
7453 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7454#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007455 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007456#endif
Paul Bakker380da532012-04-18 16:10:25 +00007457}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007458#endif
Paul Bakker380da532012-04-18 16:10:25 +00007459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460#if defined(MBEDTLS_SHA512_C)
7461static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007462 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007463{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007464#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007465 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007466#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007467 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007468#endif
Paul Bakker380da532012-04-18 16:10:25 +00007469}
Paul Bakker769075d2012-11-24 11:26:46 +01007470#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007474static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007476{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007477 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007478 mbedtls_md5_context md5;
7479 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007480
Paul Bakker5121ce52009-01-03 21:22:43 +00007481 unsigned char padbuf[48];
7482 unsigned char md5sum[16];
7483 unsigned char sha1sum[20];
7484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007485 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007486 if( !session )
7487 session = ssl->session;
7488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007490
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007491 mbedtls_md5_init( &md5 );
7492 mbedtls_sha1_init( &sha1 );
7493
7494 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7495 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007496
7497 /*
7498 * SSLv3:
7499 * hash =
7500 * MD5( master + pad2 +
7501 * MD5( handshake + sender + master + pad1 ) )
7502 * + SHA1( master + pad2 +
7503 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007504 */
7505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007507 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7508 md5.state, sizeof( md5.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#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007512 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7513 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007514#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007517 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007518
Paul Bakker1ef83d62012-04-11 12:09:53 +00007519 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007520
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007521 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7522 mbedtls_md5_update_ret( &md5, session->master, 48 );
7523 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7524 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007525
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007526 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7527 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7528 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7529 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007530
Paul Bakker1ef83d62012-04-11 12:09:53 +00007531 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007532
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007533 mbedtls_md5_starts_ret( &md5 );
7534 mbedtls_md5_update_ret( &md5, session->master, 48 );
7535 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7536 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7537 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007538
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007539 mbedtls_sha1_starts_ret( &sha1 );
7540 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7541 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7542 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7543 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007546
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007547 mbedtls_md5_free( &md5 );
7548 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007549
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007550 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7551 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7552 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007555}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007559static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007561{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007562 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007563 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007564 mbedtls_md5_context md5;
7565 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007566 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007569 if( !session )
7570 session = ssl->session;
7571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007573
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007574 mbedtls_md5_init( &md5 );
7575 mbedtls_sha1_init( &sha1 );
7576
7577 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7578 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007579
Paul Bakker1ef83d62012-04-11 12:09:53 +00007580 /*
7581 * TLSv1:
7582 * hash = PRF( master, finished_label,
7583 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7584 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007587 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7588 md5.state, sizeof( md5.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#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007592 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7593 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007594#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007597 ? "client finished"
7598 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007599
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007600 mbedtls_md5_finish_ret( &md5, padbuf );
7601 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007602
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007603 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007604 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007607
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007608 mbedtls_md5_free( &md5 );
7609 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007610
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007611 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007614}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7618#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007619static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007620 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007621{
7622 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007623 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007624 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007625#if defined(MBEDTLS_USE_PSA_CRYPTO)
7626 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007627 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007628 psa_status_t status;
7629#else
7630 mbedtls_sha256_context sha256;
7631#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007634 if( !session )
7635 session = ssl->session;
7636
Andrzej Kurekeb342242019-01-29 09:14:33 -05007637 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7638 ? "client finished"
7639 : "server finished";
7640
7641#if defined(MBEDTLS_USE_PSA_CRYPTO)
7642 sha256_psa = psa_hash_operation_init();
7643
7644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7645
7646 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7647 if( status != PSA_SUCCESS )
7648 {
7649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7650 return;
7651 }
7652
7653 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7654 if( status != PSA_SUCCESS )
7655 {
7656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7657 return;
7658 }
7659 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7660#else
7661
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007662 mbedtls_sha256_init( &sha256 );
7663
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007665
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007666 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007667
7668 /*
7669 * TLSv1.2:
7670 * hash = PRF( master, finished_label,
7671 * Hash( handshake ) )[0.11]
7672 */
7673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674#if !defined(MBEDTLS_SHA256_ALT)
7675 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007676 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007677#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007678
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007679 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007680 mbedtls_sha256_free( &sha256 );
7681#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007682
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007683 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007684 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007686 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007687
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007688 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007691}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007695static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007697{
7698 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007699 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007700 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007701#if defined(MBEDTLS_USE_PSA_CRYPTO)
7702 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007703 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007704 psa_status_t status;
7705#else
7706 mbedtls_sha512_context sha512;
7707#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007710 if( !session )
7711 session = ssl->session;
7712
Andrzej Kurekeb342242019-01-29 09:14:33 -05007713 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7714 ? "client finished"
7715 : "server finished";
7716
7717#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007718 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007719
7720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7721
Andrzej Kurek972fba52019-01-30 03:29:12 -05007722 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007723 if( status != PSA_SUCCESS )
7724 {
7725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7726 return;
7727 }
7728
Andrzej Kurek972fba52019-01-30 03:29:12 -05007729 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007730 if( status != PSA_SUCCESS )
7731 {
7732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7733 return;
7734 }
7735 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7736#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007737 mbedtls_sha512_init( &sha512 );
7738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007740
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007741 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007742
7743 /*
7744 * TLSv1.2:
7745 * hash = PRF( master, finished_label,
7746 * Hash( handshake ) )[0.11]
7747 */
7748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007750 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7751 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007752#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007753
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007754 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007755 mbedtls_sha512_free( &sha512 );
7756#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007757
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007758 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007759 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007762
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007763 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007766}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767#endif /* MBEDTLS_SHA512_C */
7768#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007771{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007773
7774 /*
7775 * Free our handshake params
7776 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007777 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007779 ssl->handshake = NULL;
7780
7781 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007782 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007783 */
7784 if( ssl->transform )
7785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786 mbedtls_ssl_transform_free( ssl->transform );
7787 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007788 }
7789 ssl->transform = ssl->transform_negotiate;
7790 ssl->transform_negotiate = NULL;
7791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007793}
7794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007796{
7797 int resume = ssl->handshake->resume;
7798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801#if defined(MBEDTLS_SSL_RENEGOTIATION)
7802 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007805 ssl->renego_records_seen = 0;
7806 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007807#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007808
7809 /*
7810 * Free the previous session and switch in the current one
7811 */
Paul Bakker0a597072012-09-25 21:55:46 +00007812 if( ssl->session )
7813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007814#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007815 /* RFC 7366 3.1: keep the EtM state */
7816 ssl->session_negotiate->encrypt_then_mac =
7817 ssl->session->encrypt_then_mac;
7818#endif
7819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820 mbedtls_ssl_session_free( ssl->session );
7821 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007822 }
7823 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007824 ssl->session_negotiate = NULL;
7825
Paul Bakker0a597072012-09-25 21:55:46 +00007826 /*
7827 * Add cache entry
7828 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007829 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007830 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007831 resume == 0 )
7832 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007833 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007835 }
Paul Bakker0a597072012-09-25 21:55:46 +00007836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007838 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007839 ssl->handshake->flight != NULL )
7840 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007841 /* Cancel handshake timer */
7842 ssl_set_timer( ssl, 0 );
7843
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007844 /* Keep last flight around in case we need to resend it:
7845 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007847 }
7848 else
7849#endif
7850 ssl_handshake_wrapup_free_hs_transform( ssl );
7851
Paul Bakker48916f92012-09-16 19:57:18 +00007852 ssl->state++;
7853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007855}
7856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007858{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007859 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007862
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007863 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007864
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007865 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007866
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007867 /*
7868 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7869 * may define some other value. Currently (early 2016), no defined
7870 * ciphersuite does this (and this is unlikely to change as activity has
7871 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007876 ssl->verify_data_len = hash_len;
7877 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007878#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007879
Paul Bakker5121ce52009-01-03 21:22:43 +00007880 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7882 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007883
7884 /*
7885 * In case of session resuming, invert the client and server
7886 * ChangeCipherSpec messages order.
7887 */
Paul Bakker0a597072012-09-25 21:55:46 +00007888 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007891 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007893#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007895 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007897#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007898 }
7899 else
7900 ssl->state++;
7901
Paul Bakker48916f92012-09-16 19:57:18 +00007902 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007903 * Switch to our negotiated transform and session parameters for outbound
7904 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007909 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007910 {
7911 unsigned char i;
7912
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007913 /* Remember current epoch settings for resending */
7914 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007915 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007916
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007917 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007918 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007919
7920 /* Increment epoch */
7921 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007922 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007923 break;
7924
7925 /* The loop goes to its end iff the counter is wrapping */
7926 if( i == 0 )
7927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7929 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007930 }
7931 }
7932 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007934 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007935
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007936 ssl->transform_out = ssl->transform_negotiate;
7937 ssl->session_out = ssl->session_negotiate;
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7940 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7945 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007946 }
7947 }
7948#endif
7949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007951 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007953#endif
7954
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007955 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007956 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007957 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007958 return( ret );
7959 }
7960
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007961#if defined(MBEDTLS_SSL_PROTO_DTLS)
7962 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7963 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7964 {
7965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7966 return( ret );
7967 }
7968#endif
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007971
7972 return( 0 );
7973}
7974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007976#define SSL_MAX_HASH_LEN 36
7977#else
7978#define SSL_MAX_HASH_LEN 12
7979#endif
7980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007982{
Paul Bakker23986e52011-04-24 08:57:21 +00007983 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007984 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007985 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007988
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007989 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007990
Hanno Becker327c93b2018-08-15 13:56:18 +01007991 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007994 return( ret );
7995 }
7996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008000 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8001 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008003 }
8004
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008005 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006#if defined(MBEDTLS_SSL_PROTO_SSL3)
8007 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008008 hash_len = 36;
8009 else
8010#endif
8011 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8014 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008017 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8018 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008020 }
8021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008023 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008026 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8027 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008029 }
8030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008031#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008032 ssl->verify_data_len = hash_len;
8033 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008034#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008035
Paul Bakker0a597072012-09-25 21:55:46 +00008036 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008039 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008040 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008042#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008043 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008044 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008045#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008046 }
8047 else
8048 ssl->state++;
8049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008051 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008053#endif
8054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008056
8057 return( 0 );
8058}
8059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8065 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8066 mbedtls_md5_init( &handshake->fin_md5 );
8067 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008068 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8069 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8072#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008073#if defined(MBEDTLS_USE_PSA_CRYPTO)
8074 handshake->fin_sha256_psa = psa_hash_operation_init();
8075 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8076#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008078 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008079#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008080#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008082#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008083 handshake->fin_sha384_psa = psa_hash_operation_init();
8084 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008085#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008086 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008087 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008088#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008089#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008091
8092 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008093
8094#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8095 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8096 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8097#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099#if defined(MBEDTLS_DHM_C)
8100 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008101#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008102#if defined(MBEDTLS_ECDH_C)
8103 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008104#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008105#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008106 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008107#if defined(MBEDTLS_SSL_CLI_C)
8108 handshake->ecjpake_cache = NULL;
8109 handshake->ecjpake_cache_len = 0;
8110#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008111#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008112
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008113#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008114 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008115#endif
8116
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008117#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8118 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8119#endif
Hanno Becker75173122019-02-06 16:18:31 +00008120
8121#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8122 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8123 mbedtls_pk_init( &handshake->peer_pubkey );
8124#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008125}
8126
Hanno Beckera18d1322018-01-03 14:27:32 +00008127void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8132 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008133
Hanno Beckerd56ed242018-01-03 15:32:51 +00008134#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135 mbedtls_md_init( &transform->md_ctx_enc );
8136 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008137#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008138}
8139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008143}
8144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008146{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008147 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008148 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008150 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008152 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008153 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008154
8155 /*
8156 * Either the pointers are now NULL or cleared properly and can be freed.
8157 * Now allocate missing structures.
8158 */
8159 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008160 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008161 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008162 }
Paul Bakker48916f92012-09-16 19:57:18 +00008163
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008164 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008165 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008166 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008167 }
Paul Bakker48916f92012-09-16 19:57:18 +00008168
Paul Bakker82788fb2014-10-20 13:59:19 +02008169 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008170 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008171 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008172 }
Paul Bakker48916f92012-09-16 19:57:18 +00008173
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008174 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008175 if( ssl->handshake == NULL ||
8176 ssl->transform_negotiate == NULL ||
8177 ssl->session_negotiate == NULL )
8178 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181 mbedtls_free( ssl->handshake );
8182 mbedtls_free( ssl->transform_negotiate );
8183 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008184
8185 ssl->handshake = NULL;
8186 ssl->transform_negotiate = NULL;
8187 ssl->session_negotiate = NULL;
8188
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008189 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008190 }
8191
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008192 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008194 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008195 ssl_handshake_params_init( ssl->handshake );
8196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008198 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8199 {
8200 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008201
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008202 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8203 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8204 else
8205 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008206
8207 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008208 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008209#endif
8210
Paul Bakker48916f92012-09-16 19:57:18 +00008211 return( 0 );
8212}
8213
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008214#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008215/* Dummy cookie callbacks for defaults */
8216static int ssl_cookie_write_dummy( void *ctx,
8217 unsigned char **p, unsigned char *end,
8218 const unsigned char *cli_id, size_t cli_id_len )
8219{
8220 ((void) ctx);
8221 ((void) p);
8222 ((void) end);
8223 ((void) cli_id);
8224 ((void) cli_id_len);
8225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008227}
8228
8229static int ssl_cookie_check_dummy( void *ctx,
8230 const unsigned char *cookie, size_t cookie_len,
8231 const unsigned char *cli_id, size_t cli_id_len )
8232{
8233 ((void) ctx);
8234 ((void) cookie);
8235 ((void) cookie_len);
8236 ((void) cli_id);
8237 ((void) cli_id_len);
8238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008240}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008241#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008242
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008243/* Once ssl->out_hdr as the address of the beginning of the
8244 * next outgoing record is set, deduce the other pointers.
8245 *
8246 * Note: For TLS, we save the implicit record sequence number
8247 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8248 * and the caller has to make sure there's space for this.
8249 */
8250
8251static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8252 mbedtls_ssl_transform *transform )
8253{
8254#if defined(MBEDTLS_SSL_PROTO_DTLS)
8255 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8256 {
8257 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008258#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008259 ssl->out_cid = ssl->out_ctr + 8;
8260 ssl->out_len = ssl->out_cid;
8261 if( transform != NULL )
8262 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008263#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008264 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008265#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008266 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008267 }
8268 else
8269#endif
8270 {
8271 ssl->out_ctr = ssl->out_hdr - 8;
8272 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008273#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008274 ssl->out_cid = ssl->out_len;
8275#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008276 ssl->out_iv = ssl->out_hdr + 5;
8277 }
8278
8279 /* Adjust out_msg to make space for explicit IV, if used. */
8280 if( transform != NULL &&
8281 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8282 {
8283 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8284 }
8285 else
8286 ssl->out_msg = ssl->out_iv;
8287}
8288
8289/* Once ssl->in_hdr as the address of the beginning of the
8290 * next incoming record is set, deduce the other pointers.
8291 *
8292 * Note: For TLS, we save the implicit record sequence number
8293 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8294 * and the caller has to make sure there's space for this.
8295 */
8296
Hanno Becker79594fd2019-05-08 09:38:41 +01008297static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008298{
Hanno Becker79594fd2019-05-08 09:38:41 +01008299 /* This function sets the pointers to match the case
8300 * of unprotected TLS/DTLS records, with both ssl->in_iv
8301 * and ssl->in_msg pointing to the beginning of the record
8302 * content.
8303 *
8304 * When decrypting a protected record, ssl->in_msg
8305 * will be shifted to point to the beginning of the
8306 * record plaintext.
8307 */
8308
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008309#if defined(MBEDTLS_SSL_PROTO_DTLS)
8310 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8311 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008312 /* This sets the header pointers to match records
8313 * without CID. When we receive a record containing
8314 * a CID, the fields are shifted accordingly in
8315 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008316 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008317#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008318 ssl->in_cid = ssl->in_ctr + 8;
8319 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008320#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008321 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008322#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008323 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008324 }
8325 else
8326#endif
8327 {
8328 ssl->in_ctr = ssl->in_hdr - 8;
8329 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008330#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008331 ssl->in_cid = ssl->in_len;
8332#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008333 ssl->in_iv = ssl->in_hdr + 5;
8334 }
8335
Hanno Becker79594fd2019-05-08 09:38:41 +01008336 /* This will be adjusted at record decryption time. */
8337 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008338}
8339
Paul Bakker5121ce52009-01-03 21:22:43 +00008340/*
8341 * Initialize an SSL context
8342 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008343void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8344{
8345 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8346}
8347
8348/*
8349 * Setup an SSL context
8350 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008351
8352static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8353{
8354 /* Set the incoming and outgoing record pointers. */
8355#if defined(MBEDTLS_SSL_PROTO_DTLS)
8356 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8357 {
8358 ssl->out_hdr = ssl->out_buf;
8359 ssl->in_hdr = ssl->in_buf;
8360 }
8361 else
8362#endif /* MBEDTLS_SSL_PROTO_DTLS */
8363 {
8364 ssl->out_hdr = ssl->out_buf + 8;
8365 ssl->in_hdr = ssl->in_buf + 8;
8366 }
8367
8368 /* Derive other internal pointers. */
8369 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008370 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008371}
8372
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008373int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008374 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008375{
Paul Bakker48916f92012-09-16 19:57:18 +00008376 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008377
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008378 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008379
8380 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008381 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008382 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008383
8384 /* Set to NULL in case of an error condition */
8385 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008386
Angus Grattond8213d02016-05-25 20:56:48 +10008387 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8388 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008389 {
Angus Grattond8213d02016-05-25 20:56:48 +10008390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008391 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008392 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008393 }
8394
8395 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8396 if( ssl->out_buf == NULL )
8397 {
8398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008399 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008400 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008401 }
8402
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008403 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008404
Paul Bakker48916f92012-09-16 19:57:18 +00008405 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008406 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008407
8408 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008409
8410error:
8411 mbedtls_free( ssl->in_buf );
8412 mbedtls_free( ssl->out_buf );
8413
8414 ssl->conf = NULL;
8415
8416 ssl->in_buf = NULL;
8417 ssl->out_buf = NULL;
8418
8419 ssl->in_hdr = NULL;
8420 ssl->in_ctr = NULL;
8421 ssl->in_len = NULL;
8422 ssl->in_iv = NULL;
8423 ssl->in_msg = NULL;
8424
8425 ssl->out_hdr = NULL;
8426 ssl->out_ctr = NULL;
8427 ssl->out_len = NULL;
8428 ssl->out_iv = NULL;
8429 ssl->out_msg = NULL;
8430
k-stachowiak9f7798e2018-07-31 16:52:32 +02008431 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008432}
8433
8434/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008435 * Reset an initialized and used SSL context for re-use while retaining
8436 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008437 *
8438 * If partial is non-zero, keep data in the input buffer and client ID.
8439 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008440 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008441static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008442{
Paul Bakker48916f92012-09-16 19:57:18 +00008443 int ret;
8444
Hanno Becker7e772132018-08-10 12:38:21 +01008445#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8446 !defined(MBEDTLS_SSL_SRV_C)
8447 ((void) partial);
8448#endif
8449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008450 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008451
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008452 /* Cancel any possibly running timer */
8453 ssl_set_timer( ssl, 0 );
8454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455#if defined(MBEDTLS_SSL_RENEGOTIATION)
8456 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008457 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008458
8459 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8461 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008462#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008464
Paul Bakker7eb013f2011-10-06 12:37:39 +00008465 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008466 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008467
8468 ssl->in_msgtype = 0;
8469 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008470#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008471 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008472 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008473#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008474#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008475 ssl_dtls_replay_reset( ssl );
8476#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008477
8478 ssl->in_hslen = 0;
8479 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008480
8481 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008482
8483 ssl->out_msgtype = 0;
8484 ssl->out_msglen = 0;
8485 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8487 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008488 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008489#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008490
Hanno Becker19859472018-08-06 09:40:20 +01008491 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8492
Paul Bakker48916f92012-09-16 19:57:18 +00008493 ssl->transform_in = NULL;
8494 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008495
Hanno Becker78640902018-08-13 16:35:15 +01008496 ssl->session_in = NULL;
8497 ssl->session_out = NULL;
8498
Angus Grattond8213d02016-05-25 20:56:48 +10008499 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008500
8501#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008502 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008503#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8504 {
8505 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008506 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008507 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8510 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8513 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8516 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008517 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008518 }
8519#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008520
Paul Bakker48916f92012-09-16 19:57:18 +00008521 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523 mbedtls_ssl_transform_free( ssl->transform );
8524 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008525 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008526 }
Paul Bakker48916f92012-09-16 19:57:18 +00008527
Paul Bakkerc0463502013-02-14 11:19:38 +01008528 if( ssl->session )
8529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530 mbedtls_ssl_session_free( ssl->session );
8531 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008532 ssl->session = NULL;
8533 }
8534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008536 ssl->alpn_chosen = NULL;
8537#endif
8538
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008539#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008540#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008541 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008542#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008543 {
8544 mbedtls_free( ssl->cli_id );
8545 ssl->cli_id = NULL;
8546 ssl->cli_id_len = 0;
8547 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008548#endif
8549
Paul Bakker48916f92012-09-16 19:57:18 +00008550 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8551 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008552
8553 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008554}
8555
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008556/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008557 * Reset an initialized and used SSL context for re-use while retaining
8558 * all application-set variables, function pointers and data.
8559 */
8560int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8561{
8562 return( ssl_session_reset_int( ssl, 0 ) );
8563}
8564
8565/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008566 * SSL set accessors
8567 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008568void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008569{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008570 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008571}
8572
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008573void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008574{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008575 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008576}
8577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008579void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008580{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008581 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008582}
8583#endif
8584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008586void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008587{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008588 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008589}
8590#endif
8591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008592#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008593
Hanno Becker1841b0a2018-08-24 11:13:57 +01008594void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8595 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008596{
8597 ssl->disable_datagram_packing = !allow_packing;
8598}
8599
8600void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8601 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008602{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008603 conf->hs_timeout_min = min;
8604 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008605}
8606#endif
8607
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008608void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008609{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008610 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008611}
8612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008614void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008615 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008616 void *p_vrfy )
8617{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008618 conf->f_vrfy = f_vrfy;
8619 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008621#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008622
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008623void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008624 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008625 void *p_rng )
8626{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008627 conf->f_rng = f_rng;
8628 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008629}
8630
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008631void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008632 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008633 void *p_dbg )
8634{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008635 conf->f_dbg = f_dbg;
8636 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008637}
8638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008640 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008641 mbedtls_ssl_send_t *f_send,
8642 mbedtls_ssl_recv_t *f_recv,
8643 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008644{
8645 ssl->p_bio = p_bio;
8646 ssl->f_send = f_send;
8647 ssl->f_recv = f_recv;
8648 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008649}
8650
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008651#if defined(MBEDTLS_SSL_PROTO_DTLS)
8652void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8653{
8654 ssl->mtu = mtu;
8655}
8656#endif
8657
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008658void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008659{
8660 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008661}
8662
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008663void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8664 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008665 mbedtls_ssl_set_timer_t *f_set_timer,
8666 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008667{
8668 ssl->p_timer = p_timer;
8669 ssl->f_set_timer = f_set_timer;
8670 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008671
8672 /* Make sure we start with no timer running */
8673 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008674}
8675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008677void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008678 void *p_cache,
8679 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8680 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008681{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008682 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008683 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008684 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688#if defined(MBEDTLS_SSL_CLI_C)
8689int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008690{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008691 int ret;
8692
8693 if( ssl == NULL ||
8694 session == NULL ||
8695 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008696 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008698 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008699 }
8700
Hanno Becker52055ae2019-02-06 14:30:46 +00008701 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8702 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008703 return( ret );
8704
Paul Bakker0a597072012-09-25 21:55:46 +00008705 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008706
8707 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008709#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008710
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008711void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008712 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008713{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008714 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8715 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8716 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8717 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008718}
8719
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008720void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008721 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008722 int major, int minor )
8723{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008725 return;
8726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008727 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008728 return;
8729
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008730 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008731}
8732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008734void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008735 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008736{
8737 conf->cert_profile = profile;
8738}
8739
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008740/* Append a new keycert entry to a (possibly empty) list */
8741static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8742 mbedtls_x509_crt *cert,
8743 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008744{
niisato8ee24222018-06-25 19:05:48 +09008745 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008746
niisato8ee24222018-06-25 19:05:48 +09008747 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8748 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008749 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008750
niisato8ee24222018-06-25 19:05:48 +09008751 new_cert->cert = cert;
8752 new_cert->key = key;
8753 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008754
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008755 /* Update head is the list was null, else add to the end */
8756 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008757 {
niisato8ee24222018-06-25 19:05:48 +09008758 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008759 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008760 else
8761 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008762 mbedtls_ssl_key_cert *cur = *head;
8763 while( cur->next != NULL )
8764 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008765 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008766 }
8767
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008768 return( 0 );
8769}
8770
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008771int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008772 mbedtls_x509_crt *own_cert,
8773 mbedtls_pk_context *pk_key )
8774{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008775 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008776}
8777
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008778void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008779 mbedtls_x509_crt *ca_chain,
8780 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008781{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008782 conf->ca_chain = ca_chain;
8783 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008784
8785#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8786 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8787 * cannot be used together. */
8788 conf->f_ca_cb = NULL;
8789 conf->p_ca_cb = NULL;
8790#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008791}
Hanno Becker5adaad92019-03-27 16:54:37 +00008792
8793#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8794void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008795 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008796 void *p_ca_cb )
8797{
8798 conf->f_ca_cb = f_ca_cb;
8799 conf->p_ca_cb = p_ca_cb;
8800
8801 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8802 * cannot be used together. */
8803 conf->ca_chain = NULL;
8804 conf->ca_crl = NULL;
8805}
8806#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008808
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008809#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8810int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8811 mbedtls_x509_crt *own_cert,
8812 mbedtls_pk_context *pk_key )
8813{
8814 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8815 own_cert, pk_key ) );
8816}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008817
8818void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8819 mbedtls_x509_crt *ca_chain,
8820 mbedtls_x509_crl *ca_crl )
8821{
8822 ssl->handshake->sni_ca_chain = ca_chain;
8823 ssl->handshake->sni_ca_crl = ca_crl;
8824}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008825
8826void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8827 int authmode )
8828{
8829 ssl->handshake->sni_authmode = authmode;
8830}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008831#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8832
Hanno Becker8927c832019-04-03 12:52:50 +01008833#if defined(MBEDTLS_X509_CRT_PARSE_C)
8834void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8835 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8836 void *p_vrfy )
8837{
8838 ssl->f_vrfy = f_vrfy;
8839 ssl->p_vrfy = p_vrfy;
8840}
8841#endif
8842
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008843#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008844/*
8845 * Set EC J-PAKE password for current handshake
8846 */
8847int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8848 const unsigned char *pw,
8849 size_t pw_len )
8850{
8851 mbedtls_ecjpake_role role;
8852
Janos Follath8eb64132016-06-03 15:40:57 +01008853 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008854 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8855
8856 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8857 role = MBEDTLS_ECJPAKE_SERVER;
8858 else
8859 role = MBEDTLS_ECJPAKE_CLIENT;
8860
8861 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8862 role,
8863 MBEDTLS_MD_SHA256,
8864 MBEDTLS_ECP_DP_SECP256R1,
8865 pw, pw_len ) );
8866}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008867#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008869#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008870
8871static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8872{
8873 /* Remove reference to existing PSK, if any. */
8874#if defined(MBEDTLS_USE_PSA_CRYPTO)
8875 if( conf->psk_opaque != 0 )
8876 {
8877 /* The maintenance of the PSK key slot is the
8878 * user's responsibility. */
8879 conf->psk_opaque = 0;
8880 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008881 /* This and the following branch should never
8882 * be taken simultaenously as we maintain the
8883 * invariant that raw and opaque PSKs are never
8884 * configured simultaneously. As a safeguard,
8885 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008886#endif /* MBEDTLS_USE_PSA_CRYPTO */
8887 if( conf->psk != NULL )
8888 {
8889 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8890
8891 mbedtls_free( conf->psk );
8892 conf->psk = NULL;
8893 conf->psk_len = 0;
8894 }
8895
8896 /* Remove reference to PSK identity, if any. */
8897 if( conf->psk_identity != NULL )
8898 {
8899 mbedtls_free( conf->psk_identity );
8900 conf->psk_identity = NULL;
8901 conf->psk_identity_len = 0;
8902 }
8903}
8904
Hanno Becker7390c712018-11-15 13:33:04 +00008905/* This function assumes that PSK identity in the SSL config is unset.
8906 * It checks that the provided identity is well-formed and attempts
8907 * to make a copy of it in the SSL config.
8908 * On failure, the PSK identity in the config remains unset. */
8909static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8910 unsigned char const *psk_identity,
8911 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008912{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008913 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008914 if( psk_identity == NULL ||
8915 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008916 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008917 {
8918 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8919 }
8920
Hanno Becker7390c712018-11-15 13:33:04 +00008921 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8922 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008923 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008924
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008925 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008926 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008927
8928 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008929}
8930
Hanno Becker7390c712018-11-15 13:33:04 +00008931int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8932 const unsigned char *psk, size_t psk_len,
8933 const unsigned char *psk_identity, size_t psk_identity_len )
8934{
8935 int ret;
8936 /* Remove opaque/raw PSK + PSK Identity */
8937 ssl_conf_remove_psk( conf );
8938
8939 /* Check and set raw PSK */
8940 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8942 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8943 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8944 conf->psk_len = psk_len;
8945 memcpy( conf->psk, psk, conf->psk_len );
8946
8947 /* Check and set PSK Identity */
8948 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8949 if( ret != 0 )
8950 ssl_conf_remove_psk( conf );
8951
8952 return( ret );
8953}
8954
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008955static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8956{
8957#if defined(MBEDTLS_USE_PSA_CRYPTO)
8958 if( ssl->handshake->psk_opaque != 0 )
8959 {
8960 ssl->handshake->psk_opaque = 0;
8961 }
8962 else
8963#endif /* MBEDTLS_USE_PSA_CRYPTO */
8964 if( ssl->handshake->psk != NULL )
8965 {
8966 mbedtls_platform_zeroize( ssl->handshake->psk,
8967 ssl->handshake->psk_len );
8968 mbedtls_free( ssl->handshake->psk );
8969 ssl->handshake->psk_len = 0;
8970 }
8971}
8972
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008973int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8974 const unsigned char *psk, size_t psk_len )
8975{
8976 if( psk == NULL || ssl->handshake == NULL )
8977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8978
8979 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8980 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8981
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008982 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008983
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008984 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008985 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008986
8987 ssl->handshake->psk_len = psk_len;
8988 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8989
8990 return( 0 );
8991}
8992
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008993#if defined(MBEDTLS_USE_PSA_CRYPTO)
8994int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008995 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008996 const unsigned char *psk_identity,
8997 size_t psk_identity_len )
8998{
Hanno Becker7390c712018-11-15 13:33:04 +00008999 int ret;
9000 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009001 ssl_conf_remove_psk( conf );
9002
Hanno Becker7390c712018-11-15 13:33:04 +00009003 /* Check and set opaque PSK */
9004 if( psk_slot == 0 )
9005 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009006 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009007
9008 /* Check and set PSK Identity */
9009 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9010 psk_identity_len );
9011 if( ret != 0 )
9012 ssl_conf_remove_psk( conf );
9013
9014 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009015}
9016
9017int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009018 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009019{
9020 if( psk_slot == 0 || ssl->handshake == NULL )
9021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9022
9023 ssl_remove_psk( ssl );
9024 ssl->handshake->psk_opaque = psk_slot;
9025 return( 0 );
9026}
9027#endif /* MBEDTLS_USE_PSA_CRYPTO */
9028
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009029void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009030 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009031 size_t),
9032 void *p_psk )
9033{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009034 conf->f_psk = f_psk;
9035 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009036}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009038
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009039#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009040
9041#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009042int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009043{
9044 int ret;
9045
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009046 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9047 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9048 {
9049 mbedtls_mpi_free( &conf->dhm_P );
9050 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009051 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009052 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009053
9054 return( 0 );
9055}
Hanno Becker470a8c42017-10-04 15:28:46 +01009056#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009057
Hanno Beckera90658f2017-10-04 15:29:08 +01009058int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9059 const unsigned char *dhm_P, size_t P_len,
9060 const unsigned char *dhm_G, size_t G_len )
9061{
9062 int ret;
9063
9064 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9065 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9066 {
9067 mbedtls_mpi_free( &conf->dhm_P );
9068 mbedtls_mpi_free( &conf->dhm_G );
9069 return( ret );
9070 }
9071
9072 return( 0 );
9073}
Paul Bakker5121ce52009-01-03 21:22:43 +00009074
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009075int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009076{
9077 int ret;
9078
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009079 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9080 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9081 {
9082 mbedtls_mpi_free( &conf->dhm_P );
9083 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009084 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009085 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009086
9087 return( 0 );
9088}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009089#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009090
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009091#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9092/*
9093 * Set the minimum length for Diffie-Hellman parameters
9094 */
9095void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9096 unsigned int bitlen )
9097{
9098 conf->dhm_min_bitlen = bitlen;
9099}
9100#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9101
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009102#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009103/*
9104 * Set allowed/preferred hashes for handshake signatures
9105 */
9106void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9107 const int *hashes )
9108{
9109 conf->sig_hashes = hashes;
9110}
Hanno Becker947194e2017-04-07 13:25:49 +01009111#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009112
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009113#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009114/*
9115 * Set the allowed elliptic curves
9116 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009117void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009118 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009119{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009120 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009121}
Hanno Becker947194e2017-04-07 13:25:49 +01009122#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009123
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009124#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009125int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009126{
Hanno Becker947194e2017-04-07 13:25:49 +01009127 /* Initialize to suppress unnecessary compiler warning */
9128 size_t hostname_len = 0;
9129
9130 /* Check if new hostname is valid before
9131 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009132 if( hostname != NULL )
9133 {
9134 hostname_len = strlen( hostname );
9135
9136 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9137 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9138 }
9139
9140 /* Now it's clear that we will overwrite the old hostname,
9141 * so we can free it safely */
9142
9143 if( ssl->hostname != NULL )
9144 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009145 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009146 mbedtls_free( ssl->hostname );
9147 }
9148
9149 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009150
Paul Bakker5121ce52009-01-03 21:22:43 +00009151 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009152 {
9153 ssl->hostname = NULL;
9154 }
9155 else
9156 {
9157 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009158 if( ssl->hostname == NULL )
9159 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009160
Hanno Becker947194e2017-04-07 13:25:49 +01009161 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009162
Hanno Becker947194e2017-04-07 13:25:49 +01009163 ssl->hostname[hostname_len] = '\0';
9164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009165
9166 return( 0 );
9167}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009168#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009169
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009170#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009171void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009173 const unsigned char *, size_t),
9174 void *p_sni )
9175{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009176 conf->f_sni = f_sni;
9177 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009181#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009182int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009183{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009184 size_t cur_len, tot_len;
9185 const char **p;
9186
9187 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009188 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9189 * MUST NOT be truncated."
9190 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009191 */
9192 tot_len = 0;
9193 for( p = protos; *p != NULL; p++ )
9194 {
9195 cur_len = strlen( *p );
9196 tot_len += cur_len;
9197
9198 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009200 }
9201
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009202 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009203
9204 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009205}
9206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009207const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009208{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009209 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009210}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009211#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009212
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009213void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009214{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009215 conf->max_major_ver = major;
9216 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009217}
9218
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009219void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009220{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009221 conf->min_major_ver = major;
9222 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009223}
9224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009226void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009227{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009228 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009229}
9230#endif
9231
Janos Follath088ce432017-04-10 12:42:31 +01009232#if defined(MBEDTLS_SSL_SRV_C)
9233void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9234 char cert_req_ca_list )
9235{
9236 conf->cert_req_ca_list = cert_req_ca_list;
9237}
9238#endif
9239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009240#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009241void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009242{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009243 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009244}
9245#endif
9246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009247#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009248void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009249{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009250 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009251}
9252#endif
9253
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009254#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009255void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009256{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009257 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009258}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009259#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009262int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009263{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009265 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009268 }
9269
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009270 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009271
9272 return( 0 );
9273}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009276#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009277void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009278{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009279 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009283#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009284void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009285{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009286 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009287}
9288#endif
9289
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009290void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009291{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009292 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009293}
9294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009296void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009297{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009298 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009299}
9300
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009301void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009302{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009303 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009304}
9305
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009306void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009307 const unsigned char period[8] )
9308{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009309 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009310}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009314#if defined(MBEDTLS_SSL_CLI_C)
9315void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009316{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009317 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009318}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009319#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009320
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009321#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009322void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9323 mbedtls_ssl_ticket_write_t *f_ticket_write,
9324 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9325 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009326{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009327 conf->f_ticket_write = f_ticket_write;
9328 conf->f_ticket_parse = f_ticket_parse;
9329 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009330}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009331#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009333
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009334#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9335void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9336 mbedtls_ssl_export_keys_t *f_export_keys,
9337 void *p_export_keys )
9338{
9339 conf->f_export_keys = f_export_keys;
9340 conf->p_export_keys = p_export_keys;
9341}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009342
9343void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9344 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9345 void *p_export_keys )
9346{
9347 conf->f_export_keys_ext = f_export_keys_ext;
9348 conf->p_export_keys = p_export_keys;
9349}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009350#endif
9351
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009352#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009353void mbedtls_ssl_conf_async_private_cb(
9354 mbedtls_ssl_config *conf,
9355 mbedtls_ssl_async_sign_t *f_async_sign,
9356 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9357 mbedtls_ssl_async_resume_t *f_async_resume,
9358 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009359 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009360{
9361 conf->f_async_sign_start = f_async_sign;
9362 conf->f_async_decrypt_start = f_async_decrypt;
9363 conf->f_async_resume = f_async_resume;
9364 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009365 conf->p_async_config_data = async_config_data;
9366}
9367
Gilles Peskine8f97af72018-04-26 11:46:10 +02009368void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9369{
9370 return( conf->p_async_config_data );
9371}
9372
Gilles Peskine1febfef2018-04-30 11:54:39 +02009373void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009374{
9375 if( ssl->handshake == NULL )
9376 return( NULL );
9377 else
9378 return( ssl->handshake->user_async_ctx );
9379}
9380
Gilles Peskine1febfef2018-04-30 11:54:39 +02009381void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009382 void *ctx )
9383{
9384 if( ssl->handshake != NULL )
9385 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009386}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009387#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009388
Paul Bakker5121ce52009-01-03 21:22:43 +00009389/*
9390 * SSL get accessors
9391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009393{
9394 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9395}
9396
Hanno Becker8b170a02017-10-10 11:51:19 +01009397int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9398{
9399 /*
9400 * Case A: We're currently holding back
9401 * a message for further processing.
9402 */
9403
9404 if( ssl->keep_current_message == 1 )
9405 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009407 return( 1 );
9408 }
9409
9410 /*
9411 * Case B: Further records are pending in the current datagram.
9412 */
9413
9414#if defined(MBEDTLS_SSL_PROTO_DTLS)
9415 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9416 ssl->in_left > ssl->next_record_offset )
9417 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009419 return( 1 );
9420 }
9421#endif /* MBEDTLS_SSL_PROTO_DTLS */
9422
9423 /*
9424 * Case C: A handshake message is being processed.
9425 */
9426
Hanno Becker8b170a02017-10-10 11:51:19 +01009427 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9428 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009429 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009430 return( 1 );
9431 }
9432
9433 /*
9434 * Case D: An application data message is being processed
9435 */
9436 if( ssl->in_offt != NULL )
9437 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009439 return( 1 );
9440 }
9441
9442 /*
9443 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009444 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009445 * we implement support for multiple alerts in single records.
9446 */
9447
9448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9449 return( 0 );
9450}
9451
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009452uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009453{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009454 if( ssl->session != NULL )
9455 return( ssl->session->verify_result );
9456
9457 if( ssl->session_negotiate != NULL )
9458 return( ssl->session_negotiate->verify_result );
9459
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009460 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009461}
9462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009464{
Paul Bakker926c8e42013-03-06 10:23:34 +01009465 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009466 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009469}
9470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009472{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009474 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009475 {
9476 switch( ssl->minor_ver )
9477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009479 return( "DTLSv1.0" );
9480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009482 return( "DTLSv1.2" );
9483
9484 default:
9485 return( "unknown (DTLS)" );
9486 }
9487 }
9488#endif
9489
Paul Bakker43ca69c2011-01-15 17:35:19 +00009490 switch( ssl->minor_ver )
9491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009493 return( "SSLv3.0" );
9494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009496 return( "TLSv1.0" );
9497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009499 return( "TLSv1.1" );
9500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009502 return( "TLSv1.2" );
9503
Paul Bakker43ca69c2011-01-15 17:35:19 +00009504 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009505 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009506 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009507}
9508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009510{
Hanno Becker3136ede2018-08-17 15:28:19 +01009511 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009513 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009514
Hanno Becker5903de42019-05-03 14:46:38 +01009515 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9516
Hanno Becker78640902018-08-13 16:35:15 +01009517 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009518 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520#if defined(MBEDTLS_ZLIB_SUPPORT)
9521 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9522 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009523#endif
9524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527 case MBEDTLS_MODE_GCM:
9528 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009529 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009531 transform_expansion = transform->minlen;
9532 break;
9533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009535
9536 block_size = mbedtls_cipher_get_block_size(
9537 &transform->cipher_ctx_enc );
9538
Hanno Becker3136ede2018-08-17 15:28:19 +01009539 /* Expansion due to the addition of the MAC. */
9540 transform_expansion += transform->maclen;
9541
9542 /* Expansion due to the addition of CBC padding;
9543 * Theoretically up to 256 bytes, but we never use
9544 * more than the block size of the underlying cipher. */
9545 transform_expansion += block_size;
9546
9547 /* For TLS 1.1 or higher, an explicit IV is added
9548 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009549#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9550 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009551 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009552#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009553
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009554 break;
9555
9556 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009559 }
9560
Hanno Beckera0e20d02019-05-15 14:03:01 +01009561#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009562 if( transform->out_cid_len != 0 )
9563 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009564#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009565
Hanno Becker5903de42019-05-03 14:46:38 +01009566 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009567}
9568
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009569#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9570size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9571{
9572 size_t max_len;
9573
9574 /*
9575 * Assume mfl_code is correct since it was checked when set
9576 */
Angus Grattond8213d02016-05-25 20:56:48 +10009577 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009578
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009579 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009580 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009581 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009582 {
Angus Grattond8213d02016-05-25 20:56:48 +10009583 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009584 }
9585
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009586 /* During a handshake, use the value being negotiated */
9587 if( ssl->session_negotiate != NULL &&
9588 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9589 {
9590 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9591 }
9592
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009593 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009594}
9595#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9596
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009597#if defined(MBEDTLS_SSL_PROTO_DTLS)
9598static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9599{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009600 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9601 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9602 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9603 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9604 return ( 0 );
9605
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009606 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9607 return( ssl->mtu );
9608
9609 if( ssl->mtu == 0 )
9610 return( ssl->handshake->mtu );
9611
9612 return( ssl->mtu < ssl->handshake->mtu ?
9613 ssl->mtu : ssl->handshake->mtu );
9614}
9615#endif /* MBEDTLS_SSL_PROTO_DTLS */
9616
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009617int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9618{
9619 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9620
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009621#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9622 !defined(MBEDTLS_SSL_PROTO_DTLS)
9623 (void) ssl;
9624#endif
9625
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009626#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9627 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9628
9629 if( max_len > mfl )
9630 max_len = mfl;
9631#endif
9632
9633#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009634 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009635 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009636 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009637 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9638 const size_t overhead = (size_t) ret;
9639
9640 if( ret < 0 )
9641 return( ret );
9642
9643 if( mtu <= overhead )
9644 {
9645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9646 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9647 }
9648
9649 if( max_len > mtu - overhead )
9650 max_len = mtu - overhead;
9651 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009652#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009653
Hanno Becker0defedb2018-08-10 12:35:02 +01009654#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9655 !defined(MBEDTLS_SSL_PROTO_DTLS)
9656 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009657#endif
9658
9659 return( (int) max_len );
9660}
9661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662#if defined(MBEDTLS_X509_CRT_PARSE_C)
9663const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009664{
9665 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009666 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009667
Hanno Beckere6824572019-02-07 13:18:46 +00009668#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009669 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009670#else
9671 return( NULL );
9672#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009673}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009677int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9678 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009679{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009680 if( ssl == NULL ||
9681 dst == NULL ||
9682 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009683 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009686 }
9687
Hanno Becker52055ae2019-02-06 14:30:46 +00009688 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009691
Paul Bakker5121ce52009-01-03 21:22:43 +00009692/*
Paul Bakker1961b702013-01-25 14:49:24 +01009693 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009696{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009697 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009698
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009699 if( ssl == NULL || ssl->conf == NULL )
9700 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009702#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009703 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009705#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009707 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009709#endif
9710
Paul Bakker1961b702013-01-25 14:49:24 +01009711 return( ret );
9712}
9713
9714/*
9715 * Perform the SSL handshake
9716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009718{
9719 int ret = 0;
9720
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009721 if( ssl == NULL || ssl->conf == NULL )
9722 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009726 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009729
9730 if( ret != 0 )
9731 break;
9732 }
9733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009735
9736 return( ret );
9737}
9738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739#if defined(MBEDTLS_SSL_RENEGOTIATION)
9740#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009741/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009742 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009745{
9746 int ret;
9747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009749
9750 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9752 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009753
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009754 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009755 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009756 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009757 return( ret );
9758 }
9759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009761
9762 return( 0 );
9763}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009765
9766/*
9767 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768 * - any side: calling mbedtls_ssl_renegotiate(),
9769 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9770 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009771 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009772 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009775static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009776{
9777 int ret;
9778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009780
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009781 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9782 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009783
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009784 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9785 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009787 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009789 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009790 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009791 ssl->handshake->out_msg_seq = 1;
9792 else
9793 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009794 }
9795#endif
9796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9798 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009800 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009803 return( ret );
9804 }
9805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009807
9808 return( 0 );
9809}
9810
9811/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009812 * Renegotiate current connection on client,
9813 * or request renegotiation on server
9814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009816{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009818
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009819 if( ssl == NULL || ssl->conf == NULL )
9820 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009823 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009824 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9827 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009830
9831 /* Did we already try/start sending HelloRequest? */
9832 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009834
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009835 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009836 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009840 /*
9841 * On client, either start the renegotiation process or,
9842 * if already in progress, continue the handshake
9843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9847 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009848
9849 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009852 return( ret );
9853 }
9854 }
9855 else
9856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009860 return( ret );
9861 }
9862 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009864
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009865 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009866}
9867
9868/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009869 * Check record counters and renegotiate if they're above the limit.
9870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009872{
Andres AG2196c7f2016-12-15 17:01:16 +00009873 size_t ep_len = ssl_ep_len( ssl );
9874 int in_ctr_cmp;
9875 int out_ctr_cmp;
9876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009877 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9878 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009879 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009880 {
9881 return( 0 );
9882 }
9883
Andres AG2196c7f2016-12-15 17:01:16 +00009884 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9885 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009886 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009887 ssl->conf->renego_period + ep_len, 8 - ep_len );
9888
9889 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009890 {
9891 return( 0 );
9892 }
9893
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009895 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009896}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009898
9899/*
9900 * Receive application data decrypted from the SSL layer
9901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009903{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009904 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009905 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009906
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009907 if( ssl == NULL || ssl->conf == NULL )
9908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009913 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009915 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009916 return( ret );
9917
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009918 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009920 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009921 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009922 return( ret );
9923 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009924 }
9925#endif
9926
Hanno Becker4a810fb2017-05-24 16:27:30 +01009927 /*
9928 * Check if renegotiation is necessary and/or handshake is
9929 * in process. If yes, perform/continue, and fall through
9930 * if an unexpected packet is received while the client
9931 * is waiting for the ServerHello.
9932 *
9933 * (There is no equivalent to the last condition on
9934 * the server-side as it is not treated as within
9935 * a handshake while waiting for the ClientHello
9936 * after a renegotiation request.)
9937 */
9938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009940 ret = ssl_check_ctr_renegotiate( ssl );
9941 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9942 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009945 return( ret );
9946 }
9947#endif
9948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009951 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009952 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9953 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009956 return( ret );
9957 }
9958 }
9959
Hanno Beckere41158b2017-10-23 13:30:32 +01009960 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009961 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009962 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009963 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009964 if( ssl->f_get_timer != NULL &&
9965 ssl->f_get_timer( ssl->p_timer ) == -1 )
9966 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009967 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009968 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009969
Hanno Becker327c93b2018-08-15 13:56:18 +01009970 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009971 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009972 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9973 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009974
Hanno Becker4a810fb2017-05-24 16:27:30 +01009975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9976 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009977 }
9978
9979 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009981 {
9982 /*
9983 * OpenSSL sends empty messages to randomize the IV
9984 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009985 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009987 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009988 return( 0 );
9989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009991 return( ret );
9992 }
9993 }
9994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009998
Hanno Becker4a810fb2017-05-24 16:27:30 +01009999 /*
10000 * - For client-side, expect SERVER_HELLO_REQUEST.
10001 * - For server-side, expect CLIENT_HELLO.
10002 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10003 */
10004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010006 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010008 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010011
10012 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010013#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010014 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010015 {
10016 continue;
10017 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010018#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010019 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010020 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010021#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010022
Hanno Becker4a810fb2017-05-24 16:27:30 +010010023#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010024 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010025 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010028
10029 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010032 {
10033 continue;
10034 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010037 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010038#endif /* MBEDTLS_SSL_SRV_C */
10039
Hanno Becker21df7f92017-10-17 11:03:26 +010010040#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010041 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010042 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10043 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10044 ssl->conf->allow_legacy_renegotiation ==
10045 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10046 {
10047 /*
10048 * Accept renegotiation request
10049 */
Paul Bakker48916f92012-09-16 19:57:18 +000010050
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010051 /* DTLS clients need to know renego is server-initiated */
10052#if defined(MBEDTLS_SSL_PROTO_DTLS)
10053 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10054 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10055 {
10056 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10057 }
10058#endif
10059 ret = ssl_start_renegotiation( ssl );
10060 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10061 ret != 0 )
10062 {
10063 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10064 return( ret );
10065 }
10066 }
10067 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010068#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010069 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010070 /*
10071 * Refuse renegotiation
10072 */
10073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010076#if defined(MBEDTLS_SSL_PROTO_SSL3)
10077 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010078 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010079 /* SSLv3 does not have a "no_renegotiation" warning, so
10080 we send a fatal alert and abort the connection. */
10081 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10082 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10083 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010084 }
10085 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10087#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10088 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10089 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010091 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10092 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10093 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010094 {
10095 return( ret );
10096 }
Paul Bakker48916f92012-09-16 19:57:18 +000010097 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010098 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10100 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010104 }
Paul Bakker48916f92012-09-16 19:57:18 +000010105 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010106
Hanno Becker90333da2017-10-10 11:27:13 +010010107 /* At this point, we don't know whether the renegotiation has been
10108 * completed or not. The cases to consider are the following:
10109 * 1) The renegotiation is complete. In this case, no new record
10110 * has been read yet.
10111 * 2) The renegotiation is incomplete because the client received
10112 * an application data record while awaiting the ServerHello.
10113 * 3) The renegotiation is incomplete because the client received
10114 * a non-handshake, non-application data message while awaiting
10115 * the ServerHello.
10116 * In each of these case, looping will be the proper action:
10117 * - For 1), the next iteration will read a new record and check
10118 * if it's application data.
10119 * - For 2), the loop condition isn't satisfied as application data
10120 * is present, hence continue is the same as break
10121 * - For 3), the loop condition is satisfied and read_record
10122 * will re-deliver the message that was held back by the client
10123 * when expecting the ServerHello.
10124 */
10125 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010126 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010127#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010129 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010130 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010131 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010132 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010135 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010136 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010137 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010138 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010139 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010140#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10143 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010146 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010147 }
10148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010149 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10152 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010153 }
10154
10155 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010156
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010157 /* We're going to return something now, cancel timer,
10158 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010160 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010161
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010163 /* If we requested renego but received AppData, resend HelloRequest.
10164 * Do it now, after setting in_offt, to avoid taking this branch
10165 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010166#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010167 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010168 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010169 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010170 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010172 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010173 return( ret );
10174 }
10175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010177#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010178 }
10179
10180 n = ( len < ssl->in_msglen )
10181 ? len : ssl->in_msglen;
10182
10183 memcpy( buf, ssl->in_offt, n );
10184 ssl->in_msglen -= n;
10185
10186 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010187 {
10188 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010189 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010190 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010191 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010192 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010193 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010194 /* more data available */
10195 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010196 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010199
Paul Bakker23986e52011-04-24 08:57:21 +000010200 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010201}
10202
10203/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010204 * Send application data to be encrypted by the SSL layer, taking care of max
10205 * fragment length and buffer size.
10206 *
10207 * According to RFC 5246 Section 6.2.1:
10208 *
10209 * Zero-length fragments of Application data MAY be sent as they are
10210 * potentially useful as a traffic analysis countermeasure.
10211 *
10212 * Therefore, it is possible that the input message length is 0 and the
10213 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010214 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010215static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010216 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010217{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010218 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10219 const size_t max_len = (size_t) ret;
10220
10221 if( ret < 0 )
10222 {
10223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10224 return( ret );
10225 }
10226
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010227 if( len > max_len )
10228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010230 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010233 "maximum fragment length: %d > %d",
10234 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010236 }
10237 else
10238#endif
10239 len = max_len;
10240 }
Paul Bakker887bd502011-06-08 13:10:54 +000010241
Paul Bakker5121ce52009-01-03 21:22:43 +000010242 if( ssl->out_left != 0 )
10243 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010244 /*
10245 * The user has previously tried to send the data and
10246 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10247 * written. In this case, we expect the high-level write function
10248 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010253 return( ret );
10254 }
10255 }
Paul Bakker887bd502011-06-08 13:10:54 +000010256 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010257 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010258 /*
10259 * The user is trying to send a message the first time, so we need to
10260 * copy the data into the internal buffers and setup the data structure
10261 * to keep track of partial writes
10262 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010263 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010265 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010266
Hanno Becker67bc7c32018-08-06 11:33:50 +010010267 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010269 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010270 return( ret );
10271 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010272 }
10273
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010274 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010275}
10276
10277/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010278 * Write application data, doing 1/n-1 splitting if necessary.
10279 *
10280 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010281 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010282 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010284#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010285static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010286 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010287{
10288 int ret;
10289
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010290 if( ssl->conf->cbc_record_splitting ==
10291 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010292 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10294 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10295 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010296 {
10297 return( ssl_write_real( ssl, buf, len ) );
10298 }
10299
10300 if( ssl->split_done == 0 )
10301 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010302 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010303 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010304 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010305 }
10306
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010307 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10308 return( ret );
10309 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010310
10311 return( ret + 1 );
10312}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010313#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010314
10315/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010316 * Write application data (public-facing wrapper)
10317 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010318int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010319{
10320 int ret;
10321
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010323
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010324 if( ssl == NULL || ssl->conf == NULL )
10325 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10326
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010327#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010328 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10329 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010330 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010331 return( ret );
10332 }
10333#endif
10334
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010335 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010336 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010337 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010338 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010340 return( ret );
10341 }
10342 }
10343
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010344#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010345 ret = ssl_write_split( ssl, buf, len );
10346#else
10347 ret = ssl_write_real( ssl, buf, len );
10348#endif
10349
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010351
10352 return( ret );
10353}
10354
10355/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010356 * Notify the peer that the connection is being closed
10357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010359{
10360 int ret;
10361
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010362 if( ssl == NULL || ssl->conf == NULL )
10363 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010366
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010367 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010370 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010372 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10373 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10374 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010376 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010377 return( ret );
10378 }
10379 }
10380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010382
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010383 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010384}
10385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010386void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010387{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010388 if( transform == NULL )
10389 return;
10390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010391#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010392 deflateEnd( &transform->ctx_deflate );
10393 inflateEnd( &transform->ctx_inflate );
10394#endif
10395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010396 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10397 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010398
Hanno Beckerd56ed242018-01-03 15:32:51 +000010399#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400 mbedtls_md_free( &transform->md_ctx_enc );
10401 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010402#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010403
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010404 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010405}
10406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407#if defined(MBEDTLS_X509_CRT_PARSE_C)
10408static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010410 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010411
10412 while( cur != NULL )
10413 {
10414 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010415 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010416 cur = next;
10417 }
10418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010420
Hanno Becker0271f962018-08-16 13:23:47 +010010421#if defined(MBEDTLS_SSL_PROTO_DTLS)
10422
10423static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10424{
10425 unsigned offset;
10426 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10427
10428 if( hs == NULL )
10429 return;
10430
Hanno Becker283f5ef2018-08-24 09:34:47 +010010431 ssl_free_buffered_record( ssl );
10432
Hanno Becker0271f962018-08-16 13:23:47 +010010433 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010434 ssl_buffering_free_slot( ssl, offset );
10435}
10436
10437static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10438 uint8_t slot )
10439{
10440 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10441 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010442
10443 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10444 return;
10445
Hanno Beckere605b192018-08-21 15:59:07 +010010446 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010447 {
Hanno Beckere605b192018-08-21 15:59:07 +010010448 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010449 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010450 mbedtls_free( hs_buf->data );
10451 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010452 }
10453}
10454
10455#endif /* MBEDTLS_SSL_PROTO_DTLS */
10456
Gilles Peskine9b562d52018-04-25 20:32:43 +020010457void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010458{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010459 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10460
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010461 if( handshake == NULL )
10462 return;
10463
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010464#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10465 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10466 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010467 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010468 handshake->async_in_progress = 0;
10469 }
10470#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10471
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010472#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10473 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10474 mbedtls_md5_free( &handshake->fin_md5 );
10475 mbedtls_sha1_free( &handshake->fin_sha1 );
10476#endif
10477#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10478#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010479#if defined(MBEDTLS_USE_PSA_CRYPTO)
10480 psa_hash_abort( &handshake->fin_sha256_psa );
10481#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010482 mbedtls_sha256_free( &handshake->fin_sha256 );
10483#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010484#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010485#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010486#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010487 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010488#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010489 mbedtls_sha512_free( &handshake->fin_sha512 );
10490#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010491#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010492#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494#if defined(MBEDTLS_DHM_C)
10495 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010496#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010497#if defined(MBEDTLS_ECDH_C)
10498 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010499#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010500#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010501 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010502#if defined(MBEDTLS_SSL_CLI_C)
10503 mbedtls_free( handshake->ecjpake_cache );
10504 handshake->ecjpake_cache = NULL;
10505 handshake->ecjpake_cache_len = 0;
10506#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010507#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010508
Janos Follath4ae5c292016-02-10 11:27:43 +000010509#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10510 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010511 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010512 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010513#endif
10514
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010515#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10516 if( handshake->psk != NULL )
10517 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010518 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010519 mbedtls_free( handshake->psk );
10520 }
10521#endif
10522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010523#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10524 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010525 /*
10526 * Free only the linked list wrapper, not the keys themselves
10527 * since the belong to the SNI callback
10528 */
10529 if( handshake->sni_key_cert != NULL )
10530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010531 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010532
10533 while( cur != NULL )
10534 {
10535 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010536 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010537 cur = next;
10538 }
10539 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010540#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010541
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010542#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010543 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010544 if( handshake->ecrs_peer_cert != NULL )
10545 {
10546 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10547 mbedtls_free( handshake->ecrs_peer_cert );
10548 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010549#endif
10550
Hanno Becker75173122019-02-06 16:18:31 +000010551#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10552 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10553 mbedtls_pk_free( &handshake->peer_pubkey );
10554#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010556#if defined(MBEDTLS_SSL_PROTO_DTLS)
10557 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010558 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010559 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010560#endif
10561
Hanno Becker4a63ed42019-01-08 11:39:35 +000010562#if defined(MBEDTLS_ECDH_C) && \
10563 defined(MBEDTLS_USE_PSA_CRYPTO)
10564 psa_destroy_key( handshake->ecdh_psa_privkey );
10565#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10566
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010567 mbedtls_platform_zeroize( handshake,
10568 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010569}
10570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010571void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010572{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010573 if( session == NULL )
10574 return;
10575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010576#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010577 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010578#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010579
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010580#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010581 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010582#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010583
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010584 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010585}
10586
Paul Bakker5121ce52009-01-03 21:22:43 +000010587/*
10588 * Free an SSL context
10589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010590void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010591{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010592 if( ssl == NULL )
10593 return;
10594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010596
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010597 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010598 {
Angus Grattond8213d02016-05-25 20:56:48 +100010599 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010600 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010601 }
10602
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010603 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010604 {
Angus Grattond8213d02016-05-25 20:56:48 +100010605 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010606 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010607 }
10608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010609#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010610 if( ssl->compress_buf != NULL )
10611 {
Angus Grattond8213d02016-05-25 20:56:48 +100010612 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010613 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010614 }
10615#endif
10616
Paul Bakker48916f92012-09-16 19:57:18 +000010617 if( ssl->transform )
10618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010619 mbedtls_ssl_transform_free( ssl->transform );
10620 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010621 }
10622
10623 if( ssl->handshake )
10624 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010625 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10627 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629 mbedtls_free( ssl->handshake );
10630 mbedtls_free( ssl->transform_negotiate );
10631 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010632 }
10633
Paul Bakkerc0463502013-02-14 11:19:38 +010010634 if( ssl->session )
10635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636 mbedtls_ssl_session_free( ssl->session );
10637 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010638 }
10639
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010640#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010641 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010642 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010643 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010645 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010646#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010648#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10649 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10652 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010653 }
10654#endif
10655
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010656#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010657 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010658#endif
10659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010661
Paul Bakker86f04f42013-02-14 11:20:09 +010010662 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010663 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010664}
10665
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010666/*
10667 * Initialze mbedtls_ssl_config
10668 */
10669void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10670{
10671 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10672}
10673
Simon Butcherc97b6972015-12-27 23:48:17 +000010674#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010675static int ssl_preset_default_hashes[] = {
10676#if defined(MBEDTLS_SHA512_C)
10677 MBEDTLS_MD_SHA512,
10678 MBEDTLS_MD_SHA384,
10679#endif
10680#if defined(MBEDTLS_SHA256_C)
10681 MBEDTLS_MD_SHA256,
10682 MBEDTLS_MD_SHA224,
10683#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010684#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010685 MBEDTLS_MD_SHA1,
10686#endif
10687 MBEDTLS_MD_NONE
10688};
Simon Butcherc97b6972015-12-27 23:48:17 +000010689#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010690
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010691static int ssl_preset_suiteb_ciphersuites[] = {
10692 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10693 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10694 0
10695};
10696
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010697#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010698static int ssl_preset_suiteb_hashes[] = {
10699 MBEDTLS_MD_SHA256,
10700 MBEDTLS_MD_SHA384,
10701 MBEDTLS_MD_NONE
10702};
10703#endif
10704
10705#if defined(MBEDTLS_ECP_C)
10706static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010707#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010708 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010709#endif
10710#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010711 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010712#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010713 MBEDTLS_ECP_DP_NONE
10714};
10715#endif
10716
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010717/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010718 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010719 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010720int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010721 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010722{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010723#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010724 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010725#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010726
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010727 /* Use the functions here so that they are covered in tests,
10728 * but otherwise access member directly for efficiency */
10729 mbedtls_ssl_conf_endpoint( conf, endpoint );
10730 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010731
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010732 /*
10733 * Things that are common to all presets
10734 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010735#if defined(MBEDTLS_SSL_CLI_C)
10736 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10737 {
10738 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10739#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10740 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10741#endif
10742 }
10743#endif
10744
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010745#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010746 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010747#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010748
10749#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10750 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10751#endif
10752
10753#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10754 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10755#endif
10756
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010757#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10758 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10759#endif
10760
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010761#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010762 conf->f_cookie_write = ssl_cookie_write_dummy;
10763 conf->f_cookie_check = ssl_cookie_check_dummy;
10764#endif
10765
10766#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10767 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10768#endif
10769
Janos Follath088ce432017-04-10 12:42:31 +010010770#if defined(MBEDTLS_SSL_SRV_C)
10771 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10772#endif
10773
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010774#if defined(MBEDTLS_SSL_PROTO_DTLS)
10775 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10776 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10777#endif
10778
10779#if defined(MBEDTLS_SSL_RENEGOTIATION)
10780 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010781 memset( conf->renego_period, 0x00, 2 );
10782 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010783#endif
10784
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010785#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10786 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10787 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010788 const unsigned char dhm_p[] =
10789 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10790 const unsigned char dhm_g[] =
10791 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10792
Hanno Beckera90658f2017-10-04 15:29:08 +010010793 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10794 dhm_p, sizeof( dhm_p ),
10795 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010796 {
10797 return( ret );
10798 }
10799 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010800#endif
10801
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010802 /*
10803 * Preset-specific defaults
10804 */
10805 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010806 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010807 /*
10808 * NSA Suite B
10809 */
10810 case MBEDTLS_SSL_PRESET_SUITEB:
10811 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10812 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10813 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10814 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10815
10816 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10817 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10818 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10819 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10820 ssl_preset_suiteb_ciphersuites;
10821
10822#if defined(MBEDTLS_X509_CRT_PARSE_C)
10823 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010824#endif
10825
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010826#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010827 conf->sig_hashes = ssl_preset_suiteb_hashes;
10828#endif
10829
10830#if defined(MBEDTLS_ECP_C)
10831 conf->curve_list = ssl_preset_suiteb_curves;
10832#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010833 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010834
10835 /*
10836 * Default
10837 */
10838 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010839 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10840 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10841 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10842 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10843 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10844 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10845 MBEDTLS_SSL_MIN_MINOR_VERSION :
10846 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010847 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10848 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10849
10850#if defined(MBEDTLS_SSL_PROTO_DTLS)
10851 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10852 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10853#endif
10854
10855 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10856 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10857 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10858 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10859 mbedtls_ssl_list_ciphersuites();
10860
10861#if defined(MBEDTLS_X509_CRT_PARSE_C)
10862 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10863#endif
10864
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010865#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010866 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010867#endif
10868
10869#if defined(MBEDTLS_ECP_C)
10870 conf->curve_list = mbedtls_ecp_grp_id_list();
10871#endif
10872
10873#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10874 conf->dhm_min_bitlen = 1024;
10875#endif
10876 }
10877
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010878 return( 0 );
10879}
10880
10881/*
10882 * Free mbedtls_ssl_config
10883 */
10884void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10885{
10886#if defined(MBEDTLS_DHM_C)
10887 mbedtls_mpi_free( &conf->dhm_P );
10888 mbedtls_mpi_free( &conf->dhm_G );
10889#endif
10890
10891#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10892 if( conf->psk != NULL )
10893 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010894 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010895 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010896 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010897 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010898 }
10899
10900 if( conf->psk_identity != NULL )
10901 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010902 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010903 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010904 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010905 conf->psk_identity_len = 0;
10906 }
10907#endif
10908
10909#if defined(MBEDTLS_X509_CRT_PARSE_C)
10910 ssl_key_cert_free( conf->key_cert );
10911#endif
10912
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010913 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010914}
10915
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010916#if defined(MBEDTLS_PK_C) && \
10917 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010918/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010919 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010921unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010922{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010923#if defined(MBEDTLS_RSA_C)
10924 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10925 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010926#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010927#if defined(MBEDTLS_ECDSA_C)
10928 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10929 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010931 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010932}
10933
Hanno Becker7e5437a2017-04-28 17:15:26 +010010934unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10935{
10936 switch( type ) {
10937 case MBEDTLS_PK_RSA:
10938 return( MBEDTLS_SSL_SIG_RSA );
10939 case MBEDTLS_PK_ECDSA:
10940 case MBEDTLS_PK_ECKEY:
10941 return( MBEDTLS_SSL_SIG_ECDSA );
10942 default:
10943 return( MBEDTLS_SSL_SIG_ANON );
10944 }
10945}
10946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010947mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010948{
10949 switch( sig )
10950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010951#if defined(MBEDTLS_RSA_C)
10952 case MBEDTLS_SSL_SIG_RSA:
10953 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010954#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010955#if defined(MBEDTLS_ECDSA_C)
10956 case MBEDTLS_SSL_SIG_ECDSA:
10957 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010958#endif
10959 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010960 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010961 }
10962}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010963#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010964
Hanno Becker7e5437a2017-04-28 17:15:26 +010010965#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10966 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10967
10968/* Find an entry in a signature-hash set matching a given hash algorithm. */
10969mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10970 mbedtls_pk_type_t sig_alg )
10971{
10972 switch( sig_alg )
10973 {
10974 case MBEDTLS_PK_RSA:
10975 return( set->rsa );
10976 case MBEDTLS_PK_ECDSA:
10977 return( set->ecdsa );
10978 default:
10979 return( MBEDTLS_MD_NONE );
10980 }
10981}
10982
10983/* Add a signature-hash-pair to a signature-hash set */
10984void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10985 mbedtls_pk_type_t sig_alg,
10986 mbedtls_md_type_t md_alg )
10987{
10988 switch( sig_alg )
10989 {
10990 case MBEDTLS_PK_RSA:
10991 if( set->rsa == MBEDTLS_MD_NONE )
10992 set->rsa = md_alg;
10993 break;
10994
10995 case MBEDTLS_PK_ECDSA:
10996 if( set->ecdsa == MBEDTLS_MD_NONE )
10997 set->ecdsa = md_alg;
10998 break;
10999
11000 default:
11001 break;
11002 }
11003}
11004
11005/* Allow exactly one hash algorithm for each signature. */
11006void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11007 mbedtls_md_type_t md_alg )
11008{
11009 set->rsa = md_alg;
11010 set->ecdsa = md_alg;
11011}
11012
11013#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11014 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11015
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011016/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011017 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011019mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011020{
11021 switch( hash )
11022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011023#if defined(MBEDTLS_MD5_C)
11024 case MBEDTLS_SSL_HASH_MD5:
11025 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011026#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011027#if defined(MBEDTLS_SHA1_C)
11028 case MBEDTLS_SSL_HASH_SHA1:
11029 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011031#if defined(MBEDTLS_SHA256_C)
11032 case MBEDTLS_SSL_HASH_SHA224:
11033 return( MBEDTLS_MD_SHA224 );
11034 case MBEDTLS_SSL_HASH_SHA256:
11035 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011037#if defined(MBEDTLS_SHA512_C)
11038 case MBEDTLS_SSL_HASH_SHA384:
11039 return( MBEDTLS_MD_SHA384 );
11040 case MBEDTLS_SSL_HASH_SHA512:
11041 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011042#endif
11043 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011044 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011045 }
11046}
11047
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011048/*
11049 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11050 */
11051unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11052{
11053 switch( md )
11054 {
11055#if defined(MBEDTLS_MD5_C)
11056 case MBEDTLS_MD_MD5:
11057 return( MBEDTLS_SSL_HASH_MD5 );
11058#endif
11059#if defined(MBEDTLS_SHA1_C)
11060 case MBEDTLS_MD_SHA1:
11061 return( MBEDTLS_SSL_HASH_SHA1 );
11062#endif
11063#if defined(MBEDTLS_SHA256_C)
11064 case MBEDTLS_MD_SHA224:
11065 return( MBEDTLS_SSL_HASH_SHA224 );
11066 case MBEDTLS_MD_SHA256:
11067 return( MBEDTLS_SSL_HASH_SHA256 );
11068#endif
11069#if defined(MBEDTLS_SHA512_C)
11070 case MBEDTLS_MD_SHA384:
11071 return( MBEDTLS_SSL_HASH_SHA384 );
11072 case MBEDTLS_MD_SHA512:
11073 return( MBEDTLS_SSL_HASH_SHA512 );
11074#endif
11075 default:
11076 return( MBEDTLS_SSL_HASH_NONE );
11077 }
11078}
11079
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011080#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011081/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011082 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011083 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011084 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011085int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011086{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011087 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011088
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011089 if( ssl->conf->curve_list == NULL )
11090 return( -1 );
11091
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011092 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011093 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011094 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011095
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011096 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011097}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011098#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011099
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011100#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011101/*
11102 * Check if a hash proposed by the peer is in our list.
11103 * Return 0 if we're willing to use it, -1 otherwise.
11104 */
11105int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11106 mbedtls_md_type_t md )
11107{
11108 const int *cur;
11109
11110 if( ssl->conf->sig_hashes == NULL )
11111 return( -1 );
11112
11113 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11114 if( *cur == (int) md )
11115 return( 0 );
11116
11117 return( -1 );
11118}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011119#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011121#if defined(MBEDTLS_X509_CRT_PARSE_C)
11122int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11123 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011124 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011125 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011126{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011127 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011128#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011129 int usage = 0;
11130#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011131#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011132 const char *ext_oid;
11133 size_t ext_len;
11134#endif
11135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011136#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11137 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011138 ((void) cert);
11139 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011140 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011141#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011143#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11144 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011145 {
11146 /* Server part of the key exchange */
11147 switch( ciphersuite->key_exchange )
11148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011149 case MBEDTLS_KEY_EXCHANGE_RSA:
11150 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011151 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011152 break;
11153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011154 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11155 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11156 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11157 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011158 break;
11159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011160 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11161 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011162 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011163 break;
11164
11165 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011166 case MBEDTLS_KEY_EXCHANGE_NONE:
11167 case MBEDTLS_KEY_EXCHANGE_PSK:
11168 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11169 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011170 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011171 usage = 0;
11172 }
11173 }
11174 else
11175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011176 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11177 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011178 }
11179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011180 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011181 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011182 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011183 ret = -1;
11184 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011185#else
11186 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011187#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011189#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11190 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011192 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11193 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011194 }
11195 else
11196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011197 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11198 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011199 }
11200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011201 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011202 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011203 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011204 ret = -1;
11205 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011206#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011207
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011208 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011210#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011211
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011212/*
11213 * Convert version numbers to/from wire format
11214 * and, for DTLS, to/from TLS equivalent.
11215 *
11216 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011217 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011218 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11219 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011221void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011222 unsigned char ver[2] )
11223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011224#if defined(MBEDTLS_SSL_PROTO_DTLS)
11225 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011227 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011228 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11229
11230 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11231 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11232 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011233 else
11234#else
11235 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011236#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011237 {
11238 ver[0] = (unsigned char) major;
11239 ver[1] = (unsigned char) minor;
11240 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011241}
11242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011243void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011244 const unsigned char ver[2] )
11245{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011246#if defined(MBEDTLS_SSL_PROTO_DTLS)
11247 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011248 {
11249 *major = 255 - ver[0] + 2;
11250 *minor = 255 - ver[1] + 1;
11251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011252 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011253 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11254 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011255 else
11256#else
11257 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011258#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011259 {
11260 *major = ver[0];
11261 *minor = ver[1];
11262 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011263}
11264
Simon Butcher99000142016-10-13 17:21:01 +010011265int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11266{
11267#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11268 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11269 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11270
11271 switch( md )
11272 {
11273#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11274#if defined(MBEDTLS_MD5_C)
11275 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011276 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011277#endif
11278#if defined(MBEDTLS_SHA1_C)
11279 case MBEDTLS_SSL_HASH_SHA1:
11280 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11281 break;
11282#endif
11283#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11284#if defined(MBEDTLS_SHA512_C)
11285 case MBEDTLS_SSL_HASH_SHA384:
11286 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11287 break;
11288#endif
11289#if defined(MBEDTLS_SHA256_C)
11290 case MBEDTLS_SSL_HASH_SHA256:
11291 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11292 break;
11293#endif
11294 default:
11295 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11296 }
11297
11298 return 0;
11299#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11300 (void) ssl;
11301 (void) md;
11302
11303 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11304#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11305}
11306
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011307#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11308 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11309int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11310 unsigned char *output,
11311 unsigned char *data, size_t data_len )
11312{
11313 int ret = 0;
11314 mbedtls_md5_context mbedtls_md5;
11315 mbedtls_sha1_context mbedtls_sha1;
11316
11317 mbedtls_md5_init( &mbedtls_md5 );
11318 mbedtls_sha1_init( &mbedtls_sha1 );
11319
11320 /*
11321 * digitally-signed struct {
11322 * opaque md5_hash[16];
11323 * opaque sha_hash[20];
11324 * };
11325 *
11326 * md5_hash
11327 * MD5(ClientHello.random + ServerHello.random
11328 * + ServerParams);
11329 * sha_hash
11330 * SHA(ClientHello.random + ServerHello.random
11331 * + ServerParams);
11332 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011333 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011334 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011336 goto exit;
11337 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011338 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011339 ssl->handshake->randbytes, 64 ) ) != 0 )
11340 {
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_update_ret( &mbedtls_md5, data, data_len ) ) != 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_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011347 goto exit;
11348 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011349 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011350 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011352 goto exit;
11353 }
11354
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011355 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011356 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011357 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011358 goto exit;
11359 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011360 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011361 ssl->handshake->randbytes, 64 ) ) != 0 )
11362 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011364 goto exit;
11365 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011366 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011367 data_len ) ) != 0 )
11368 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011370 goto exit;
11371 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011372 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011373 output + 16 ) ) != 0 )
11374 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011376 goto exit;
11377 }
11378
11379exit:
11380 mbedtls_md5_free( &mbedtls_md5 );
11381 mbedtls_sha1_free( &mbedtls_sha1 );
11382
11383 if( ret != 0 )
11384 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11385 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11386
11387 return( ret );
11388
11389}
11390#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11391 MBEDTLS_SSL_PROTO_TLS1_1 */
11392
11393#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11394 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011395
11396#if defined(MBEDTLS_USE_PSA_CRYPTO)
11397int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11398 unsigned char *hash, size_t *hashlen,
11399 unsigned char *data, size_t data_len,
11400 mbedtls_md_type_t md_alg )
11401{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011402 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011403 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011404 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11405
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011407
11408 if( ( status = psa_hash_setup( &hash_operation,
11409 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011410 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011411 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011412 goto exit;
11413 }
11414
Andrzej Kurek814feff2019-01-14 04:35:19 -050011415 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11416 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011417 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011418 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011419 goto exit;
11420 }
11421
Andrzej Kurek814feff2019-01-14 04:35:19 -050011422 if( ( status = psa_hash_update( &hash_operation,
11423 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011424 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011425 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011426 goto exit;
11427 }
11428
Andrzej Kurek814feff2019-01-14 04:35:19 -050011429 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11430 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011431 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011432 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011433 goto exit;
11434 }
11435
11436exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011437 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011438 {
11439 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11440 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011441 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011442 {
11443 case PSA_ERROR_NOT_SUPPORTED:
11444 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011445 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011446 case PSA_ERROR_BUFFER_TOO_SMALL:
11447 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11448 case PSA_ERROR_INSUFFICIENT_MEMORY:
11449 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11450 default:
11451 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11452 }
11453 }
11454 return( 0 );
11455}
11456
11457#else
11458
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011459int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011460 unsigned char *hash, size_t *hashlen,
11461 unsigned char *data, size_t data_len,
11462 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011463{
11464 int ret = 0;
11465 mbedtls_md_context_t ctx;
11466 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011467 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011468
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011469 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011470
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011471 mbedtls_md_init( &ctx );
11472
11473 /*
11474 * digitally-signed struct {
11475 * opaque client_random[32];
11476 * opaque server_random[32];
11477 * ServerDHParams params;
11478 * };
11479 */
11480 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11481 {
11482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11483 goto exit;
11484 }
11485 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11486 {
11487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11488 goto exit;
11489 }
11490 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11491 {
11492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11493 goto exit;
11494 }
11495 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11496 {
11497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11498 goto exit;
11499 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011500 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011501 {
11502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11503 goto exit;
11504 }
11505
11506exit:
11507 mbedtls_md_free( &ctx );
11508
11509 if( ret != 0 )
11510 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11511 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11512
11513 return( ret );
11514}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011515#endif /* MBEDTLS_USE_PSA_CRYPTO */
11516
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011517#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11518 MBEDTLS_SSL_PROTO_TLS1_2 */
11519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011520#endif /* MBEDTLS_SSL_TLS_C */