blob: 102a13f7d2f5fd85a913b18a0bc6767407c1311c [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
Hanno Beckere5e7e782019-07-11 12:29:35 +01005101 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005102 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5103 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005104 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5105
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005107 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005108 rec->type,
5109 major_ver, minor_ver, rec->data_len ) );
5110
5111 rec->buf = buf;
5112 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005113
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005114 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005115 * DTLS-related tests.
5116 * Check epoch before checking length constraint because
5117 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5118 * message gets duplicated before the corresponding Finished message,
5119 * the second ChangeCipherSpec should be discarded because it belongs
5120 * to an old epoch, but not because its length is shorter than
5121 * the minimum record length for packets using the new record transform.
5122 * Note that these two kinds of failures are handled differently,
5123 * as an unexpected record is silently skipped but an invalid
5124 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005125 */
5126#if defined(MBEDTLS_SSL_PROTO_DTLS)
5127 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5128 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005129 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005130
Hanno Becker955a5c92019-07-10 17:12:07 +01005131 /* Check that the datagram is large enough to contain a record
5132 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005133 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005134 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5136 (unsigned) len,
5137 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005138 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5139 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005140
Hanno Becker37cfe732019-07-10 17:20:01 +01005141 /* Records from other, non-matching epochs are silently discarded.
5142 * (The case of same-port Client reconnects must be considered in
5143 * the caller). */
Hanno Becker552f7472019-07-19 10:59:12 +01005144 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005145 {
5146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5147 "expected %d, received %d",
5148 ssl->in_epoch, rec_epoch ) );
Hanno Becker552f7472019-07-19 10:59:12 +01005149
5150 /* Records from the next epoch are considered for buffering
5151 * (concretely: early Finished messages). */
5152 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5153 {
5154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5155 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5156 }
5157
Hanno Becker2fddd372019-07-10 14:37:41 +01005158 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005159 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005160#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005161 /* For records from the correct epoch, check whether their
5162 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005163 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005164 {
5165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5166 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5167 }
5168#endif
5169 }
5170#endif /* MBEDTLS_SSL_PROTO_DTLS */
5171
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005172 return( 0 );
5173}
Paul Bakker5121ce52009-01-03 21:22:43 +00005174
Hanno Becker2fddd372019-07-10 14:37:41 +01005175
5176#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5177static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5178{
5179 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5180
5181 /*
5182 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5183 * access the first byte of record content (handshake type), as we
5184 * have an active transform (possibly iv_len != 0), so use the
5185 * fact that the record header len is 13 instead.
5186 */
5187 if( rec_epoch == 0 &&
5188 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5189 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5190 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5191 ssl->in_left > 13 &&
5192 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5193 {
5194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5195 "from the same port" ) );
5196 return( ssl_handle_possible_reconnect( ssl ) );
5197 }
5198
5199 return( 0 );
5200}
5201#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5202
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005203/*
5204 * If applicable, decrypt (and decompress) record content
5205 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005206static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5207 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005208{
5209 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005211 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005212 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5215 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005219 ret = mbedtls_ssl_hw_record_read( ssl );
5220 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5223 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005224 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005225
5226 if( ret == 0 )
5227 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005228 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005230 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005231 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005232 unsigned char const old_msg_type = rec->type;
5233
Hanno Beckera18d1322018-01-03 14:27:32 +00005234 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005235 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005238
Hanno Beckera0e20d02019-05-15 14:03:01 +01005239#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005240 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5241 ssl->conf->ignore_unexpected_cid
5242 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5243 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005244 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005245 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005246 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005247#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005248
Paul Bakker5121ce52009-01-03 21:22:43 +00005249 return( ret );
5250 }
5251
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005252 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005253 {
5254 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005255 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005256 }
5257
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005258 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005259 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005260
Hanno Beckera0e20d02019-05-15 14:03:01 +01005261#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005262 /* We have already checked the record content type
5263 * in ssl_parse_record_header(), failing or silently
5264 * dropping the record in the case of an unknown type.
5265 *
5266 * Since with the use of CIDs, the record content type
5267 * might change during decryption, re-check the record
5268 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005269 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005270 {
5271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5272 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5273 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005274#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005275
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005276 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005277 {
5278#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5279 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005280 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005281 {
5282 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5284 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5285 }
5286#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5287
5288 ssl->nb_zero++;
5289
5290 /*
5291 * Three or more empty messages may be a DoS attack
5292 * (excessive CPU consumption).
5293 */
5294 if( ssl->nb_zero > 3 )
5295 {
5296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005297 "messages, possible DoS attack" ) );
5298 /* Treat the records as if they were not properly authenticated,
5299 * thereby failing the connection if we see more than allowed
5300 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005301 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5302 }
5303 }
5304 else
5305 ssl->nb_zero = 0;
5306
5307#if defined(MBEDTLS_SSL_PROTO_DTLS)
5308 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5309 {
5310 ; /* in_ctr read from peer, not maintained internally */
5311 }
5312 else
5313#endif
5314 {
5315 unsigned i;
5316 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5317 if( ++ssl->in_ctr[i - 1] != 0 )
5318 break;
5319
5320 /* The loop goes to its end iff the counter is wrapping */
5321 if( i == ssl_ep_len( ssl ) )
5322 {
5323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5324 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5325 }
5326 }
5327
Paul Bakker5121ce52009-01-03 21:22:43 +00005328 }
5329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005331 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005333 {
5334 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005337 return( ret );
5338 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005343 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005346 }
5347#endif
5348
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005349 /* Check actual (decrypted) record content length against
5350 * configured maximum. */
5351 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5352 {
5353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5354 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5355 }
5356
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005357 return( 0 );
5358}
5359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005361
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005362/*
5363 * Read a record.
5364 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005365 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5366 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5367 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005368 */
Hanno Becker1097b342018-08-15 14:09:41 +01005369
5370/* Helper functions for mbedtls_ssl_read_record(). */
5371static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005372static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5373static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005374
Hanno Becker327c93b2018-08-15 13:56:18 +01005375int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005376 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005377{
5378 int ret;
5379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005381
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005382 if( ssl->keep_current_message == 0 )
5383 {
5384 do {
Simon Butcher99000142016-10-13 17:21:01 +01005385
Hanno Becker26994592018-08-15 14:14:59 +01005386 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005387 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005388 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005389
Hanno Beckere74d5562018-08-15 14:26:08 +01005390 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005391 {
Hanno Becker40f50842018-08-15 14:48:01 +01005392#if defined(MBEDTLS_SSL_PROTO_DTLS)
5393 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005394
Hanno Becker40f50842018-08-15 14:48:01 +01005395 /* We only check for buffered messages if the
5396 * current datagram is fully consumed. */
5397 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005398 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005399 {
Hanno Becker40f50842018-08-15 14:48:01 +01005400 if( ssl_load_buffered_message( ssl ) == 0 )
5401 have_buffered = 1;
5402 }
5403
5404 if( have_buffered == 0 )
5405#endif /* MBEDTLS_SSL_PROTO_DTLS */
5406 {
5407 ret = ssl_get_next_record( ssl );
5408 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5409 continue;
5410
5411 if( ret != 0 )
5412 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005413 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005414 return( ret );
5415 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005416 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005417 }
5418
5419 ret = mbedtls_ssl_handle_message_type( ssl );
5420
Hanno Becker40f50842018-08-15 14:48:01 +01005421#if defined(MBEDTLS_SSL_PROTO_DTLS)
5422 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5423 {
5424 /* Buffer future message */
5425 ret = ssl_buffer_message( ssl );
5426 if( ret != 0 )
5427 return( ret );
5428
5429 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5430 }
5431#endif /* MBEDTLS_SSL_PROTO_DTLS */
5432
Hanno Becker90333da2017-10-10 11:27:13 +01005433 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5434 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005435
5436 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005437 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005438 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005439 return( ret );
5440 }
5441
Hanno Becker327c93b2018-08-15 13:56:18 +01005442 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005443 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005444 {
5445 mbedtls_ssl_update_handshake_status( ssl );
5446 }
Simon Butcher99000142016-10-13 17:21:01 +01005447 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005448 else
Simon Butcher99000142016-10-13 17:21:01 +01005449 {
Hanno Becker02f59072018-08-15 14:00:24 +01005450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005451 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005452 }
5453
5454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5455
5456 return( 0 );
5457}
5458
Hanno Becker40f50842018-08-15 14:48:01 +01005459#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005460static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005461{
Hanno Becker40f50842018-08-15 14:48:01 +01005462 if( ssl->in_left > ssl->next_record_offset )
5463 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005464
Hanno Becker40f50842018-08-15 14:48:01 +01005465 return( 0 );
5466}
5467
5468static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5469{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005470 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005471 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005472 int ret = 0;
5473
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005474 if( hs == NULL )
5475 return( -1 );
5476
Hanno Beckere00ae372018-08-20 09:39:42 +01005477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5478
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005479 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5480 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5481 {
5482 /* Check if we have seen a ChangeCipherSpec before.
5483 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005484 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005485 {
5486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5487 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005488 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005489 }
5490
Hanno Becker39b8bc92018-08-28 17:17:13 +01005491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005492 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5493 ssl->in_msglen = 1;
5494 ssl->in_msg[0] = 1;
5495
5496 /* As long as they are equal, the exact value doesn't matter. */
5497 ssl->in_left = 0;
5498 ssl->next_record_offset = 0;
5499
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005500 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005501 goto exit;
5502 }
Hanno Becker37f95322018-08-16 13:55:32 +01005503
Hanno Beckerb8f50142018-08-28 10:01:34 +01005504#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005505 /* Debug only */
5506 {
5507 unsigned offset;
5508 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5509 {
5510 hs_buf = &hs->buffering.hs[offset];
5511 if( hs_buf->is_valid == 1 )
5512 {
5513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5514 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005515 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005516 }
5517 }
5518 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005519#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005520
5521 /* Check if we have buffered and/or fully reassembled the
5522 * next handshake message. */
5523 hs_buf = &hs->buffering.hs[0];
5524 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5525 {
5526 /* Synthesize a record containing the buffered HS message. */
5527 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5528 ( hs_buf->data[2] << 8 ) |
5529 hs_buf->data[3];
5530
5531 /* Double-check that we haven't accidentally buffered
5532 * a message that doesn't fit into the input buffer. */
5533 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5534 {
5535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5536 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5537 }
5538
5539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5540 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5541 hs_buf->data, msg_len + 12 );
5542
5543 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5544 ssl->in_hslen = msg_len + 12;
5545 ssl->in_msglen = msg_len + 12;
5546 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5547
5548 ret = 0;
5549 goto exit;
5550 }
5551 else
5552 {
5553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5554 hs->in_msg_seq ) );
5555 }
5556
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005557 ret = -1;
5558
5559exit:
5560
5561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5562 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005563}
5564
Hanno Beckera02b0b42018-08-21 17:20:27 +01005565static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5566 size_t desired )
5567{
5568 int offset;
5569 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5571 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005572
Hanno Becker01315ea2018-08-21 17:22:17 +01005573 /* Get rid of future records epoch first, if such exist. */
5574 ssl_free_buffered_record( ssl );
5575
5576 /* Check if we have enough space available now. */
5577 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5578 hs->buffering.total_bytes_buffered ) )
5579 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005581 return( 0 );
5582 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005583
Hanno Becker4f432ad2018-08-28 10:02:32 +01005584 /* We don't have enough space to buffer the next expected handshake
5585 * message. Remove buffers used for future messages to gain space,
5586 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005587 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5588 offset >= 0; offset-- )
5589 {
5590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5591 offset ) );
5592
Hanno Beckerb309b922018-08-23 13:18:05 +01005593 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005594
5595 /* Check if we have enough space available now. */
5596 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5597 hs->buffering.total_bytes_buffered ) )
5598 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005600 return( 0 );
5601 }
5602 }
5603
5604 return( -1 );
5605}
5606
Hanno Becker40f50842018-08-15 14:48:01 +01005607static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5608{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005609 int ret = 0;
5610 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5611
5612 if( hs == NULL )
5613 return( 0 );
5614
5615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5616
5617 switch( ssl->in_msgtype )
5618 {
5619 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005621
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005622 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005623 break;
5624
5625 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005626 {
5627 unsigned recv_msg_seq_offset;
5628 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5629 mbedtls_ssl_hs_buffer *hs_buf;
5630 size_t msg_len = ssl->in_hslen - 12;
5631
5632 /* We should never receive an old handshake
5633 * message - double-check nonetheless. */
5634 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5635 {
5636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5637 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5638 }
5639
5640 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5641 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5642 {
5643 /* Silently ignore -- message too far in the future */
5644 MBEDTLS_SSL_DEBUG_MSG( 2,
5645 ( "Ignore future HS message with sequence number %u, "
5646 "buffering window %u - %u",
5647 recv_msg_seq, ssl->handshake->in_msg_seq,
5648 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5649
5650 goto exit;
5651 }
5652
5653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5654 recv_msg_seq, recv_msg_seq_offset ) );
5655
5656 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5657
5658 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005659 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005660 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005661 size_t reassembly_buf_sz;
5662
Hanno Becker37f95322018-08-16 13:55:32 +01005663 hs_buf->is_fragmented =
5664 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5665
5666 /* We copy the message back into the input buffer
5667 * after reassembly, so check that it's not too large.
5668 * This is an implementation-specific limitation
5669 * and not one from the standard, hence it is not
5670 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005671 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005672 {
5673 /* Ignore message */
5674 goto exit;
5675 }
5676
Hanno Beckere0b150f2018-08-21 15:51:03 +01005677 /* Check if we have enough space to buffer the message. */
5678 if( hs->buffering.total_bytes_buffered >
5679 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5680 {
5681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5682 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5683 }
5684
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005685 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5686 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005687
5688 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5689 hs->buffering.total_bytes_buffered ) )
5690 {
5691 if( recv_msg_seq_offset > 0 )
5692 {
5693 /* If we can't buffer a future message because
5694 * of space limitations -- ignore. */
5695 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",
5696 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5697 (unsigned) hs->buffering.total_bytes_buffered ) );
5698 goto exit;
5699 }
Hanno Beckere1801392018-08-21 16:51:05 +01005700 else
5701 {
5702 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",
5703 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5704 (unsigned) hs->buffering.total_bytes_buffered ) );
5705 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005706
Hanno Beckera02b0b42018-08-21 17:20:27 +01005707 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005708 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005709 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",
5710 (unsigned) msg_len,
5711 (unsigned) reassembly_buf_sz,
5712 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005713 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005714 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5715 goto exit;
5716 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005717 }
5718
5719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5720 msg_len ) );
5721
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005722 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5723 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005724 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005725 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005726 goto exit;
5727 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005728 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005729
5730 /* Prepare final header: copy msg_type, length and message_seq,
5731 * then add standardised fragment_offset and fragment_length */
5732 memcpy( hs_buf->data, ssl->in_msg, 6 );
5733 memset( hs_buf->data + 6, 0, 3 );
5734 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5735
5736 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005737
5738 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005739 }
5740 else
5741 {
5742 /* Make sure msg_type and length are consistent */
5743 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5744 {
5745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5746 /* Ignore */
5747 goto exit;
5748 }
5749 }
5750
Hanno Becker4422bbb2018-08-20 09:40:19 +01005751 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005752 {
5753 size_t frag_len, frag_off;
5754 unsigned char * const msg = hs_buf->data + 12;
5755
5756 /*
5757 * Check and copy current fragment
5758 */
5759
5760 /* Validation of header fields already done in
5761 * mbedtls_ssl_prepare_handshake_record(). */
5762 frag_off = ssl_get_hs_frag_off( ssl );
5763 frag_len = ssl_get_hs_frag_len( ssl );
5764
5765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5766 frag_off, frag_len ) );
5767 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5768
5769 if( hs_buf->is_fragmented )
5770 {
5771 unsigned char * const bitmask = msg + msg_len;
5772 ssl_bitmask_set( bitmask, frag_off, frag_len );
5773 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5774 msg_len ) == 0 );
5775 }
5776 else
5777 {
5778 hs_buf->is_complete = 1;
5779 }
5780
5781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5782 hs_buf->is_complete ? "" : "not yet " ) );
5783 }
5784
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005785 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005786 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005787
5788 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005789 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005790 break;
5791 }
5792
5793exit:
5794
5795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5796 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005797}
5798#endif /* MBEDTLS_SSL_PROTO_DTLS */
5799
Hanno Becker1097b342018-08-15 14:09:41 +01005800static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005801{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005802 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005803 * Consume last content-layer message and potentially
5804 * update in_msglen which keeps track of the contents'
5805 * consumption state.
5806 *
5807 * (1) Handshake messages:
5808 * Remove last handshake message, move content
5809 * and adapt in_msglen.
5810 *
5811 * (2) Alert messages:
5812 * Consume whole record content, in_msglen = 0.
5813 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005814 * (3) Change cipher spec:
5815 * Consume whole record content, in_msglen = 0.
5816 *
5817 * (4) Application data:
5818 * Don't do anything - the record layer provides
5819 * the application data as a stream transport
5820 * and consumes through mbedtls_ssl_read only.
5821 *
5822 */
5823
5824 /* Case (1): Handshake messages */
5825 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005826 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005827 /* Hard assertion to be sure that no application data
5828 * is in flight, as corrupting ssl->in_msglen during
5829 * ssl->in_offt != NULL is fatal. */
5830 if( ssl->in_offt != NULL )
5831 {
5832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5833 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5834 }
5835
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005836 /*
5837 * Get next Handshake message in the current record
5838 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005839
Hanno Becker4a810fb2017-05-24 16:27:30 +01005840 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005841 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005842 * current handshake content: If DTLS handshake
5843 * fragmentation is used, that's the fragment
5844 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005845 * size here is faulty and should be changed at
5846 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005847 * (2) While it doesn't seem to cause problems, one
5848 * has to be very careful not to assume that in_hslen
5849 * is always <= in_msglen in a sensible communication.
5850 * Again, it's wrong for DTLS handshake fragmentation.
5851 * The following check is therefore mandatory, and
5852 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005853 * Additionally, ssl->in_hslen might be arbitrarily out of
5854 * bounds after handling a DTLS message with an unexpected
5855 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005856 */
5857 if( ssl->in_hslen < ssl->in_msglen )
5858 {
5859 ssl->in_msglen -= ssl->in_hslen;
5860 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5861 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005862
Hanno Becker4a810fb2017-05-24 16:27:30 +01005863 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5864 ssl->in_msg, ssl->in_msglen );
5865 }
5866 else
5867 {
5868 ssl->in_msglen = 0;
5869 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005870
Hanno Becker4a810fb2017-05-24 16:27:30 +01005871 ssl->in_hslen = 0;
5872 }
5873 /* Case (4): Application data */
5874 else if( ssl->in_offt != NULL )
5875 {
5876 return( 0 );
5877 }
5878 /* Everything else (CCS & Alerts) */
5879 else
5880 {
5881 ssl->in_msglen = 0;
5882 }
5883
Hanno Becker1097b342018-08-15 14:09:41 +01005884 return( 0 );
5885}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005886
Hanno Beckere74d5562018-08-15 14:26:08 +01005887static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5888{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005889 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005890 return( 1 );
5891
5892 return( 0 );
5893}
5894
Hanno Becker5f066e72018-08-16 14:56:31 +01005895#if defined(MBEDTLS_SSL_PROTO_DTLS)
5896
5897static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5898{
5899 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5900 if( hs == NULL )
5901 return;
5902
Hanno Becker01315ea2018-08-21 17:22:17 +01005903 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005904 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005905 hs->buffering.total_bytes_buffered -=
5906 hs->buffering.future_record.len;
5907
5908 mbedtls_free( hs->buffering.future_record.data );
5909 hs->buffering.future_record.data = NULL;
5910 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005911}
5912
5913static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5914{
5915 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5916 unsigned char * rec;
5917 size_t rec_len;
5918 unsigned rec_epoch;
5919
5920 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5921 return( 0 );
5922
5923 if( hs == NULL )
5924 return( 0 );
5925
Hanno Becker5f066e72018-08-16 14:56:31 +01005926 rec = hs->buffering.future_record.data;
5927 rec_len = hs->buffering.future_record.len;
5928 rec_epoch = hs->buffering.future_record.epoch;
5929
5930 if( rec == NULL )
5931 return( 0 );
5932
Hanno Becker4cb782d2018-08-20 11:19:05 +01005933 /* Only consider loading future records if the
5934 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005935 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005936 return( 0 );
5937
Hanno Becker5f066e72018-08-16 14:56:31 +01005938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5939
5940 if( rec_epoch != ssl->in_epoch )
5941 {
5942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5943 goto exit;
5944 }
5945
5946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5947
5948 /* Double-check that the record is not too large */
5949 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5950 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5951 {
5952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5953 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5954 }
5955
5956 memcpy( ssl->in_hdr, rec, rec_len );
5957 ssl->in_left = rec_len;
5958 ssl->next_record_offset = 0;
5959
5960 ssl_free_buffered_record( ssl );
5961
5962exit:
5963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5964 return( 0 );
5965}
5966
Hanno Becker519f15d2019-07-11 12:43:20 +01005967static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
5968 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01005969{
5970 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01005971
5972 /* Don't buffer future records outside handshakes. */
5973 if( hs == NULL )
5974 return( 0 );
5975
5976 /* Only buffer handshake records (we are only interested
5977 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01005978 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01005979 return( 0 );
5980
5981 /* Don't buffer more than one future epoch record. */
5982 if( hs->buffering.future_record.data != NULL )
5983 return( 0 );
5984
Hanno Becker01315ea2018-08-21 17:22:17 +01005985 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01005986 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01005987 hs->buffering.total_bytes_buffered ) )
5988 {
5989 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 +01005990 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01005991 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005992 return( 0 );
5993 }
5994
Hanno Becker5f066e72018-08-16 14:56:31 +01005995 /* Buffer record */
5996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5997 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01005998 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01005999
6000 /* ssl_parse_record_header() only considers records
6001 * of the next epoch as candidates for buffering. */
6002 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006003 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006004
6005 hs->buffering.future_record.data =
6006 mbedtls_calloc( 1, hs->buffering.future_record.len );
6007 if( hs->buffering.future_record.data == NULL )
6008 {
6009 /* If we run out of RAM trying to buffer a
6010 * record from the next epoch, just ignore. */
6011 return( 0 );
6012 }
6013
Hanno Becker519f15d2019-07-11 12:43:20 +01006014 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006015
Hanno Becker519f15d2019-07-11 12:43:20 +01006016 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006017 return( 0 );
6018}
6019
6020#endif /* MBEDTLS_SSL_PROTO_DTLS */
6021
Hanno Beckere74d5562018-08-15 14:26:08 +01006022static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006023{
6024 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006025 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006026
Hanno Becker5f066e72018-08-16 14:56:31 +01006027#if defined(MBEDTLS_SSL_PROTO_DTLS)
6028 /* We might have buffered a future record; if so,
6029 * and if the epoch matches now, load it.
6030 * On success, this call will set ssl->in_left to
6031 * the length of the buffered record, so that
6032 * the calls to ssl_fetch_input() below will
6033 * essentially be no-ops. */
6034 ret = ssl_load_buffered_record( ssl );
6035 if( ret != 0 )
6036 return( ret );
6037#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006038
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006039 /* Ensure that we have enough space available for the default form
6040 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6041 * with no space for CIDs counted in). */
6042 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6043 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006046 return( ret );
6047 }
6048
Hanno Beckere5e7e782019-07-11 12:29:35 +01006049 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6050 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006053 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006054 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006055 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6056 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006057 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006058 if( ret != 0 )
6059 return( ret );
6060
6061 /* Fall through to handling of unexpected records */
6062 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6063 }
6064
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006065 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6066 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006067#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006068 /* Reset in pointers to default state for TLS/DTLS records,
6069 * assuming no CID and no offset between record content and
6070 * record plaintext. */
6071 ssl_update_in_pointers( ssl );
6072
Hanno Becker7ae20e02019-07-12 08:33:49 +01006073 /* Setup internal message pointers from record structure. */
6074 ssl->in_msgtype = rec.type;
6075#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6076 ssl->in_len = ssl->in_cid + rec.cid_len;
6077#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6078 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6079 ssl->in_msglen = rec.data_len;
6080
Hanno Becker2fddd372019-07-10 14:37:41 +01006081 ret = ssl_check_client_reconnect( ssl );
6082 if( ret != 0 )
6083 return( ret );
6084#endif
6085
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006086 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006087 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006088
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6090 "(header)" ) );
6091 }
6092 else
6093 {
6094 /* Skip invalid record and the rest of the datagram */
6095 ssl->next_record_offset = 0;
6096 ssl->in_left = 0;
6097
6098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6099 "(header)" ) );
6100 }
6101
6102 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006103 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006104 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006105 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006106#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006107 {
6108 return( ret );
6109 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006110 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006113 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006114 {
Hanno Beckera8814792019-07-10 15:01:45 +01006115 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006116 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006117 if( ssl->next_record_offset < ssl->in_left )
6118 {
6119 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6120 }
6121 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006122 else
6123#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006124 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006125 /*
6126 * Fetch record contents from underlying transport.
6127 */
Hanno Beckera3175662019-07-11 12:50:29 +01006128 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006129 if( ret != 0 )
6130 {
6131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6132 return( ret );
6133 }
6134
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006135 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006136 }
6137
6138 /*
6139 * Decrypt record contents.
6140 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006141
Hanno Beckerfdf66042019-07-11 13:07:45 +01006142 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006145 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006146 {
6147 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006148 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006149 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006150 /* Except when waiting for Finished as a bad mac here
6151 * probably means something went wrong in the handshake
6152 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6153 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6154 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6155 {
6156#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6157 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6158 {
6159 mbedtls_ssl_send_alert_message( ssl,
6160 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6161 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6162 }
6163#endif
6164 return( ret );
6165 }
6166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006168 if( ssl->conf->badmac_limit != 0 &&
6169 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6172 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006173 }
6174#endif
6175
Hanno Becker4a810fb2017-05-24 16:27:30 +01006176 /* As above, invalid records cause
6177 * dismissal of the whole datagram. */
6178
6179 ssl->next_record_offset = 0;
6180 ssl->in_left = 0;
6181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006183 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006184 }
6185
6186 return( ret );
6187 }
6188 else
6189#endif
6190 {
6191 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6193 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195 mbedtls_ssl_send_alert_message( ssl,
6196 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6197 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006198 }
6199#endif
6200 return( ret );
6201 }
6202 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006203
Hanno Becker44d89b22019-07-12 09:40:44 +01006204
6205 /* Reset in pointers to default state for TLS/DTLS records,
6206 * assuming no CID and no offset between record content and
6207 * record plaintext. */
6208 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006209#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6210 ssl->in_len = ssl->in_cid + rec.cid_len;
6211#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6212 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006213
Hanno Becker8685c822019-07-12 09:37:30 +01006214 /* The record content type may change during decryption,
6215 * so re-read it. */
6216 ssl->in_msgtype = rec.type;
6217 /* Also update the input buffer, because unfortunately
6218 * the server-side ssl_parse_client_hello() reparses the
6219 * record header when receiving a ClientHello initiating
6220 * a renegotiation. */
6221 ssl->in_hdr[0] = rec.type;
6222 ssl->in_msg = rec.buf + rec.data_offset;
6223 ssl->in_msglen = rec.data_len;
6224 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6225 ssl->in_len[1] = (unsigned char)( rec.data_len );
6226
Simon Butcher99000142016-10-13 17:21:01 +01006227 return( 0 );
6228}
6229
6230int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6231{
6232 int ret;
6233
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006234 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006235 * Handle particular types of records
6236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006238 {
Simon Butcher99000142016-10-13 17:21:01 +01006239 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6240 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006241 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006243 }
6244
Hanno Beckere678eaa2018-08-21 14:57:46 +01006245 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006246 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006247 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006248 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6250 ssl->in_msglen ) );
6251 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006252 }
6253
Hanno Beckere678eaa2018-08-21 14:57:46 +01006254 if( ssl->in_msg[0] != 1 )
6255 {
6256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6257 ssl->in_msg[0] ) );
6258 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6259 }
6260
6261#if defined(MBEDTLS_SSL_PROTO_DTLS)
6262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6263 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6264 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6265 {
6266 if( ssl->handshake == NULL )
6267 {
6268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6269 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6270 }
6271
6272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6273 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6274 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006275#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006276 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006279 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006280 if( ssl->in_msglen != 2 )
6281 {
6282 /* Note: Standard allows for more than one 2 byte alert
6283 to be packed in a single message, but Mbed TLS doesn't
6284 currently support this. */
6285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6286 ssl->in_msglen ) );
6287 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6288 }
6289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006291 ssl->in_msg[0], ssl->in_msg[1] ) );
6292
6293 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006294 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006299 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006301 }
6302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6304 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6307 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006308 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006309
6310#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6311 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6312 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6313 {
Hanno Becker90333da2017-10-10 11:27:13 +01006314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006315 /* Will be handled when trying to parse ServerHello */
6316 return( 0 );
6317 }
6318#endif
6319
6320#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6321 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6322 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6323 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6324 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6325 {
6326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6327 /* Will be handled in mbedtls_ssl_parse_certificate() */
6328 return( 0 );
6329 }
6330#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6331
6332 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006333 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006334 }
6335
Hanno Beckerc76c6192017-06-06 10:03:17 +01006336#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006337 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006338 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006339 /* Drop unexpected ApplicationData records,
6340 * except at the beginning of renegotiations */
6341 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6342 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6343#if defined(MBEDTLS_SSL_RENEGOTIATION)
6344 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6345 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006346#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006347 )
6348 {
6349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6350 return( MBEDTLS_ERR_SSL_NON_FATAL );
6351 }
6352
6353 if( ssl->handshake != NULL &&
6354 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6355 {
6356 ssl_handshake_wrapup_free_hs_transform( ssl );
6357 }
6358 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006359#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006360
Paul Bakker5121ce52009-01-03 21:22:43 +00006361 return( 0 );
6362}
6363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006365{
6366 int ret;
6367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6369 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6370 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006371 {
6372 return( ret );
6373 }
6374
6375 return( 0 );
6376}
6377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006379 unsigned char level,
6380 unsigned char message )
6381{
6382 int ret;
6383
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006384 if( ssl == NULL || ssl->conf == NULL )
6385 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006390 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006391 ssl->out_msglen = 2;
6392 ssl->out_msg[0] = level;
6393 ssl->out_msg[1] = message;
6394
Hanno Becker67bc7c32018-08-06 11:33:50 +01006395 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006398 return( ret );
6399 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006401
6402 return( 0 );
6403}
6404
Hanno Beckerb9d44792019-02-08 07:19:04 +00006405#if defined(MBEDTLS_X509_CRT_PARSE_C)
6406static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6407{
6408#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6409 if( session->peer_cert != NULL )
6410 {
6411 mbedtls_x509_crt_free( session->peer_cert );
6412 mbedtls_free( session->peer_cert );
6413 session->peer_cert = NULL;
6414 }
6415#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6416 if( session->peer_cert_digest != NULL )
6417 {
6418 /* Zeroization is not necessary. */
6419 mbedtls_free( session->peer_cert_digest );
6420 session->peer_cert_digest = NULL;
6421 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6422 session->peer_cert_digest_len = 0;
6423 }
6424#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6425}
6426#endif /* MBEDTLS_X509_CRT_PARSE_C */
6427
Paul Bakker5121ce52009-01-03 21:22:43 +00006428/*
6429 * Handshake functions
6430 */
Hanno Becker21489932019-02-05 13:20:55 +00006431#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006432/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006434{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006435 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6436 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006439
Hanno Becker7177a882019-02-05 13:36:46 +00006440 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006443 ssl->state++;
6444 return( 0 );
6445 }
6446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6448 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006449}
6450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006452{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006453 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6454 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006457
Hanno Becker7177a882019-02-05 13:36:46 +00006458 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006461 ssl->state++;
6462 return( 0 );
6463 }
6464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6466 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006467}
Gilles Peskinef9828522017-05-03 12:28:43 +02006468
Hanno Becker21489932019-02-05 13:20:55 +00006469#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006470/* Some certificate support -> implement write and parse */
6471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006473{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006475 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006477 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6478 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006481
Hanno Becker7177a882019-02-05 13:36:46 +00006482 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006485 ssl->state++;
6486 return( 0 );
6487 }
6488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006490 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006491 {
6492 if( ssl->client_auth == 0 )
6493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006495 ssl->state++;
6496 return( 0 );
6497 }
6498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006500 /*
6501 * If using SSLv3 and got no cert, send an Alert message
6502 * (otherwise an empty Certificate message will be sent).
6503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6505 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006506 {
6507 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6509 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6510 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006513 goto write_msg;
6514 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006516 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517#endif /* MBEDTLS_SSL_CLI_C */
6518#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006519 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6524 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006525 }
6526 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006527#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006530
6531 /*
6532 * 0 . 0 handshake type
6533 * 1 . 3 handshake length
6534 * 4 . 6 length of all certs
6535 * 7 . 9 length of cert. 1
6536 * 10 . n-1 peer certificate
6537 * n . n+2 length of cert. 2
6538 * n+3 . ... upper level cert, etc.
6539 */
6540 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006542
Paul Bakker29087132010-03-21 21:03:34 +00006543 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006544 {
6545 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006546 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006549 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006551 }
6552
6553 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6554 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6555 ssl->out_msg[i + 2] = (unsigned char)( n );
6556
6557 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6558 i += n; crt = crt->next;
6559 }
6560
6561 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6562 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6563 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6564
6565 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6567 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006568
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006569#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006570write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006571#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006572
6573 ssl->state++;
6574
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006575 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006576 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006578 return( ret );
6579 }
6580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006582
Paul Bakkered27a042013-04-18 22:46:23 +02006583 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006584}
6585
Hanno Becker84879e32019-01-31 07:44:03 +00006586#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006587
6588#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006589static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6590 unsigned char *crt_buf,
6591 size_t crt_buf_len )
6592{
6593 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6594
6595 if( peer_crt == NULL )
6596 return( -1 );
6597
6598 if( peer_crt->raw.len != crt_buf_len )
6599 return( -1 );
6600
Hanno Becker46f34d02019-02-08 14:00:04 +00006601 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006602}
Hanno Becker177475a2019-02-05 17:02:46 +00006603#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6604static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6605 unsigned char *crt_buf,
6606 size_t crt_buf_len )
6607{
6608 int ret;
6609 unsigned char const * const peer_cert_digest =
6610 ssl->session->peer_cert_digest;
6611 mbedtls_md_type_t const peer_cert_digest_type =
6612 ssl->session->peer_cert_digest_type;
6613 mbedtls_md_info_t const * const digest_info =
6614 mbedtls_md_info_from_type( peer_cert_digest_type );
6615 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6616 size_t digest_len;
6617
6618 if( peer_cert_digest == NULL || digest_info == NULL )
6619 return( -1 );
6620
6621 digest_len = mbedtls_md_get_size( digest_info );
6622 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6623 return( -1 );
6624
6625 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6626 if( ret != 0 )
6627 return( -1 );
6628
6629 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6630}
6631#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006632#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006633
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006634/*
6635 * Once the certificate message is read, parse it into a cert chain and
6636 * perform basic checks, but leave actual verification to the caller
6637 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006638static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6639 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006640{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006641 int ret;
6642#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6643 int crt_cnt=0;
6644#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006645 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006646 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006651 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6652 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006654 }
6655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6657 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006660 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6661 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006663 }
6664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006666
Paul Bakker5121ce52009-01-03 21:22:43 +00006667 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006669 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006670 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006671
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006672 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006676 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6677 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006679 }
6680
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006681 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6682 i += 3;
6683
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006684 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006685 while( i < ssl->in_hslen )
6686 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006687 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006688 if ( i + 3 > ssl->in_hslen ) {
6689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006690 mbedtls_ssl_send_alert_message( ssl,
6691 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6692 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006693 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6694 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006695 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6696 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006697 if( ssl->in_msg[i] != 0 )
6698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006700 mbedtls_ssl_send_alert_message( ssl,
6701 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6702 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006704 }
6705
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006706 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006707 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6708 | (unsigned int) ssl->in_msg[i + 2];
6709 i += 3;
6710
6711 if( n < 128 || i + n > ssl->in_hslen )
6712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006714 mbedtls_ssl_send_alert_message( ssl,
6715 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6716 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006718 }
6719
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006720 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006721#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6722 if( crt_cnt++ == 0 &&
6723 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6724 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006725 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006726 /* During client-side renegotiation, check that the server's
6727 * end-CRTs hasn't changed compared to the initial handshake,
6728 * mitigating the triple handshake attack. On success, reuse
6729 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6731 if( ssl_check_peer_crt_unchanged( ssl,
6732 &ssl->in_msg[i],
6733 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006734 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6736 mbedtls_ssl_send_alert_message( ssl,
6737 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6738 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6739 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006740 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006741
6742 /* Now we can safely free the original chain. */
6743 ssl_clear_peer_cert( ssl->session );
6744 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006745#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6746
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006747 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006748#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006749 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006750#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006751 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006752 * it in-place from the input buffer instead of making a copy. */
6753 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6754#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006755 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006756 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006757 case 0: /*ok*/
6758 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6759 /* Ignore certificate with an unknown algorithm: maybe a
6760 prior certificate was already trusted. */
6761 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006762
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006763 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6764 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6765 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006766
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006767 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6768 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6769 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006770
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006771 default:
6772 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6773 crt_parse_der_failed:
6774 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6775 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6776 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006777 }
6778
6779 i += n;
6780 }
6781
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006782 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006783 return( 0 );
6784}
6785
Hanno Becker4a55f632019-02-05 12:49:06 +00006786#if defined(MBEDTLS_SSL_SRV_C)
6787static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6788{
6789 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6790 return( -1 );
6791
6792#if defined(MBEDTLS_SSL_PROTO_SSL3)
6793 /*
6794 * Check if the client sent an empty certificate
6795 */
6796 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6797 {
6798 if( ssl->in_msglen == 2 &&
6799 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6800 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6801 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6802 {
6803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6804 return( 0 );
6805 }
6806
6807 return( -1 );
6808 }
6809#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6810
6811#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6812 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6813 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6814 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6815 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6816 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6817 {
6818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6819 return( 0 );
6820 }
6821
6822 return( -1 );
6823#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6824 MBEDTLS_SSL_PROTO_TLS1_2 */
6825}
6826#endif /* MBEDTLS_SSL_SRV_C */
6827
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006828/* Check if a certificate message is expected.
6829 * Return either
6830 * - SSL_CERTIFICATE_EXPECTED, or
6831 * - SSL_CERTIFICATE_SKIP
6832 * indicating whether a Certificate message is expected or not.
6833 */
6834#define SSL_CERTIFICATE_EXPECTED 0
6835#define SSL_CERTIFICATE_SKIP 1
6836static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6837 int authmode )
6838{
6839 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006840 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006841
6842 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6843 return( SSL_CERTIFICATE_SKIP );
6844
6845#if defined(MBEDTLS_SSL_SRV_C)
6846 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6847 {
6848 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6849 return( SSL_CERTIFICATE_SKIP );
6850
6851 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6852 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006853 ssl->session_negotiate->verify_result =
6854 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6855 return( SSL_CERTIFICATE_SKIP );
6856 }
6857 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006858#else
6859 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006860#endif /* MBEDTLS_SSL_SRV_C */
6861
6862 return( SSL_CERTIFICATE_EXPECTED );
6863}
6864
Hanno Becker68636192019-02-05 14:36:34 +00006865static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6866 int authmode,
6867 mbedtls_x509_crt *chain,
6868 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006869{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006870 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006871 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006872 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006873 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006874
Hanno Becker8927c832019-04-03 12:52:50 +01006875 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6876 void *p_vrfy;
6877
Hanno Becker68636192019-02-05 14:36:34 +00006878 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6879 return( 0 );
6880
Hanno Becker8927c832019-04-03 12:52:50 +01006881 if( ssl->f_vrfy != NULL )
6882 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006884 f_vrfy = ssl->f_vrfy;
6885 p_vrfy = ssl->p_vrfy;
6886 }
6887 else
6888 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006890 f_vrfy = ssl->conf->f_vrfy;
6891 p_vrfy = ssl->conf->p_vrfy;
6892 }
6893
Hanno Becker68636192019-02-05 14:36:34 +00006894 /*
6895 * Main check: verify certificate
6896 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006897#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6898 if( ssl->conf->f_ca_cb != NULL )
6899 {
6900 ((void) rs_ctx);
6901 have_ca_chain = 1;
6902
6903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006904 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006905 chain,
6906 ssl->conf->f_ca_cb,
6907 ssl->conf->p_ca_cb,
6908 ssl->conf->cert_profile,
6909 ssl->hostname,
6910 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006911 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006912 }
6913 else
6914#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6915 {
6916 mbedtls_x509_crt *ca_chain;
6917 mbedtls_x509_crl *ca_crl;
6918
6919#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6920 if( ssl->handshake->sni_ca_chain != NULL )
6921 {
6922 ca_chain = ssl->handshake->sni_ca_chain;
6923 ca_crl = ssl->handshake->sni_ca_crl;
6924 }
6925 else
6926#endif
6927 {
6928 ca_chain = ssl->conf->ca_chain;
6929 ca_crl = ssl->conf->ca_crl;
6930 }
6931
6932 if( ca_chain != NULL )
6933 have_ca_chain = 1;
6934
6935 ret = mbedtls_x509_crt_verify_restartable(
6936 chain,
6937 ca_chain, ca_crl,
6938 ssl->conf->cert_profile,
6939 ssl->hostname,
6940 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006941 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006942 }
Hanno Becker68636192019-02-05 14:36:34 +00006943
6944 if( ret != 0 )
6945 {
6946 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6947 }
6948
6949#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6950 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6951 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6952#endif
6953
6954 /*
6955 * Secondary checks: always done, but change 'ret' only if it was 0
6956 */
6957
6958#if defined(MBEDTLS_ECP_C)
6959 {
6960 const mbedtls_pk_context *pk = &chain->pk;
6961
6962 /* If certificate uses an EC key, make sure the curve is OK */
6963 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6964 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6965 {
6966 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6967
6968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6969 if( ret == 0 )
6970 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6971 }
6972 }
6973#endif /* MBEDTLS_ECP_C */
6974
6975 if( mbedtls_ssl_check_cert_usage( chain,
6976 ciphersuite_info,
6977 ! ssl->conf->endpoint,
6978 &ssl->session_negotiate->verify_result ) != 0 )
6979 {
6980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6981 if( ret == 0 )
6982 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6983 }
6984
6985 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6986 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6987 * with details encoded in the verification flags. All other kinds
6988 * of error codes, including those from the user provided f_vrfy
6989 * functions, are treated as fatal and lead to a failure of
6990 * ssl_parse_certificate even if verification was optional. */
6991 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6992 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6993 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6994 {
6995 ret = 0;
6996 }
6997
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006998 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006999 {
7000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7001 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7002 }
7003
7004 if( ret != 0 )
7005 {
7006 uint8_t alert;
7007
7008 /* The certificate may have been rejected for several reasons.
7009 Pick one and send the corresponding alert. Which alert to send
7010 may be a subject of debate in some cases. */
7011 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7012 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7013 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7014 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7015 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7016 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7017 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7018 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7019 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7020 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7021 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7022 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7023 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7024 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7025 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7026 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7027 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7028 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7029 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7030 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7031 else
7032 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7033 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7034 alert );
7035 }
7036
7037#if defined(MBEDTLS_DEBUG_C)
7038 if( ssl->session_negotiate->verify_result != 0 )
7039 {
7040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7041 ssl->session_negotiate->verify_result ) );
7042 }
7043 else
7044 {
7045 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7046 }
7047#endif /* MBEDTLS_DEBUG_C */
7048
7049 return( ret );
7050}
7051
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007052#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7053static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7054 unsigned char *start, size_t len )
7055{
7056 int ret;
7057 /* Remember digest of the peer's end-CRT. */
7058 ssl->session_negotiate->peer_cert_digest =
7059 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7060 if( ssl->session_negotiate->peer_cert_digest == NULL )
7061 {
7062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7063 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7064 mbedtls_ssl_send_alert_message( ssl,
7065 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7066 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7067
7068 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7069 }
7070
7071 ret = mbedtls_md( mbedtls_md_info_from_type(
7072 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7073 start, len,
7074 ssl->session_negotiate->peer_cert_digest );
7075
7076 ssl->session_negotiate->peer_cert_digest_type =
7077 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7078 ssl->session_negotiate->peer_cert_digest_len =
7079 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7080
7081 return( ret );
7082}
7083
7084static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7085 unsigned char *start, size_t len )
7086{
7087 unsigned char *end = start + len;
7088 int ret;
7089
7090 /* Make a copy of the peer's raw public key. */
7091 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7092 ret = mbedtls_pk_parse_subpubkey( &start, end,
7093 &ssl->handshake->peer_pubkey );
7094 if( ret != 0 )
7095 {
7096 /* We should have parsed the public key before. */
7097 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7098 }
7099
7100 return( 0 );
7101}
7102#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7103
Hanno Becker68636192019-02-05 14:36:34 +00007104int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7105{
7106 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007107 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007108#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7109 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7110 ? ssl->handshake->sni_authmode
7111 : ssl->conf->authmode;
7112#else
7113 const int authmode = ssl->conf->authmode;
7114#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007115 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007116 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007117
7118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7119
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007120 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7121 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007122 {
7123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007124 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007125 }
7126
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007127#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7128 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007129 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007130 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007131 chain = ssl->handshake->ecrs_peer_cert;
7132 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007133 goto crt_verify;
7134 }
7135#endif
7136
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007137 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007138 {
7139 /* mbedtls_ssl_read_record may have sent an alert already. We
7140 let it decide whether to alert. */
7141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007142 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007143 }
7144
Hanno Becker4a55f632019-02-05 12:49:06 +00007145#if defined(MBEDTLS_SSL_SRV_C)
7146 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7147 {
7148 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007149
7150 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007151 ret = 0;
7152 else
7153 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007154
Hanno Becker6bdfab22019-02-05 13:11:17 +00007155 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007156 }
7157#endif /* MBEDTLS_SSL_SRV_C */
7158
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007159 /* Clear existing peer CRT structure in case we tried to
7160 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007161 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007162
7163 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7164 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007165 {
7166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7167 sizeof( mbedtls_x509_crt ) ) );
7168 mbedtls_ssl_send_alert_message( ssl,
7169 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7170 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007171
Hanno Becker3dad3112019-02-05 17:19:52 +00007172 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7173 goto exit;
7174 }
7175 mbedtls_x509_crt_init( chain );
7176
7177 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007178 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007179 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007180
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007181#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7182 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007183 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007184
7185crt_verify:
7186 if( ssl->handshake->ecrs_enabled)
7187 rs_ctx = &ssl->handshake->ecrs_ctx;
7188#endif
7189
Hanno Becker68636192019-02-05 14:36:34 +00007190 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007191 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007192 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007193 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007194
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007195#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007196 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007197 unsigned char *crt_start, *pk_start;
7198 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007199
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007200 /* We parse the CRT chain without copying, so
7201 * these pointers point into the input buffer,
7202 * and are hence still valid after freeing the
7203 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007204
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007205 crt_start = chain->raw.p;
7206 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007207
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007208 pk_start = chain->pk_raw.p;
7209 pk_len = chain->pk_raw.len;
7210
7211 /* Free the CRT structures before computing
7212 * digest and copying the peer's public key. */
7213 mbedtls_x509_crt_free( chain );
7214 mbedtls_free( chain );
7215 chain = NULL;
7216
7217 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007218 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007219 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007220
7221 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7222 if( ret != 0 )
7223 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007224 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007225#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7226 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007227 ssl->session_negotiate->peer_cert = chain;
7228 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007229#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007232
Hanno Becker6bdfab22019-02-05 13:11:17 +00007233exit:
7234
Hanno Becker3dad3112019-02-05 17:19:52 +00007235 if( ret == 0 )
7236 ssl->state++;
7237
7238#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7239 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7240 {
7241 ssl->handshake->ecrs_peer_cert = chain;
7242 chain = NULL;
7243 }
7244#endif
7245
7246 if( chain != NULL )
7247 {
7248 mbedtls_x509_crt_free( chain );
7249 mbedtls_free( chain );
7250 }
7251
Paul Bakker5121ce52009-01-03 21:22:43 +00007252 return( ret );
7253}
Hanno Becker21489932019-02-05 13:20:55 +00007254#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007257{
7258 int ret;
7259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007263 ssl->out_msglen = 1;
7264 ssl->out_msg[0] = 1;
7265
Paul Bakker5121ce52009-01-03 21:22:43 +00007266 ssl->state++;
7267
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007268 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007269 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007271 return( ret );
7272 }
7273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007275
7276 return( 0 );
7277}
7278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007280{
7281 int ret;
7282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007284
Hanno Becker327c93b2018-08-15 13:56:18 +01007285 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007288 return( ret );
7289 }
7290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007294 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7295 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007296 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007297 }
7298
Hanno Beckere678eaa2018-08-21 14:57:46 +01007299 /* CCS records are only accepted if they have length 1 and content '1',
7300 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007301
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007302 /*
7303 * Switch to our negotiated transform and session parameters for inbound
7304 * data.
7305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007307 ssl->transform_in = ssl->transform_negotiate;
7308 ssl->session_in = ssl->session_negotiate;
7309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007314 ssl_dtls_replay_reset( ssl );
7315#endif
7316
7317 /* Increment epoch */
7318 if( ++ssl->in_epoch == 0 )
7319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007321 /* This is highly unlikely to happen for legitimate reasons, so
7322 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007324 }
7325 }
7326 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007328 memset( ssl->in_ctr, 0, 8 );
7329
Hanno Becker79594fd2019-05-08 09:38:41 +01007330 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7333 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007338 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7339 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007341 }
7342 }
7343#endif
7344
Paul Bakker5121ce52009-01-03 21:22:43 +00007345 ssl->state++;
7346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007348
7349 return( 0 );
7350}
7351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7353 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007354{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007355 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7358 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7359 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007360 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007361 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7364#if defined(MBEDTLS_SHA512_C)
7365 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007366 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7367 else
7368#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369#if defined(MBEDTLS_SHA256_C)
7370 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007371 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007372 else
7373#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007377 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007378 }
Paul Bakker380da532012-04-18 16:10:25 +00007379}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007382{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7384 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007385 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7386 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007387#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7389#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007390#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007391 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007392 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7393#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007394 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007395#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007396#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007398#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007399 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007400 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007401#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007402 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007403#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007404#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007406}
7407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007409 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007410{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7412 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007413 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7414 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007415#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7417#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007418#if defined(MBEDTLS_USE_PSA_CRYPTO)
7419 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7420#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007421 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007422#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007423#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007425#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007426 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007427#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007428 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007429#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007432}
7433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7435 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7436static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007437 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007438{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007439 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7440 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007441}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007442#endif
Paul Bakker380da532012-04-18 16:10:25 +00007443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7445#if defined(MBEDTLS_SHA256_C)
7446static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007447 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007448{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007449#if defined(MBEDTLS_USE_PSA_CRYPTO)
7450 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7451#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007452 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007453#endif
Paul Bakker380da532012-04-18 16:10:25 +00007454}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007455#endif
Paul Bakker380da532012-04-18 16:10:25 +00007456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457#if defined(MBEDTLS_SHA512_C)
7458static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007459 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007460{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007461#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007462 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007463#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007464 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007465#endif
Paul Bakker380da532012-04-18 16:10:25 +00007466}
Paul Bakker769075d2012-11-24 11:26:46 +01007467#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007471static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007473{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007474 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007475 mbedtls_md5_context md5;
7476 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007477
Paul Bakker5121ce52009-01-03 21:22:43 +00007478 unsigned char padbuf[48];
7479 unsigned char md5sum[16];
7480 unsigned char sha1sum[20];
7481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007483 if( !session )
7484 session = ssl->session;
7485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007487
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007488 mbedtls_md5_init( &md5 );
7489 mbedtls_sha1_init( &sha1 );
7490
7491 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7492 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007493
7494 /*
7495 * SSLv3:
7496 * hash =
7497 * MD5( master + pad2 +
7498 * MD5( handshake + sender + master + pad1 ) )
7499 * + SHA1( master + pad2 +
7500 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007501 */
7502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007504 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7505 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007506#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007509 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7510 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007511#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007514 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007515
Paul Bakker1ef83d62012-04-11 12:09:53 +00007516 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007517
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007518 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7519 mbedtls_md5_update_ret( &md5, session->master, 48 );
7520 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7521 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007522
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007523 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7524 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7525 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7526 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007527
Paul Bakker1ef83d62012-04-11 12:09:53 +00007528 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007529
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007530 mbedtls_md5_starts_ret( &md5 );
7531 mbedtls_md5_update_ret( &md5, session->master, 48 );
7532 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7533 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7534 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007535
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007536 mbedtls_sha1_starts_ret( &sha1 );
7537 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7538 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7539 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7540 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007543
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007544 mbedtls_md5_free( &md5 );
7545 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007546
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007547 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7548 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7549 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007556static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007558{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007559 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007560 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007561 mbedtls_md5_context md5;
7562 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007563 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007566 if( !session )
7567 session = ssl->session;
7568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007570
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007571 mbedtls_md5_init( &md5 );
7572 mbedtls_sha1_init( &sha1 );
7573
7574 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7575 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007576
Paul Bakker1ef83d62012-04-11 12:09:53 +00007577 /*
7578 * TLSv1:
7579 * hash = PRF( master, finished_label,
7580 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7581 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007584 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7585 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007586#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007589 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7590 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007591#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007594 ? "client finished"
7595 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007596
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007597 mbedtls_md5_finish_ret( &md5, padbuf );
7598 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007599
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007600 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007601 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007604
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007605 mbedtls_md5_free( &md5 );
7606 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007607
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007608 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7615#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007616static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007618{
7619 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007620 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007621 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007622#if defined(MBEDTLS_USE_PSA_CRYPTO)
7623 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007624 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007625 psa_status_t status;
7626#else
7627 mbedtls_sha256_context sha256;
7628#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007631 if( !session )
7632 session = ssl->session;
7633
Andrzej Kurekeb342242019-01-29 09:14:33 -05007634 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7635 ? "client finished"
7636 : "server finished";
7637
7638#if defined(MBEDTLS_USE_PSA_CRYPTO)
7639 sha256_psa = psa_hash_operation_init();
7640
7641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7642
7643 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7644 if( status != PSA_SUCCESS )
7645 {
7646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7647 return;
7648 }
7649
7650 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7651 if( status != PSA_SUCCESS )
7652 {
7653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7654 return;
7655 }
7656 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7657#else
7658
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007659 mbedtls_sha256_init( &sha256 );
7660
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007662
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007663 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007664
7665 /*
7666 * TLSv1.2:
7667 * hash = PRF( master, finished_label,
7668 * Hash( handshake ) )[0.11]
7669 */
7670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671#if !defined(MBEDTLS_SHA256_ALT)
7672 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007673 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007674#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007675
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007676 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007677 mbedtls_sha256_free( &sha256 );
7678#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007679
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007680 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007681 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007684
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007685 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007692static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007694{
7695 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007696 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007697 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007698#if defined(MBEDTLS_USE_PSA_CRYPTO)
7699 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007700 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007701 psa_status_t status;
7702#else
7703 mbedtls_sha512_context sha512;
7704#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007707 if( !session )
7708 session = ssl->session;
7709
Andrzej Kurekeb342242019-01-29 09:14:33 -05007710 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7711 ? "client finished"
7712 : "server finished";
7713
7714#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007715 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007716
7717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7718
Andrzej Kurek972fba52019-01-30 03:29:12 -05007719 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007720 if( status != PSA_SUCCESS )
7721 {
7722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7723 return;
7724 }
7725
Andrzej Kurek972fba52019-01-30 03:29:12 -05007726 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007727 if( status != PSA_SUCCESS )
7728 {
7729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7730 return;
7731 }
7732 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7733#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007734 mbedtls_sha512_init( &sha512 );
7735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007737
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007738 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007739
7740 /*
7741 * TLSv1.2:
7742 * hash = PRF( master, finished_label,
7743 * Hash( handshake ) )[0.11]
7744 */
7745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007746#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007747 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7748 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007749#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007750
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007751 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007752 mbedtls_sha512_free( &sha512 );
7753#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007754
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007755 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007756 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007759
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007760 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007763}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764#endif /* MBEDTLS_SHA512_C */
7765#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007768{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007770
7771 /*
7772 * Free our handshake params
7773 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007774 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007776 ssl->handshake = NULL;
7777
7778 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007779 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007780 */
7781 if( ssl->transform )
7782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007783 mbedtls_ssl_transform_free( ssl->transform );
7784 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007785 }
7786 ssl->transform = ssl->transform_negotiate;
7787 ssl->transform_negotiate = NULL;
7788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007790}
7791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007793{
7794 int resume = ssl->handshake->resume;
7795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798#if defined(MBEDTLS_SSL_RENEGOTIATION)
7799 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007802 ssl->renego_records_seen = 0;
7803 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007804#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007805
7806 /*
7807 * Free the previous session and switch in the current one
7808 */
Paul Bakker0a597072012-09-25 21:55:46 +00007809 if( ssl->session )
7810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007812 /* RFC 7366 3.1: keep the EtM state */
7813 ssl->session_negotiate->encrypt_then_mac =
7814 ssl->session->encrypt_then_mac;
7815#endif
7816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817 mbedtls_ssl_session_free( ssl->session );
7818 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007819 }
7820 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007821 ssl->session_negotiate = NULL;
7822
Paul Bakker0a597072012-09-25 21:55:46 +00007823 /*
7824 * Add cache entry
7825 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007826 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007827 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007828 resume == 0 )
7829 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007830 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007832 }
Paul Bakker0a597072012-09-25 21:55:46 +00007833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007835 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007836 ssl->handshake->flight != NULL )
7837 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007838 /* Cancel handshake timer */
7839 ssl_set_timer( ssl, 0 );
7840
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007841 /* Keep last flight around in case we need to resend it:
7842 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007844 }
7845 else
7846#endif
7847 ssl_handshake_wrapup_free_hs_transform( ssl );
7848
Paul Bakker48916f92012-09-16 19:57:18 +00007849 ssl->state++;
7850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007852}
7853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007855{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007856 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007859
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007860 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007861
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007862 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007863
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007864 /*
7865 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7866 * may define some other value. Currently (early 2016), no defined
7867 * ciphersuite does this (and this is unlikely to change as activity has
7868 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007873 ssl->verify_data_len = hash_len;
7874 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007875#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007876
Paul Bakker5121ce52009-01-03 21:22:43 +00007877 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7879 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007880
7881 /*
7882 * In case of session resuming, invert the client and server
7883 * ChangeCipherSpec messages order.
7884 */
Paul Bakker0a597072012-09-25 21:55:46 +00007885 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007888 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007890#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007892 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007894#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007895 }
7896 else
7897 ssl->state++;
7898
Paul Bakker48916f92012-09-16 19:57:18 +00007899 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007900 * Switch to our negotiated transform and session parameters for outbound
7901 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007906 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007907 {
7908 unsigned char i;
7909
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007910 /* Remember current epoch settings for resending */
7911 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007912 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007913
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007914 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007915 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007916
7917 /* Increment epoch */
7918 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007919 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007920 break;
7921
7922 /* The loop goes to its end iff the counter is wrapping */
7923 if( i == 0 )
7924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7926 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007927 }
7928 }
7929 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007931 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007932
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007933 ssl->transform_out = ssl->transform_negotiate;
7934 ssl->session_out = ssl->session_negotiate;
7935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7937 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7942 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007943 }
7944 }
7945#endif
7946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007948 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007950#endif
7951
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007952 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007953 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007954 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007955 return( ret );
7956 }
7957
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007958#if defined(MBEDTLS_SSL_PROTO_DTLS)
7959 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7960 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7961 {
7962 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7963 return( ret );
7964 }
7965#endif
7966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007968
7969 return( 0 );
7970}
7971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007973#define SSL_MAX_HASH_LEN 36
7974#else
7975#define SSL_MAX_HASH_LEN 12
7976#endif
7977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007979{
Paul Bakker23986e52011-04-24 08:57:21 +00007980 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007981 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007982 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007985
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007986 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007987
Hanno Becker327c93b2018-08-15 13:56:18 +01007988 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007991 return( ret );
7992 }
7993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007997 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7998 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008000 }
8001
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008002 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003#if defined(MBEDTLS_SSL_PROTO_SSL3)
8004 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008005 hash_len = 36;
8006 else
8007#endif
8008 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8011 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008014 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8015 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008017 }
8018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008020 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008023 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8024 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008026 }
8027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008029 ssl->verify_data_len = hash_len;
8030 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008031#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008032
Paul Bakker0a597072012-09-25 21:55:46 +00008033 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008036 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008040 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008043 }
8044 else
8045 ssl->state++;
8046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008047#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008048 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008049 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008050#endif
8051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008053
8054 return( 0 );
8055}
8056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008057static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008058{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8062 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8063 mbedtls_md5_init( &handshake->fin_md5 );
8064 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008065 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8066 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8069#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008070#if defined(MBEDTLS_USE_PSA_CRYPTO)
8071 handshake->fin_sha256_psa = psa_hash_operation_init();
8072 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8073#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008075 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008076#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008078#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008079#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008080 handshake->fin_sha384_psa = psa_hash_operation_init();
8081 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008082#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008084 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008085#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008088
8089 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008090
8091#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8092 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8093 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8094#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096#if defined(MBEDTLS_DHM_C)
8097 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099#if defined(MBEDTLS_ECDH_C)
8100 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008101#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008102#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008103 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008104#if defined(MBEDTLS_SSL_CLI_C)
8105 handshake->ecjpake_cache = NULL;
8106 handshake->ecjpake_cache_len = 0;
8107#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008108#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008109
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008110#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008111 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008112#endif
8113
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008114#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8115 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8116#endif
Hanno Becker75173122019-02-06 16:18:31 +00008117
8118#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8119 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8120 mbedtls_pk_init( &handshake->peer_pubkey );
8121#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008122}
8123
Hanno Beckera18d1322018-01-03 14:27:32 +00008124void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8129 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008130
Hanno Beckerd56ed242018-01-03 15:32:51 +00008131#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132 mbedtls_md_init( &transform->md_ctx_enc );
8133 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008134#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008135}
8136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008138{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008140}
8141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008143{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008144 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008145 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008147 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008149 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008150 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008151
8152 /*
8153 * Either the pointers are now NULL or cleared properly and can be freed.
8154 * Now allocate missing structures.
8155 */
8156 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008157 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008158 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008159 }
Paul Bakker48916f92012-09-16 19:57:18 +00008160
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008161 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008162 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008163 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008164 }
Paul Bakker48916f92012-09-16 19:57:18 +00008165
Paul Bakker82788fb2014-10-20 13:59:19 +02008166 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008167 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008168 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008169 }
Paul Bakker48916f92012-09-16 19:57:18 +00008170
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008171 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008172 if( ssl->handshake == NULL ||
8173 ssl->transform_negotiate == NULL ||
8174 ssl->session_negotiate == NULL )
8175 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178 mbedtls_free( ssl->handshake );
8179 mbedtls_free( ssl->transform_negotiate );
8180 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008181
8182 ssl->handshake = NULL;
8183 ssl->transform_negotiate = NULL;
8184 ssl->session_negotiate = NULL;
8185
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008186 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008187 }
8188
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008189 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008191 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008192 ssl_handshake_params_init( ssl->handshake );
8193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008195 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8196 {
8197 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008198
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008199 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8200 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8201 else
8202 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008203
8204 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008205 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008206#endif
8207
Paul Bakker48916f92012-09-16 19:57:18 +00008208 return( 0 );
8209}
8210
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008211#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008212/* Dummy cookie callbacks for defaults */
8213static int ssl_cookie_write_dummy( void *ctx,
8214 unsigned char **p, unsigned char *end,
8215 const unsigned char *cli_id, size_t cli_id_len )
8216{
8217 ((void) ctx);
8218 ((void) p);
8219 ((void) end);
8220 ((void) cli_id);
8221 ((void) cli_id_len);
8222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008224}
8225
8226static int ssl_cookie_check_dummy( void *ctx,
8227 const unsigned char *cookie, size_t cookie_len,
8228 const unsigned char *cli_id, size_t cli_id_len )
8229{
8230 ((void) ctx);
8231 ((void) cookie);
8232 ((void) cookie_len);
8233 ((void) cli_id);
8234 ((void) cli_id_len);
8235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008237}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008238#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008239
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008240/* Once ssl->out_hdr as the address of the beginning of the
8241 * next outgoing record is set, deduce the other pointers.
8242 *
8243 * Note: For TLS, we save the implicit record sequence number
8244 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8245 * and the caller has to make sure there's space for this.
8246 */
8247
8248static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8249 mbedtls_ssl_transform *transform )
8250{
8251#if defined(MBEDTLS_SSL_PROTO_DTLS)
8252 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8253 {
8254 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008255#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008256 ssl->out_cid = ssl->out_ctr + 8;
8257 ssl->out_len = ssl->out_cid;
8258 if( transform != NULL )
8259 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008260#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008261 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008262#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008263 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008264 }
8265 else
8266#endif
8267 {
8268 ssl->out_ctr = ssl->out_hdr - 8;
8269 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008270#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008271 ssl->out_cid = ssl->out_len;
8272#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008273 ssl->out_iv = ssl->out_hdr + 5;
8274 }
8275
8276 /* Adjust out_msg to make space for explicit IV, if used. */
8277 if( transform != NULL &&
8278 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8279 {
8280 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8281 }
8282 else
8283 ssl->out_msg = ssl->out_iv;
8284}
8285
8286/* Once ssl->in_hdr as the address of the beginning of the
8287 * next incoming record is set, deduce the other pointers.
8288 *
8289 * Note: For TLS, we save the implicit record sequence number
8290 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8291 * and the caller has to make sure there's space for this.
8292 */
8293
Hanno Becker79594fd2019-05-08 09:38:41 +01008294static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008295{
Hanno Becker79594fd2019-05-08 09:38:41 +01008296 /* This function sets the pointers to match the case
8297 * of unprotected TLS/DTLS records, with both ssl->in_iv
8298 * and ssl->in_msg pointing to the beginning of the record
8299 * content.
8300 *
8301 * When decrypting a protected record, ssl->in_msg
8302 * will be shifted to point to the beginning of the
8303 * record plaintext.
8304 */
8305
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008306#if defined(MBEDTLS_SSL_PROTO_DTLS)
8307 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8308 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008309 /* This sets the header pointers to match records
8310 * without CID. When we receive a record containing
8311 * a CID, the fields are shifted accordingly in
8312 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008313 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008314#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008315 ssl->in_cid = ssl->in_ctr + 8;
8316 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008317#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008318 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008319#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008320 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008321 }
8322 else
8323#endif
8324 {
8325 ssl->in_ctr = ssl->in_hdr - 8;
8326 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008327#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008328 ssl->in_cid = ssl->in_len;
8329#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008330 ssl->in_iv = ssl->in_hdr + 5;
8331 }
8332
Hanno Becker79594fd2019-05-08 09:38:41 +01008333 /* This will be adjusted at record decryption time. */
8334 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008335}
8336
Paul Bakker5121ce52009-01-03 21:22:43 +00008337/*
8338 * Initialize an SSL context
8339 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008340void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8341{
8342 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8343}
8344
8345/*
8346 * Setup an SSL context
8347 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008348
8349static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8350{
8351 /* Set the incoming and outgoing record pointers. */
8352#if defined(MBEDTLS_SSL_PROTO_DTLS)
8353 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8354 {
8355 ssl->out_hdr = ssl->out_buf;
8356 ssl->in_hdr = ssl->in_buf;
8357 }
8358 else
8359#endif /* MBEDTLS_SSL_PROTO_DTLS */
8360 {
8361 ssl->out_hdr = ssl->out_buf + 8;
8362 ssl->in_hdr = ssl->in_buf + 8;
8363 }
8364
8365 /* Derive other internal pointers. */
8366 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008367 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008368}
8369
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008370int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008371 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008372{
Paul Bakker48916f92012-09-16 19:57:18 +00008373 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008374
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008375 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008376
8377 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008378 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008379 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008380
8381 /* Set to NULL in case of an error condition */
8382 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008383
Angus Grattond8213d02016-05-25 20:56:48 +10008384 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8385 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008386 {
Angus Grattond8213d02016-05-25 20:56:48 +10008387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008388 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008389 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008390 }
8391
8392 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8393 if( ssl->out_buf == NULL )
8394 {
8395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008396 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008397 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008398 }
8399
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008400 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008401
Paul Bakker48916f92012-09-16 19:57:18 +00008402 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008403 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008404
8405 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008406
8407error:
8408 mbedtls_free( ssl->in_buf );
8409 mbedtls_free( ssl->out_buf );
8410
8411 ssl->conf = NULL;
8412
8413 ssl->in_buf = NULL;
8414 ssl->out_buf = NULL;
8415
8416 ssl->in_hdr = NULL;
8417 ssl->in_ctr = NULL;
8418 ssl->in_len = NULL;
8419 ssl->in_iv = NULL;
8420 ssl->in_msg = NULL;
8421
8422 ssl->out_hdr = NULL;
8423 ssl->out_ctr = NULL;
8424 ssl->out_len = NULL;
8425 ssl->out_iv = NULL;
8426 ssl->out_msg = NULL;
8427
k-stachowiak9f7798e2018-07-31 16:52:32 +02008428 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008429}
8430
8431/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008432 * Reset an initialized and used SSL context for re-use while retaining
8433 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008434 *
8435 * If partial is non-zero, keep data in the input buffer and client ID.
8436 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008437 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008438static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008439{
Paul Bakker48916f92012-09-16 19:57:18 +00008440 int ret;
8441
Hanno Becker7e772132018-08-10 12:38:21 +01008442#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8443 !defined(MBEDTLS_SSL_SRV_C)
8444 ((void) partial);
8445#endif
8446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008448
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008449 /* Cancel any possibly running timer */
8450 ssl_set_timer( ssl, 0 );
8451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452#if defined(MBEDTLS_SSL_RENEGOTIATION)
8453 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008454 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008455
8456 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8458 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008459#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008461
Paul Bakker7eb013f2011-10-06 12:37:39 +00008462 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008463 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008464
8465 ssl->in_msgtype = 0;
8466 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008468 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008469 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008470#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008471#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008472 ssl_dtls_replay_reset( ssl );
8473#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008474
8475 ssl->in_hslen = 0;
8476 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008477
8478 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008479
8480 ssl->out_msgtype = 0;
8481 ssl->out_msglen = 0;
8482 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8484 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008485 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008486#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008487
Hanno Becker19859472018-08-06 09:40:20 +01008488 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8489
Paul Bakker48916f92012-09-16 19:57:18 +00008490 ssl->transform_in = NULL;
8491 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008492
Hanno Becker78640902018-08-13 16:35:15 +01008493 ssl->session_in = NULL;
8494 ssl->session_out = NULL;
8495
Angus Grattond8213d02016-05-25 20:56:48 +10008496 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008497
8498#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008499 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008500#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8501 {
8502 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008503 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008504 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8507 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8510 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8513 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008514 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008515 }
8516#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008517
Paul Bakker48916f92012-09-16 19:57:18 +00008518 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520 mbedtls_ssl_transform_free( ssl->transform );
8521 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008522 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008523 }
Paul Bakker48916f92012-09-16 19:57:18 +00008524
Paul Bakkerc0463502013-02-14 11:19:38 +01008525 if( ssl->session )
8526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527 mbedtls_ssl_session_free( ssl->session );
8528 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008529 ssl->session = NULL;
8530 }
8531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008532#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008533 ssl->alpn_chosen = NULL;
8534#endif
8535
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008536#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008537#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008538 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008539#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008540 {
8541 mbedtls_free( ssl->cli_id );
8542 ssl->cli_id = NULL;
8543 ssl->cli_id_len = 0;
8544 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008545#endif
8546
Paul Bakker48916f92012-09-16 19:57:18 +00008547 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8548 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008549
8550 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008551}
8552
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008553/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008554 * Reset an initialized and used SSL context for re-use while retaining
8555 * all application-set variables, function pointers and data.
8556 */
8557int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8558{
8559 return( ssl_session_reset_int( ssl, 0 ) );
8560}
8561
8562/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008563 * SSL set accessors
8564 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008565void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008566{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008567 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008568}
8569
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008570void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008571{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008572 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008573}
8574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008576void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008577{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008578 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008579}
8580#endif
8581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008583void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008585 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008586}
8587#endif
8588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008590
Hanno Becker1841b0a2018-08-24 11:13:57 +01008591void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8592 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008593{
8594 ssl->disable_datagram_packing = !allow_packing;
8595}
8596
8597void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8598 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008599{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008600 conf->hs_timeout_min = min;
8601 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008602}
8603#endif
8604
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008605void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008606{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008607 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008608}
8609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008611void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008612 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008613 void *p_vrfy )
8614{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008615 conf->f_vrfy = f_vrfy;
8616 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008617}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008619
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008620void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008621 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008622 void *p_rng )
8623{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008624 conf->f_rng = f_rng;
8625 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008626}
8627
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008628void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008629 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008630 void *p_dbg )
8631{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008632 conf->f_dbg = f_dbg;
8633 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008634}
8635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008637 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008638 mbedtls_ssl_send_t *f_send,
8639 mbedtls_ssl_recv_t *f_recv,
8640 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008641{
8642 ssl->p_bio = p_bio;
8643 ssl->f_send = f_send;
8644 ssl->f_recv = f_recv;
8645 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008646}
8647
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008648#if defined(MBEDTLS_SSL_PROTO_DTLS)
8649void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8650{
8651 ssl->mtu = mtu;
8652}
8653#endif
8654
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008655void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008656{
8657 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008658}
8659
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008660void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8661 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008662 mbedtls_ssl_set_timer_t *f_set_timer,
8663 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008664{
8665 ssl->p_timer = p_timer;
8666 ssl->f_set_timer = f_set_timer;
8667 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008668
8669 /* Make sure we start with no timer running */
8670 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008671}
8672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008674void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008675 void *p_cache,
8676 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8677 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008678{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008679 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008680 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008681 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008683#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685#if defined(MBEDTLS_SSL_CLI_C)
8686int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008687{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008688 int ret;
8689
8690 if( ssl == NULL ||
8691 session == NULL ||
8692 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008693 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008696 }
8697
Hanno Becker52055ae2019-02-06 14:30:46 +00008698 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8699 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008700 return( ret );
8701
Paul Bakker0a597072012-09-25 21:55:46 +00008702 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008703
8704 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008707
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008708void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008709 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008710{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008711 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8712 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8713 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8714 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008715}
8716
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008717void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008718 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008719 int major, int minor )
8720{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008722 return;
8723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008725 return;
8726
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008727 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008728}
8729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008731void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008732 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008733{
8734 conf->cert_profile = profile;
8735}
8736
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008737/* Append a new keycert entry to a (possibly empty) list */
8738static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8739 mbedtls_x509_crt *cert,
8740 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008741{
niisato8ee24222018-06-25 19:05:48 +09008742 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008743
niisato8ee24222018-06-25 19:05:48 +09008744 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8745 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008746 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008747
niisato8ee24222018-06-25 19:05:48 +09008748 new_cert->cert = cert;
8749 new_cert->key = key;
8750 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008751
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008752 /* Update head is the list was null, else add to the end */
8753 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008754 {
niisato8ee24222018-06-25 19:05:48 +09008755 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008756 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008757 else
8758 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008759 mbedtls_ssl_key_cert *cur = *head;
8760 while( cur->next != NULL )
8761 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008762 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008763 }
8764
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008765 return( 0 );
8766}
8767
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008768int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008769 mbedtls_x509_crt *own_cert,
8770 mbedtls_pk_context *pk_key )
8771{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008772 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008773}
8774
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008775void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008776 mbedtls_x509_crt *ca_chain,
8777 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008778{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008779 conf->ca_chain = ca_chain;
8780 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008781
8782#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8783 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8784 * cannot be used together. */
8785 conf->f_ca_cb = NULL;
8786 conf->p_ca_cb = NULL;
8787#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008788}
Hanno Becker5adaad92019-03-27 16:54:37 +00008789
8790#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8791void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008792 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008793 void *p_ca_cb )
8794{
8795 conf->f_ca_cb = f_ca_cb;
8796 conf->p_ca_cb = p_ca_cb;
8797
8798 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8799 * cannot be used together. */
8800 conf->ca_chain = NULL;
8801 conf->ca_crl = NULL;
8802}
8803#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008805
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008806#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8807int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8808 mbedtls_x509_crt *own_cert,
8809 mbedtls_pk_context *pk_key )
8810{
8811 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8812 own_cert, pk_key ) );
8813}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008814
8815void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8816 mbedtls_x509_crt *ca_chain,
8817 mbedtls_x509_crl *ca_crl )
8818{
8819 ssl->handshake->sni_ca_chain = ca_chain;
8820 ssl->handshake->sni_ca_crl = ca_crl;
8821}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008822
8823void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8824 int authmode )
8825{
8826 ssl->handshake->sni_authmode = authmode;
8827}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008828#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8829
Hanno Becker8927c832019-04-03 12:52:50 +01008830#if defined(MBEDTLS_X509_CRT_PARSE_C)
8831void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8832 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8833 void *p_vrfy )
8834{
8835 ssl->f_vrfy = f_vrfy;
8836 ssl->p_vrfy = p_vrfy;
8837}
8838#endif
8839
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008840#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008841/*
8842 * Set EC J-PAKE password for current handshake
8843 */
8844int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8845 const unsigned char *pw,
8846 size_t pw_len )
8847{
8848 mbedtls_ecjpake_role role;
8849
Janos Follath8eb64132016-06-03 15:40:57 +01008850 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8852
8853 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8854 role = MBEDTLS_ECJPAKE_SERVER;
8855 else
8856 role = MBEDTLS_ECJPAKE_CLIENT;
8857
8858 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8859 role,
8860 MBEDTLS_MD_SHA256,
8861 MBEDTLS_ECP_DP_SECP256R1,
8862 pw, pw_len ) );
8863}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008864#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008866#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008867
8868static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8869{
8870 /* Remove reference to existing PSK, if any. */
8871#if defined(MBEDTLS_USE_PSA_CRYPTO)
8872 if( conf->psk_opaque != 0 )
8873 {
8874 /* The maintenance of the PSK key slot is the
8875 * user's responsibility. */
8876 conf->psk_opaque = 0;
8877 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008878 /* This and the following branch should never
8879 * be taken simultaenously as we maintain the
8880 * invariant that raw and opaque PSKs are never
8881 * configured simultaneously. As a safeguard,
8882 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008883#endif /* MBEDTLS_USE_PSA_CRYPTO */
8884 if( conf->psk != NULL )
8885 {
8886 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8887
8888 mbedtls_free( conf->psk );
8889 conf->psk = NULL;
8890 conf->psk_len = 0;
8891 }
8892
8893 /* Remove reference to PSK identity, if any. */
8894 if( conf->psk_identity != NULL )
8895 {
8896 mbedtls_free( conf->psk_identity );
8897 conf->psk_identity = NULL;
8898 conf->psk_identity_len = 0;
8899 }
8900}
8901
Hanno Becker7390c712018-11-15 13:33:04 +00008902/* This function assumes that PSK identity in the SSL config is unset.
8903 * It checks that the provided identity is well-formed and attempts
8904 * to make a copy of it in the SSL config.
8905 * On failure, the PSK identity in the config remains unset. */
8906static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8907 unsigned char const *psk_identity,
8908 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008909{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008910 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008911 if( psk_identity == NULL ||
8912 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008913 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008914 {
8915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8916 }
8917
Hanno Becker7390c712018-11-15 13:33:04 +00008918 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8919 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008920 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008921
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008922 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008923 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008924
8925 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008926}
8927
Hanno Becker7390c712018-11-15 13:33:04 +00008928int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8929 const unsigned char *psk, size_t psk_len,
8930 const unsigned char *psk_identity, size_t psk_identity_len )
8931{
8932 int ret;
8933 /* Remove opaque/raw PSK + PSK Identity */
8934 ssl_conf_remove_psk( conf );
8935
8936 /* Check and set raw PSK */
8937 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8938 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8939 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8940 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8941 conf->psk_len = psk_len;
8942 memcpy( conf->psk, psk, conf->psk_len );
8943
8944 /* Check and set PSK Identity */
8945 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8946 if( ret != 0 )
8947 ssl_conf_remove_psk( conf );
8948
8949 return( ret );
8950}
8951
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008952static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8953{
8954#if defined(MBEDTLS_USE_PSA_CRYPTO)
8955 if( ssl->handshake->psk_opaque != 0 )
8956 {
8957 ssl->handshake->psk_opaque = 0;
8958 }
8959 else
8960#endif /* MBEDTLS_USE_PSA_CRYPTO */
8961 if( ssl->handshake->psk != NULL )
8962 {
8963 mbedtls_platform_zeroize( ssl->handshake->psk,
8964 ssl->handshake->psk_len );
8965 mbedtls_free( ssl->handshake->psk );
8966 ssl->handshake->psk_len = 0;
8967 }
8968}
8969
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008970int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8971 const unsigned char *psk, size_t psk_len )
8972{
8973 if( psk == NULL || ssl->handshake == NULL )
8974 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8975
8976 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8978
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008979 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008980
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008981 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008982 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008983
8984 ssl->handshake->psk_len = psk_len;
8985 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8986
8987 return( 0 );
8988}
8989
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008990#if defined(MBEDTLS_USE_PSA_CRYPTO)
8991int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008992 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008993 const unsigned char *psk_identity,
8994 size_t psk_identity_len )
8995{
Hanno Becker7390c712018-11-15 13:33:04 +00008996 int ret;
8997 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008998 ssl_conf_remove_psk( conf );
8999
Hanno Becker7390c712018-11-15 13:33:04 +00009000 /* Check and set opaque PSK */
9001 if( psk_slot == 0 )
9002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009003 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009004
9005 /* Check and set PSK Identity */
9006 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9007 psk_identity_len );
9008 if( ret != 0 )
9009 ssl_conf_remove_psk( conf );
9010
9011 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009012}
9013
9014int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009015 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009016{
9017 if( psk_slot == 0 || ssl->handshake == NULL )
9018 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9019
9020 ssl_remove_psk( ssl );
9021 ssl->handshake->psk_opaque = psk_slot;
9022 return( 0 );
9023}
9024#endif /* MBEDTLS_USE_PSA_CRYPTO */
9025
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009026void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009027 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009028 size_t),
9029 void *p_psk )
9030{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009031 conf->f_psk = f_psk;
9032 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009033}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009035
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009036#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009037
9038#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009039int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009040{
9041 int ret;
9042
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009043 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9044 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9045 {
9046 mbedtls_mpi_free( &conf->dhm_P );
9047 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009048 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009049 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009050
9051 return( 0 );
9052}
Hanno Becker470a8c42017-10-04 15:28:46 +01009053#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009054
Hanno Beckera90658f2017-10-04 15:29:08 +01009055int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9056 const unsigned char *dhm_P, size_t P_len,
9057 const unsigned char *dhm_G, size_t G_len )
9058{
9059 int ret;
9060
9061 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9062 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9063 {
9064 mbedtls_mpi_free( &conf->dhm_P );
9065 mbedtls_mpi_free( &conf->dhm_G );
9066 return( ret );
9067 }
9068
9069 return( 0 );
9070}
Paul Bakker5121ce52009-01-03 21:22:43 +00009071
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009072int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009073{
9074 int ret;
9075
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009076 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9077 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9078 {
9079 mbedtls_mpi_free( &conf->dhm_P );
9080 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009081 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009082 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009083
9084 return( 0 );
9085}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009086#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009087
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009088#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9089/*
9090 * Set the minimum length for Diffie-Hellman parameters
9091 */
9092void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9093 unsigned int bitlen )
9094{
9095 conf->dhm_min_bitlen = bitlen;
9096}
9097#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9098
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009099#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009100/*
9101 * Set allowed/preferred hashes for handshake signatures
9102 */
9103void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9104 const int *hashes )
9105{
9106 conf->sig_hashes = hashes;
9107}
Hanno Becker947194e2017-04-07 13:25:49 +01009108#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009109
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009110#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009111/*
9112 * Set the allowed elliptic curves
9113 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009114void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009115 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009116{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009117 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009118}
Hanno Becker947194e2017-04-07 13:25:49 +01009119#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009120
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009121#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009122int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009123{
Hanno Becker947194e2017-04-07 13:25:49 +01009124 /* Initialize to suppress unnecessary compiler warning */
9125 size_t hostname_len = 0;
9126
9127 /* Check if new hostname is valid before
9128 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009129 if( hostname != NULL )
9130 {
9131 hostname_len = strlen( hostname );
9132
9133 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9134 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9135 }
9136
9137 /* Now it's clear that we will overwrite the old hostname,
9138 * so we can free it safely */
9139
9140 if( ssl->hostname != NULL )
9141 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009142 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009143 mbedtls_free( ssl->hostname );
9144 }
9145
9146 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009147
Paul Bakker5121ce52009-01-03 21:22:43 +00009148 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009149 {
9150 ssl->hostname = NULL;
9151 }
9152 else
9153 {
9154 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009155 if( ssl->hostname == NULL )
9156 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009157
Hanno Becker947194e2017-04-07 13:25:49 +01009158 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009159
Hanno Becker947194e2017-04-07 13:25:49 +01009160 ssl->hostname[hostname_len] = '\0';
9161 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009162
9163 return( 0 );
9164}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009165#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009166
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009167#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009168void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009169 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009170 const unsigned char *, size_t),
9171 void *p_sni )
9172{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009173 conf->f_sni = f_sni;
9174 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009178#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009179int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009180{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009181 size_t cur_len, tot_len;
9182 const char **p;
9183
9184 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009185 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9186 * MUST NOT be truncated."
9187 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009188 */
9189 tot_len = 0;
9190 for( p = protos; *p != NULL; p++ )
9191 {
9192 cur_len = strlen( *p );
9193 tot_len += cur_len;
9194
9195 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009197 }
9198
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009199 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009200
9201 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009202}
9203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009204const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009205{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009206 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009207}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009209
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009210void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009211{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009212 conf->max_major_ver = major;
9213 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009214}
9215
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009216void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009217{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009218 conf->min_major_ver = major;
9219 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009220}
9221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009222#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009223void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009224{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009225 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009226}
9227#endif
9228
Janos Follath088ce432017-04-10 12:42:31 +01009229#if defined(MBEDTLS_SSL_SRV_C)
9230void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9231 char cert_req_ca_list )
9232{
9233 conf->cert_req_ca_list = cert_req_ca_list;
9234}
9235#endif
9236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009237#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009238void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009239{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009240 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009241}
9242#endif
9243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009244#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009245void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009246{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009247 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009248}
9249#endif
9250
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009251#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009252void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009253{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009254 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009255}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009256#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009258#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009259int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009262 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009265 }
9266
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009267 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009268
9269 return( 0 );
9270}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009271#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009273#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009274void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009275{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009276 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009277}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009280#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009281void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009282{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009283 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009284}
9285#endif
9286
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009287void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009288{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009289 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009290}
9291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009293void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009294{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009295 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009296}
9297
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009298void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009300 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009301}
9302
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009303void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009304 const unsigned char period[8] )
9305{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009306 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009311#if defined(MBEDTLS_SSL_CLI_C)
9312void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009313{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009314 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009315}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009316#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009317
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009318#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009319void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9320 mbedtls_ssl_ticket_write_t *f_ticket_write,
9321 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9322 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009323{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009324 conf->f_ticket_write = f_ticket_write;
9325 conf->f_ticket_parse = f_ticket_parse;
9326 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009327}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009328#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009330
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009331#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9332void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9333 mbedtls_ssl_export_keys_t *f_export_keys,
9334 void *p_export_keys )
9335{
9336 conf->f_export_keys = f_export_keys;
9337 conf->p_export_keys = p_export_keys;
9338}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009339
9340void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9341 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9342 void *p_export_keys )
9343{
9344 conf->f_export_keys_ext = f_export_keys_ext;
9345 conf->p_export_keys = p_export_keys;
9346}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009347#endif
9348
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009349#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009350void mbedtls_ssl_conf_async_private_cb(
9351 mbedtls_ssl_config *conf,
9352 mbedtls_ssl_async_sign_t *f_async_sign,
9353 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9354 mbedtls_ssl_async_resume_t *f_async_resume,
9355 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009356 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009357{
9358 conf->f_async_sign_start = f_async_sign;
9359 conf->f_async_decrypt_start = f_async_decrypt;
9360 conf->f_async_resume = f_async_resume;
9361 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009362 conf->p_async_config_data = async_config_data;
9363}
9364
Gilles Peskine8f97af72018-04-26 11:46:10 +02009365void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9366{
9367 return( conf->p_async_config_data );
9368}
9369
Gilles Peskine1febfef2018-04-30 11:54:39 +02009370void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009371{
9372 if( ssl->handshake == NULL )
9373 return( NULL );
9374 else
9375 return( ssl->handshake->user_async_ctx );
9376}
9377
Gilles Peskine1febfef2018-04-30 11:54:39 +02009378void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009379 void *ctx )
9380{
9381 if( ssl->handshake != NULL )
9382 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009383}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009384#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009385
Paul Bakker5121ce52009-01-03 21:22:43 +00009386/*
9387 * SSL get accessors
9388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009389size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009390{
9391 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9392}
9393
Hanno Becker8b170a02017-10-10 11:51:19 +01009394int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9395{
9396 /*
9397 * Case A: We're currently holding back
9398 * a message for further processing.
9399 */
9400
9401 if( ssl->keep_current_message == 1 )
9402 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009404 return( 1 );
9405 }
9406
9407 /*
9408 * Case B: Further records are pending in the current datagram.
9409 */
9410
9411#if defined(MBEDTLS_SSL_PROTO_DTLS)
9412 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9413 ssl->in_left > ssl->next_record_offset )
9414 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009415 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009416 return( 1 );
9417 }
9418#endif /* MBEDTLS_SSL_PROTO_DTLS */
9419
9420 /*
9421 * Case C: A handshake message is being processed.
9422 */
9423
Hanno Becker8b170a02017-10-10 11:51:19 +01009424 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9425 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009426 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009427 return( 1 );
9428 }
9429
9430 /*
9431 * Case D: An application data message is being processed
9432 */
9433 if( ssl->in_offt != NULL )
9434 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009436 return( 1 );
9437 }
9438
9439 /*
9440 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009441 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009442 * we implement support for multiple alerts in single records.
9443 */
9444
9445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9446 return( 0 );
9447}
9448
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009449uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009450{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009451 if( ssl->session != NULL )
9452 return( ssl->session->verify_result );
9453
9454 if( ssl->session_negotiate != NULL )
9455 return( ssl->session_negotiate->verify_result );
9456
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009457 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009458}
9459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009460const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009461{
Paul Bakker926c8e42013-03-06 10:23:34 +01009462 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009463 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009466}
9467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009469{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009471 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009472 {
9473 switch( ssl->minor_ver )
9474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009476 return( "DTLSv1.0" );
9477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009479 return( "DTLSv1.2" );
9480
9481 default:
9482 return( "unknown (DTLS)" );
9483 }
9484 }
9485#endif
9486
Paul Bakker43ca69c2011-01-15 17:35:19 +00009487 switch( ssl->minor_ver )
9488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009489 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009490 return( "SSLv3.0" );
9491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009493 return( "TLSv1.0" );
9494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009496 return( "TLSv1.1" );
9497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009499 return( "TLSv1.2" );
9500
Paul Bakker43ca69c2011-01-15 17:35:19 +00009501 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009502 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009503 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009504}
9505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009507{
Hanno Becker3136ede2018-08-17 15:28:19 +01009508 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009510 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009511
Hanno Becker5903de42019-05-03 14:46:38 +01009512 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9513
Hanno Becker78640902018-08-13 16:35:15 +01009514 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009515 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517#if defined(MBEDTLS_ZLIB_SUPPORT)
9518 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9519 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009520#endif
9521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524 case MBEDTLS_MODE_GCM:
9525 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009526 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009528 transform_expansion = transform->minlen;
9529 break;
9530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009532
9533 block_size = mbedtls_cipher_get_block_size(
9534 &transform->cipher_ctx_enc );
9535
Hanno Becker3136ede2018-08-17 15:28:19 +01009536 /* Expansion due to the addition of the MAC. */
9537 transform_expansion += transform->maclen;
9538
9539 /* Expansion due to the addition of CBC padding;
9540 * Theoretically up to 256 bytes, but we never use
9541 * more than the block size of the underlying cipher. */
9542 transform_expansion += block_size;
9543
9544 /* For TLS 1.1 or higher, an explicit IV is added
9545 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009546#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9547 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009548 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009549#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009550
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009551 break;
9552
9553 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009556 }
9557
Hanno Beckera0e20d02019-05-15 14:03:01 +01009558#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009559 if( transform->out_cid_len != 0 )
9560 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009561#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009562
Hanno Becker5903de42019-05-03 14:46:38 +01009563 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009564}
9565
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009566#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9567size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9568{
9569 size_t max_len;
9570
9571 /*
9572 * Assume mfl_code is correct since it was checked when set
9573 */
Angus Grattond8213d02016-05-25 20:56:48 +10009574 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009575
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009576 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009577 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009578 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009579 {
Angus Grattond8213d02016-05-25 20:56:48 +10009580 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009581 }
9582
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009583 /* During a handshake, use the value being negotiated */
9584 if( ssl->session_negotiate != NULL &&
9585 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9586 {
9587 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9588 }
9589
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009590 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009591}
9592#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9593
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009594#if defined(MBEDTLS_SSL_PROTO_DTLS)
9595static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9596{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009597 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9598 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9599 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9600 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9601 return ( 0 );
9602
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009603 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9604 return( ssl->mtu );
9605
9606 if( ssl->mtu == 0 )
9607 return( ssl->handshake->mtu );
9608
9609 return( ssl->mtu < ssl->handshake->mtu ?
9610 ssl->mtu : ssl->handshake->mtu );
9611}
9612#endif /* MBEDTLS_SSL_PROTO_DTLS */
9613
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009614int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9615{
9616 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9617
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009618#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9619 !defined(MBEDTLS_SSL_PROTO_DTLS)
9620 (void) ssl;
9621#endif
9622
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009623#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9624 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9625
9626 if( max_len > mfl )
9627 max_len = mfl;
9628#endif
9629
9630#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009631 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009632 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009633 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009634 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9635 const size_t overhead = (size_t) ret;
9636
9637 if( ret < 0 )
9638 return( ret );
9639
9640 if( mtu <= overhead )
9641 {
9642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9643 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9644 }
9645
9646 if( max_len > mtu - overhead )
9647 max_len = mtu - overhead;
9648 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009649#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009650
Hanno Becker0defedb2018-08-10 12:35:02 +01009651#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9652 !defined(MBEDTLS_SSL_PROTO_DTLS)
9653 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009654#endif
9655
9656 return( (int) max_len );
9657}
9658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659#if defined(MBEDTLS_X509_CRT_PARSE_C)
9660const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009661{
9662 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009663 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009664
Hanno Beckere6824572019-02-07 13:18:46 +00009665#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009666 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009667#else
9668 return( NULL );
9669#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009674int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9675 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009676{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009677 if( ssl == NULL ||
9678 dst == NULL ||
9679 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009680 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009683 }
9684
Hanno Becker52055ae2019-02-06 14:30:46 +00009685 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009688
Paul Bakker5121ce52009-01-03 21:22:43 +00009689/*
Paul Bakker1961b702013-01-25 14:49:24 +01009690 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009692int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009695
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009696 if( ssl == NULL || ssl->conf == NULL )
9697 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009700 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009704 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009705 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009706#endif
9707
Paul Bakker1961b702013-01-25 14:49:24 +01009708 return( ret );
9709}
9710
9711/*
9712 * Perform the SSL handshake
9713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009715{
9716 int ret = 0;
9717
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009718 if( ssl == NULL || ssl->conf == NULL )
9719 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009723 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009725 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009726
9727 if( ret != 0 )
9728 break;
9729 }
9730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009732
9733 return( ret );
9734}
9735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736#if defined(MBEDTLS_SSL_RENEGOTIATION)
9737#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009738/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009739 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009742{
9743 int ret;
9744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009746
9747 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9749 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009750
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009751 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009752 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009754 return( ret );
9755 }
9756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009758
9759 return( 0 );
9760}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009762
9763/*
9764 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765 * - any side: calling mbedtls_ssl_renegotiate(),
9766 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9767 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009768 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009769 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009773{
9774 int ret;
9775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009777
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009778 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9779 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009780
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009781 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9782 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009784 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009786 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009787 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009788 ssl->handshake->out_msg_seq = 1;
9789 else
9790 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009791 }
9792#endif
9793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9795 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009800 return( ret );
9801 }
9802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009804
9805 return( 0 );
9806}
9807
9808/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009809 * Renegotiate current connection on client,
9810 * or request renegotiation on server
9811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009813{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009815
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009816 if( ssl == NULL || ssl->conf == NULL )
9817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009820 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009821 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9824 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009827
9828 /* Did we already try/start sending HelloRequest? */
9829 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009831
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009832 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009833 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009837 /*
9838 * On client, either start the renegotiation process or,
9839 * if already in progress, continue the handshake
9840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009845
9846 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009849 return( ret );
9850 }
9851 }
9852 else
9853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009857 return( ret );
9858 }
9859 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009861
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009862 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009863}
9864
9865/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009866 * Check record counters and renegotiate if they're above the limit.
9867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009869{
Andres AG2196c7f2016-12-15 17:01:16 +00009870 size_t ep_len = ssl_ep_len( ssl );
9871 int in_ctr_cmp;
9872 int out_ctr_cmp;
9873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9875 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009876 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009877 {
9878 return( 0 );
9879 }
9880
Andres AG2196c7f2016-12-15 17:01:16 +00009881 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9882 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009883 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009884 ssl->conf->renego_period + ep_len, 8 - ep_len );
9885
9886 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009887 {
9888 return( 0 );
9889 }
9890
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009893}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009895
9896/*
9897 * Receive application data decrypted from the SSL layer
9898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009900{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009901 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009902 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009903
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009904 if( ssl == NULL || ssl->conf == NULL )
9905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009910 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009913 return( ret );
9914
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009915 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009917 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009918 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009919 return( ret );
9920 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009921 }
9922#endif
9923
Hanno Becker4a810fb2017-05-24 16:27:30 +01009924 /*
9925 * Check if renegotiation is necessary and/or handshake is
9926 * in process. If yes, perform/continue, and fall through
9927 * if an unexpected packet is received while the client
9928 * is waiting for the ServerHello.
9929 *
9930 * (There is no equivalent to the last condition on
9931 * the server-side as it is not treated as within
9932 * a handshake while waiting for the ClientHello
9933 * after a renegotiation request.)
9934 */
9935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009936#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009937 ret = ssl_check_ctr_renegotiate( ssl );
9938 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9939 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009941 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009942 return( ret );
9943 }
9944#endif
9945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009946 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009949 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9950 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009953 return( ret );
9954 }
9955 }
9956
Hanno Beckere41158b2017-10-23 13:30:32 +01009957 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009958 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009959 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009960 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009961 if( ssl->f_get_timer != NULL &&
9962 ssl->f_get_timer( ssl->p_timer ) == -1 )
9963 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009964 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009965 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009966
Hanno Becker327c93b2018-08-15 13:56:18 +01009967 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009968 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009969 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9970 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009971
Hanno Becker4a810fb2017-05-24 16:27:30 +01009972 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9973 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009974 }
9975
9976 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009978 {
9979 /*
9980 * OpenSSL sends empty messages to randomize the IV
9981 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009982 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009985 return( 0 );
9986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009987 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009988 return( ret );
9989 }
9990 }
9991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009992 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009995
Hanno Becker4a810fb2017-05-24 16:27:30 +01009996 /*
9997 * - For client-side, expect SERVER_HELLO_REQUEST.
9998 * - For server-side, expect CLIENT_HELLO.
9999 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10000 */
10001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010002#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010003 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010004 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010005 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010008
10009 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010010#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010011 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010012 {
10013 continue;
10014 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010015#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010016 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010017 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010018#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010019
Hanno Becker4a810fb2017-05-24 16:27:30 +010010020#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010021 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010022 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010025
10026 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010027#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010028 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010029 {
10030 continue;
10031 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010032#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010034 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010035#endif /* MBEDTLS_SSL_SRV_C */
10036
Hanno Becker21df7f92017-10-17 11:03:26 +010010037#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010038 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010039 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10040 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10041 ssl->conf->allow_legacy_renegotiation ==
10042 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10043 {
10044 /*
10045 * Accept renegotiation request
10046 */
Paul Bakker48916f92012-09-16 19:57:18 +000010047
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010048 /* DTLS clients need to know renego is server-initiated */
10049#if defined(MBEDTLS_SSL_PROTO_DTLS)
10050 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10051 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10052 {
10053 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10054 }
10055#endif
10056 ret = ssl_start_renegotiation( ssl );
10057 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10058 ret != 0 )
10059 {
10060 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10061 return( ret );
10062 }
10063 }
10064 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010065#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010066 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010067 /*
10068 * Refuse renegotiation
10069 */
10070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010073#if defined(MBEDTLS_SSL_PROTO_SSL3)
10074 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010075 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010076 /* SSLv3 does not have a "no_renegotiation" warning, so
10077 we send a fatal alert and abort the connection. */
10078 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10079 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10080 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010081 }
10082 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10084#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10085 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10086 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10089 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10090 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010091 {
10092 return( ret );
10093 }
Paul Bakker48916f92012-09-16 19:57:18 +000010094 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010095 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10097 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10100 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010101 }
Paul Bakker48916f92012-09-16 19:57:18 +000010102 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010103
Hanno Becker90333da2017-10-10 11:27:13 +010010104 /* At this point, we don't know whether the renegotiation has been
10105 * completed or not. The cases to consider are the following:
10106 * 1) The renegotiation is complete. In this case, no new record
10107 * has been read yet.
10108 * 2) The renegotiation is incomplete because the client received
10109 * an application data record while awaiting the ServerHello.
10110 * 3) The renegotiation is incomplete because the client received
10111 * a non-handshake, non-application data message while awaiting
10112 * the ServerHello.
10113 * In each of these case, looping will be the proper action:
10114 * - For 1), the next iteration will read a new record and check
10115 * if it's application data.
10116 * - For 2), the loop condition isn't satisfied as application data
10117 * is present, hence continue is the same as break
10118 * - For 3), the loop condition is satisfied and read_record
10119 * will re-deliver the message that was held back by the client
10120 * when expecting the ServerHello.
10121 */
10122 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010123 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010124#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010125 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010126 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010127 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010128 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010129 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010132 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010134 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010135 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010136 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010137#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010139 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10140 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010143 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010144 }
10145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10149 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010150 }
10151
10152 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010153
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010154 /* We're going to return something now, cancel timer,
10155 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010156 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010157 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010158
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010159#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010160 /* If we requested renego but received AppData, resend HelloRequest.
10161 * Do it now, after setting in_offt, to avoid taking this branch
10162 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010163#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010164 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010165 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010166 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010167 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010170 return( ret );
10171 }
10172 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010173#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010174#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010175 }
10176
10177 n = ( len < ssl->in_msglen )
10178 ? len : ssl->in_msglen;
10179
10180 memcpy( buf, ssl->in_offt, n );
10181 ssl->in_msglen -= n;
10182
10183 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010184 {
10185 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010186 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010187 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010188 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010189 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010190 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010191 /* more data available */
10192 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010193 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010196
Paul Bakker23986e52011-04-24 08:57:21 +000010197 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010198}
10199
10200/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010201 * Send application data to be encrypted by the SSL layer, taking care of max
10202 * fragment length and buffer size.
10203 *
10204 * According to RFC 5246 Section 6.2.1:
10205 *
10206 * Zero-length fragments of Application data MAY be sent as they are
10207 * potentially useful as a traffic analysis countermeasure.
10208 *
10209 * Therefore, it is possible that the input message length is 0 and the
10210 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010211 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010212static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010213 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010214{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010215 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10216 const size_t max_len = (size_t) ret;
10217
10218 if( ret < 0 )
10219 {
10220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10221 return( ret );
10222 }
10223
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010224 if( len > max_len )
10225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010227 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010230 "maximum fragment length: %d > %d",
10231 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010233 }
10234 else
10235#endif
10236 len = max_len;
10237 }
Paul Bakker887bd502011-06-08 13:10:54 +000010238
Paul Bakker5121ce52009-01-03 21:22:43 +000010239 if( ssl->out_left != 0 )
10240 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010241 /*
10242 * The user has previously tried to send the data and
10243 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10244 * written. In this case, we expect the high-level write function
10245 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010250 return( ret );
10251 }
10252 }
Paul Bakker887bd502011-06-08 13:10:54 +000010253 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010254 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010255 /*
10256 * The user is trying to send a message the first time, so we need to
10257 * copy the data into the internal buffers and setup the data structure
10258 * to keep track of partial writes
10259 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010260 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010262 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010263
Hanno Becker67bc7c32018-08-06 11:33:50 +010010264 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010267 return( ret );
10268 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010269 }
10270
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010271 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010272}
10273
10274/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010275 * Write application data, doing 1/n-1 splitting if necessary.
10276 *
10277 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010278 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010279 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010281#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010282static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010283 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010284{
10285 int ret;
10286
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010287 if( ssl->conf->cbc_record_splitting ==
10288 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010289 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010290 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10291 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10292 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010293 {
10294 return( ssl_write_real( ssl, buf, len ) );
10295 }
10296
10297 if( ssl->split_done == 0 )
10298 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010299 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010300 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010301 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010302 }
10303
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010304 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10305 return( ret );
10306 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010307
10308 return( ret + 1 );
10309}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010310#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010311
10312/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010313 * Write application data (public-facing wrapper)
10314 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010315int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010316{
10317 int ret;
10318
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010320
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010321 if( ssl == NULL || ssl->conf == NULL )
10322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10323
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010324#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010325 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10326 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010327 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010328 return( ret );
10329 }
10330#endif
10331
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010332 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010333 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010334 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010335 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010337 return( ret );
10338 }
10339 }
10340
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010341#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010342 ret = ssl_write_split( ssl, buf, len );
10343#else
10344 ret = ssl_write_real( ssl, buf, len );
10345#endif
10346
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010348
10349 return( ret );
10350}
10351
10352/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010353 * Notify the peer that the connection is being closed
10354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010356{
10357 int ret;
10358
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010359 if( ssl == NULL || ssl->conf == NULL )
10360 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010363
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010364 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010369 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10370 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10371 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010374 return( ret );
10375 }
10376 }
10377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010379
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010380 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010381}
10382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010383void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010384{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010385 if( transform == NULL )
10386 return;
10387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010388#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010389 deflateEnd( &transform->ctx_deflate );
10390 inflateEnd( &transform->ctx_inflate );
10391#endif
10392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010393 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10394 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010395
Hanno Beckerd56ed242018-01-03 15:32:51 +000010396#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010397 mbedtls_md_free( &transform->md_ctx_enc );
10398 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010399#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010400
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010401 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010402}
10403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404#if defined(MBEDTLS_X509_CRT_PARSE_C)
10405static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010406{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010408
10409 while( cur != NULL )
10410 {
10411 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010413 cur = next;
10414 }
10415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010416#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010417
Hanno Becker0271f962018-08-16 13:23:47 +010010418#if defined(MBEDTLS_SSL_PROTO_DTLS)
10419
10420static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10421{
10422 unsigned offset;
10423 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10424
10425 if( hs == NULL )
10426 return;
10427
Hanno Becker283f5ef2018-08-24 09:34:47 +010010428 ssl_free_buffered_record( ssl );
10429
Hanno Becker0271f962018-08-16 13:23:47 +010010430 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010431 ssl_buffering_free_slot( ssl, offset );
10432}
10433
10434static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10435 uint8_t slot )
10436{
10437 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10438 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010439
10440 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10441 return;
10442
Hanno Beckere605b192018-08-21 15:59:07 +010010443 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010444 {
Hanno Beckere605b192018-08-21 15:59:07 +010010445 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010446 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010447 mbedtls_free( hs_buf->data );
10448 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010449 }
10450}
10451
10452#endif /* MBEDTLS_SSL_PROTO_DTLS */
10453
Gilles Peskine9b562d52018-04-25 20:32:43 +020010454void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010455{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010456 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10457
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010458 if( handshake == NULL )
10459 return;
10460
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010461#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10462 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10463 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010464 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010465 handshake->async_in_progress = 0;
10466 }
10467#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10468
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010469#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10470 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10471 mbedtls_md5_free( &handshake->fin_md5 );
10472 mbedtls_sha1_free( &handshake->fin_sha1 );
10473#endif
10474#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10475#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010476#if defined(MBEDTLS_USE_PSA_CRYPTO)
10477 psa_hash_abort( &handshake->fin_sha256_psa );
10478#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010479 mbedtls_sha256_free( &handshake->fin_sha256 );
10480#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010481#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010482#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010483#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010484 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010485#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010486 mbedtls_sha512_free( &handshake->fin_sha512 );
10487#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010488#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010489#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010491#if defined(MBEDTLS_DHM_C)
10492 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010493#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494#if defined(MBEDTLS_ECDH_C)
10495 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010496#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010497#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010498 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010499#if defined(MBEDTLS_SSL_CLI_C)
10500 mbedtls_free( handshake->ecjpake_cache );
10501 handshake->ecjpake_cache = NULL;
10502 handshake->ecjpake_cache_len = 0;
10503#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010504#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010505
Janos Follath4ae5c292016-02-10 11:27:43 +000010506#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10507 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010508 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010510#endif
10511
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010512#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10513 if( handshake->psk != NULL )
10514 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010515 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010516 mbedtls_free( handshake->psk );
10517 }
10518#endif
10519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010520#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10521 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010522 /*
10523 * Free only the linked list wrapper, not the keys themselves
10524 * since the belong to the SNI callback
10525 */
10526 if( handshake->sni_key_cert != NULL )
10527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010528 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010529
10530 while( cur != NULL )
10531 {
10532 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010533 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010534 cur = next;
10535 }
10536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010537#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010538
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010539#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010540 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010541 if( handshake->ecrs_peer_cert != NULL )
10542 {
10543 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10544 mbedtls_free( handshake->ecrs_peer_cert );
10545 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010546#endif
10547
Hanno Becker75173122019-02-06 16:18:31 +000010548#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10549 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10550 mbedtls_pk_free( &handshake->peer_pubkey );
10551#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010553#if defined(MBEDTLS_SSL_PROTO_DTLS)
10554 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010555 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010556 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010557#endif
10558
Hanno Becker4a63ed42019-01-08 11:39:35 +000010559#if defined(MBEDTLS_ECDH_C) && \
10560 defined(MBEDTLS_USE_PSA_CRYPTO)
10561 psa_destroy_key( handshake->ecdh_psa_privkey );
10562#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10563
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010564 mbedtls_platform_zeroize( handshake,
10565 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010566}
10567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010568void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010569{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010570 if( session == NULL )
10571 return;
10572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010573#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010574 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010575#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010576
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010577#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010578 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010579#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010580
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010581 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010582}
10583
Paul Bakker5121ce52009-01-03 21:22:43 +000010584/*
10585 * Free an SSL context
10586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010587void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010588{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010589 if( ssl == NULL )
10590 return;
10591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010593
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010594 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010595 {
Angus Grattond8213d02016-05-25 20:56:48 +100010596 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010598 }
10599
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010600 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010601 {
Angus Grattond8213d02016-05-25 20:56:48 +100010602 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010603 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010604 }
10605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010606#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010607 if( ssl->compress_buf != NULL )
10608 {
Angus Grattond8213d02016-05-25 20:56:48 +100010609 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010610 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010611 }
10612#endif
10613
Paul Bakker48916f92012-09-16 19:57:18 +000010614 if( ssl->transform )
10615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010616 mbedtls_ssl_transform_free( ssl->transform );
10617 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010618 }
10619
10620 if( ssl->handshake )
10621 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010622 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010623 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10624 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626 mbedtls_free( ssl->handshake );
10627 mbedtls_free( ssl->transform_negotiate );
10628 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010629 }
10630
Paul Bakkerc0463502013-02-14 11:19:38 +010010631 if( ssl->session )
10632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010633 mbedtls_ssl_session_free( ssl->session );
10634 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010635 }
10636
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010637#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010638 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010639 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010640 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010641 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010642 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010643#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010645#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10646 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10649 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010650 }
10651#endif
10652
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010653#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010655#endif
10656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010658
Paul Bakker86f04f42013-02-14 11:20:09 +010010659 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010660 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010661}
10662
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010663/*
10664 * Initialze mbedtls_ssl_config
10665 */
10666void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10667{
10668 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10669}
10670
Simon Butcherc97b6972015-12-27 23:48:17 +000010671#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010672static int ssl_preset_default_hashes[] = {
10673#if defined(MBEDTLS_SHA512_C)
10674 MBEDTLS_MD_SHA512,
10675 MBEDTLS_MD_SHA384,
10676#endif
10677#if defined(MBEDTLS_SHA256_C)
10678 MBEDTLS_MD_SHA256,
10679 MBEDTLS_MD_SHA224,
10680#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010681#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010682 MBEDTLS_MD_SHA1,
10683#endif
10684 MBEDTLS_MD_NONE
10685};
Simon Butcherc97b6972015-12-27 23:48:17 +000010686#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010687
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010688static int ssl_preset_suiteb_ciphersuites[] = {
10689 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10690 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10691 0
10692};
10693
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010694#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010695static int ssl_preset_suiteb_hashes[] = {
10696 MBEDTLS_MD_SHA256,
10697 MBEDTLS_MD_SHA384,
10698 MBEDTLS_MD_NONE
10699};
10700#endif
10701
10702#if defined(MBEDTLS_ECP_C)
10703static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010704#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010705 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010706#endif
10707#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010708 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010709#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010710 MBEDTLS_ECP_DP_NONE
10711};
10712#endif
10713
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010714/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010715 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010716 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010717int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010718 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010719{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010720#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010721 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010722#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010723
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010724 /* Use the functions here so that they are covered in tests,
10725 * but otherwise access member directly for efficiency */
10726 mbedtls_ssl_conf_endpoint( conf, endpoint );
10727 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010728
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010729 /*
10730 * Things that are common to all presets
10731 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010732#if defined(MBEDTLS_SSL_CLI_C)
10733 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10734 {
10735 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10736#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10737 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10738#endif
10739 }
10740#endif
10741
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010742#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010743 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010744#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010745
10746#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10747 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10748#endif
10749
10750#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10751 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10752#endif
10753
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010754#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10755 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10756#endif
10757
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010758#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010759 conf->f_cookie_write = ssl_cookie_write_dummy;
10760 conf->f_cookie_check = ssl_cookie_check_dummy;
10761#endif
10762
10763#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10764 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10765#endif
10766
Janos Follath088ce432017-04-10 12:42:31 +010010767#if defined(MBEDTLS_SSL_SRV_C)
10768 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10769#endif
10770
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010771#if defined(MBEDTLS_SSL_PROTO_DTLS)
10772 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10773 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10774#endif
10775
10776#if defined(MBEDTLS_SSL_RENEGOTIATION)
10777 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010778 memset( conf->renego_period, 0x00, 2 );
10779 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010780#endif
10781
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010782#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10783 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10784 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010785 const unsigned char dhm_p[] =
10786 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10787 const unsigned char dhm_g[] =
10788 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10789
Hanno Beckera90658f2017-10-04 15:29:08 +010010790 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10791 dhm_p, sizeof( dhm_p ),
10792 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010793 {
10794 return( ret );
10795 }
10796 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010797#endif
10798
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010799 /*
10800 * Preset-specific defaults
10801 */
10802 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010803 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010804 /*
10805 * NSA Suite B
10806 */
10807 case MBEDTLS_SSL_PRESET_SUITEB:
10808 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10809 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10810 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10811 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10812
10813 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10814 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10815 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10816 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10817 ssl_preset_suiteb_ciphersuites;
10818
10819#if defined(MBEDTLS_X509_CRT_PARSE_C)
10820 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010821#endif
10822
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010823#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010824 conf->sig_hashes = ssl_preset_suiteb_hashes;
10825#endif
10826
10827#if defined(MBEDTLS_ECP_C)
10828 conf->curve_list = ssl_preset_suiteb_curves;
10829#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010830 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010831
10832 /*
10833 * Default
10834 */
10835 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010836 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10837 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10838 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10839 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10840 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10841 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10842 MBEDTLS_SSL_MIN_MINOR_VERSION :
10843 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010844 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10845 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10846
10847#if defined(MBEDTLS_SSL_PROTO_DTLS)
10848 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10849 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10850#endif
10851
10852 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10853 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10854 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10855 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10856 mbedtls_ssl_list_ciphersuites();
10857
10858#if defined(MBEDTLS_X509_CRT_PARSE_C)
10859 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10860#endif
10861
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010862#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010863 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010864#endif
10865
10866#if defined(MBEDTLS_ECP_C)
10867 conf->curve_list = mbedtls_ecp_grp_id_list();
10868#endif
10869
10870#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10871 conf->dhm_min_bitlen = 1024;
10872#endif
10873 }
10874
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010875 return( 0 );
10876}
10877
10878/*
10879 * Free mbedtls_ssl_config
10880 */
10881void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10882{
10883#if defined(MBEDTLS_DHM_C)
10884 mbedtls_mpi_free( &conf->dhm_P );
10885 mbedtls_mpi_free( &conf->dhm_G );
10886#endif
10887
10888#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10889 if( conf->psk != NULL )
10890 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010891 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010892 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010893 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010894 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010895 }
10896
10897 if( conf->psk_identity != NULL )
10898 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010899 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010900 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010901 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010902 conf->psk_identity_len = 0;
10903 }
10904#endif
10905
10906#if defined(MBEDTLS_X509_CRT_PARSE_C)
10907 ssl_key_cert_free( conf->key_cert );
10908#endif
10909
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010910 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010911}
10912
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010913#if defined(MBEDTLS_PK_C) && \
10914 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010915/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010918unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010919{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010920#if defined(MBEDTLS_RSA_C)
10921 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10922 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010923#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010924#if defined(MBEDTLS_ECDSA_C)
10925 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10926 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010927#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010928 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010929}
10930
Hanno Becker7e5437a2017-04-28 17:15:26 +010010931unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10932{
10933 switch( type ) {
10934 case MBEDTLS_PK_RSA:
10935 return( MBEDTLS_SSL_SIG_RSA );
10936 case MBEDTLS_PK_ECDSA:
10937 case MBEDTLS_PK_ECKEY:
10938 return( MBEDTLS_SSL_SIG_ECDSA );
10939 default:
10940 return( MBEDTLS_SSL_SIG_ANON );
10941 }
10942}
10943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010944mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010945{
10946 switch( sig )
10947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010948#if defined(MBEDTLS_RSA_C)
10949 case MBEDTLS_SSL_SIG_RSA:
10950 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010951#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010952#if defined(MBEDTLS_ECDSA_C)
10953 case MBEDTLS_SSL_SIG_ECDSA:
10954 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010955#endif
10956 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010957 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010958 }
10959}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010960#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010961
Hanno Becker7e5437a2017-04-28 17:15:26 +010010962#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10963 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10964
10965/* Find an entry in a signature-hash set matching a given hash algorithm. */
10966mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10967 mbedtls_pk_type_t sig_alg )
10968{
10969 switch( sig_alg )
10970 {
10971 case MBEDTLS_PK_RSA:
10972 return( set->rsa );
10973 case MBEDTLS_PK_ECDSA:
10974 return( set->ecdsa );
10975 default:
10976 return( MBEDTLS_MD_NONE );
10977 }
10978}
10979
10980/* Add a signature-hash-pair to a signature-hash set */
10981void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10982 mbedtls_pk_type_t sig_alg,
10983 mbedtls_md_type_t md_alg )
10984{
10985 switch( sig_alg )
10986 {
10987 case MBEDTLS_PK_RSA:
10988 if( set->rsa == MBEDTLS_MD_NONE )
10989 set->rsa = md_alg;
10990 break;
10991
10992 case MBEDTLS_PK_ECDSA:
10993 if( set->ecdsa == MBEDTLS_MD_NONE )
10994 set->ecdsa = md_alg;
10995 break;
10996
10997 default:
10998 break;
10999 }
11000}
11001
11002/* Allow exactly one hash algorithm for each signature. */
11003void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11004 mbedtls_md_type_t md_alg )
11005{
11006 set->rsa = md_alg;
11007 set->ecdsa = md_alg;
11008}
11009
11010#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11011 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11012
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011013/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011014 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011016mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011017{
11018 switch( hash )
11019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020#if defined(MBEDTLS_MD5_C)
11021 case MBEDTLS_SSL_HASH_MD5:
11022 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011023#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024#if defined(MBEDTLS_SHA1_C)
11025 case MBEDTLS_SSL_HASH_SHA1:
11026 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011027#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011028#if defined(MBEDTLS_SHA256_C)
11029 case MBEDTLS_SSL_HASH_SHA224:
11030 return( MBEDTLS_MD_SHA224 );
11031 case MBEDTLS_SSL_HASH_SHA256:
11032 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011033#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011034#if defined(MBEDTLS_SHA512_C)
11035 case MBEDTLS_SSL_HASH_SHA384:
11036 return( MBEDTLS_MD_SHA384 );
11037 case MBEDTLS_SSL_HASH_SHA512:
11038 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011039#endif
11040 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011041 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011042 }
11043}
11044
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011045/*
11046 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11047 */
11048unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11049{
11050 switch( md )
11051 {
11052#if defined(MBEDTLS_MD5_C)
11053 case MBEDTLS_MD_MD5:
11054 return( MBEDTLS_SSL_HASH_MD5 );
11055#endif
11056#if defined(MBEDTLS_SHA1_C)
11057 case MBEDTLS_MD_SHA1:
11058 return( MBEDTLS_SSL_HASH_SHA1 );
11059#endif
11060#if defined(MBEDTLS_SHA256_C)
11061 case MBEDTLS_MD_SHA224:
11062 return( MBEDTLS_SSL_HASH_SHA224 );
11063 case MBEDTLS_MD_SHA256:
11064 return( MBEDTLS_SSL_HASH_SHA256 );
11065#endif
11066#if defined(MBEDTLS_SHA512_C)
11067 case MBEDTLS_MD_SHA384:
11068 return( MBEDTLS_SSL_HASH_SHA384 );
11069 case MBEDTLS_MD_SHA512:
11070 return( MBEDTLS_SSL_HASH_SHA512 );
11071#endif
11072 default:
11073 return( MBEDTLS_SSL_HASH_NONE );
11074 }
11075}
11076
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011077#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011078/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011079 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011080 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011081 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011082int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011084 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011085
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011086 if( ssl->conf->curve_list == NULL )
11087 return( -1 );
11088
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011089 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011090 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011091 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011092
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011093 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011094}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011095#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011096
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011097#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011098/*
11099 * Check if a hash proposed by the peer is in our list.
11100 * Return 0 if we're willing to use it, -1 otherwise.
11101 */
11102int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11103 mbedtls_md_type_t md )
11104{
11105 const int *cur;
11106
11107 if( ssl->conf->sig_hashes == NULL )
11108 return( -1 );
11109
11110 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11111 if( *cur == (int) md )
11112 return( 0 );
11113
11114 return( -1 );
11115}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011116#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011118#if defined(MBEDTLS_X509_CRT_PARSE_C)
11119int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11120 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011121 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011122 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011123{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011124 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011125#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011126 int usage = 0;
11127#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011128#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011129 const char *ext_oid;
11130 size_t ext_len;
11131#endif
11132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011133#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11134 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011135 ((void) cert);
11136 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011137 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011138#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011140#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11141 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011142 {
11143 /* Server part of the key exchange */
11144 switch( ciphersuite->key_exchange )
11145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011146 case MBEDTLS_KEY_EXCHANGE_RSA:
11147 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011148 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011149 break;
11150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011151 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11152 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11153 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11154 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011155 break;
11156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011157 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11158 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011159 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011160 break;
11161
11162 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011163 case MBEDTLS_KEY_EXCHANGE_NONE:
11164 case MBEDTLS_KEY_EXCHANGE_PSK:
11165 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11166 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011167 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011168 usage = 0;
11169 }
11170 }
11171 else
11172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011173 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11174 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011175 }
11176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011177 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011178 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011179 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011180 ret = -1;
11181 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011182#else
11183 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011184#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011186#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11187 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011189 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11190 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011191 }
11192 else
11193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011194 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11195 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011196 }
11197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011198 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011199 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011200 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011201 ret = -1;
11202 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011203#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011204
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011205 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011206}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011207#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011208
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011209/*
11210 * Convert version numbers to/from wire format
11211 * and, for DTLS, to/from TLS equivalent.
11212 *
11213 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011214 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011215 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11216 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011218void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011219 unsigned char ver[2] )
11220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011221#if defined(MBEDTLS_SSL_PROTO_DTLS)
11222 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011224 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011225 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11226
11227 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11228 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11229 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011230 else
11231#else
11232 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011233#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011234 {
11235 ver[0] = (unsigned char) major;
11236 ver[1] = (unsigned char) minor;
11237 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011238}
11239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011240void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011241 const unsigned char ver[2] )
11242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011243#if defined(MBEDTLS_SSL_PROTO_DTLS)
11244 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011245 {
11246 *major = 255 - ver[0] + 2;
11247 *minor = 255 - ver[1] + 1;
11248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011249 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011250 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11251 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011252 else
11253#else
11254 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011255#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011256 {
11257 *major = ver[0];
11258 *minor = ver[1];
11259 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011260}
11261
Simon Butcher99000142016-10-13 17:21:01 +010011262int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11263{
11264#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11265 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11266 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11267
11268 switch( md )
11269 {
11270#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11271#if defined(MBEDTLS_MD5_C)
11272 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011273 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011274#endif
11275#if defined(MBEDTLS_SHA1_C)
11276 case MBEDTLS_SSL_HASH_SHA1:
11277 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11278 break;
11279#endif
11280#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11281#if defined(MBEDTLS_SHA512_C)
11282 case MBEDTLS_SSL_HASH_SHA384:
11283 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11284 break;
11285#endif
11286#if defined(MBEDTLS_SHA256_C)
11287 case MBEDTLS_SSL_HASH_SHA256:
11288 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11289 break;
11290#endif
11291 default:
11292 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11293 }
11294
11295 return 0;
11296#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11297 (void) ssl;
11298 (void) md;
11299
11300 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11301#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11302}
11303
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011304#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11305 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11306int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11307 unsigned char *output,
11308 unsigned char *data, size_t data_len )
11309{
11310 int ret = 0;
11311 mbedtls_md5_context mbedtls_md5;
11312 mbedtls_sha1_context mbedtls_sha1;
11313
11314 mbedtls_md5_init( &mbedtls_md5 );
11315 mbedtls_sha1_init( &mbedtls_sha1 );
11316
11317 /*
11318 * digitally-signed struct {
11319 * opaque md5_hash[16];
11320 * opaque sha_hash[20];
11321 * };
11322 *
11323 * md5_hash
11324 * MD5(ClientHello.random + ServerHello.random
11325 * + ServerParams);
11326 * sha_hash
11327 * SHA(ClientHello.random + ServerHello.random
11328 * + ServerParams);
11329 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011330 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011331 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011333 goto exit;
11334 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011335 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011336 ssl->handshake->randbytes, 64 ) ) != 0 )
11337 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011339 goto exit;
11340 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011341 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011342 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011344 goto exit;
11345 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011346 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011347 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011348 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011349 goto exit;
11350 }
11351
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011352 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011353 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011355 goto exit;
11356 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011357 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011358 ssl->handshake->randbytes, 64 ) ) != 0 )
11359 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011361 goto exit;
11362 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011363 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011364 data_len ) ) != 0 )
11365 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011367 goto exit;
11368 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011369 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011370 output + 16 ) ) != 0 )
11371 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011372 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011373 goto exit;
11374 }
11375
11376exit:
11377 mbedtls_md5_free( &mbedtls_md5 );
11378 mbedtls_sha1_free( &mbedtls_sha1 );
11379
11380 if( ret != 0 )
11381 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11382 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11383
11384 return( ret );
11385
11386}
11387#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11388 MBEDTLS_SSL_PROTO_TLS1_1 */
11389
11390#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11391 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011392
11393#if defined(MBEDTLS_USE_PSA_CRYPTO)
11394int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11395 unsigned char *hash, size_t *hashlen,
11396 unsigned char *data, size_t data_len,
11397 mbedtls_md_type_t md_alg )
11398{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011399 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011400 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011401 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11402
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011404
11405 if( ( status = psa_hash_setup( &hash_operation,
11406 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011407 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011408 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011409 goto exit;
11410 }
11411
Andrzej Kurek814feff2019-01-14 04:35:19 -050011412 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11413 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011414 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011415 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011416 goto exit;
11417 }
11418
Andrzej Kurek814feff2019-01-14 04:35:19 -050011419 if( ( status = psa_hash_update( &hash_operation,
11420 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011421 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011422 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011423 goto exit;
11424 }
11425
Andrzej Kurek814feff2019-01-14 04:35:19 -050011426 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11427 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011428 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011429 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011430 goto exit;
11431 }
11432
11433exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011434 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011435 {
11436 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11437 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011438 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011439 {
11440 case PSA_ERROR_NOT_SUPPORTED:
11441 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011442 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011443 case PSA_ERROR_BUFFER_TOO_SMALL:
11444 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11445 case PSA_ERROR_INSUFFICIENT_MEMORY:
11446 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11447 default:
11448 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11449 }
11450 }
11451 return( 0 );
11452}
11453
11454#else
11455
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011456int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011457 unsigned char *hash, size_t *hashlen,
11458 unsigned char *data, size_t data_len,
11459 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011460{
11461 int ret = 0;
11462 mbedtls_md_context_t ctx;
11463 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011464 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011465
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011467
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011468 mbedtls_md_init( &ctx );
11469
11470 /*
11471 * digitally-signed struct {
11472 * opaque client_random[32];
11473 * opaque server_random[32];
11474 * ServerDHParams params;
11475 * };
11476 */
11477 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11478 {
11479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11480 goto exit;
11481 }
11482 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11483 {
11484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11485 goto exit;
11486 }
11487 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11488 {
11489 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11490 goto exit;
11491 }
11492 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11493 {
11494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11495 goto exit;
11496 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011497 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011498 {
11499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11500 goto exit;
11501 }
11502
11503exit:
11504 mbedtls_md_free( &ctx );
11505
11506 if( ret != 0 )
11507 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11508 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11509
11510 return( ret );
11511}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011512#endif /* MBEDTLS_USE_PSA_CRYPTO */
11513
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011514#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11515 MBEDTLS_SSL_PROTO_TLS1_2 */
11516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011517#endif /* MBEDTLS_SSL_TLS_C */