blob: a9c099c1fbd59084edac62d8bc6c4fec7e438ae9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100114
Hanno Beckercfe45792019-07-03 16:13:00 +0100115#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +0100116static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
117 unsigned char *buf,
118 size_t len,
119 mbedtls_record *rec );
120
Hanno Beckercfe45792019-07-03 16:13:00 +0100121int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
122 unsigned char *buf,
123 size_t buflen )
124{
Hanno Becker54229812019-07-12 14:40:00 +0100125 int ret = 0;
126 mbedtls_record rec;
127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
128 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
129
130 /* We don't support record checking in TLS because
131 * (a) there doesn't seem to be a usecase for it, and
132 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
133 * and we'd need to backup the transform here.
134 */
135 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
136 {
137 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
138 goto exit;
139 }
140#if defined(MBEDTLS_SSL_PROTO_DTLS)
141 else
142 {
143 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
144 if( ret != 0 )
145 {
146 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
147 goto exit;
148 }
149
150 if( ssl->transform_in != NULL )
151 {
152 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
153 if( ret != 0 )
154 {
155 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
156 goto exit;
157 }
158 }
159 }
160#endif /* MBEDTLS_SSL_PROTO_DTLS */
161
162exit:
163 /* On success, we have decrypted the buffer in-place, so make
164 * sure we don't leak any plaintext data. */
165 mbedtls_platform_zeroize( buf, buflen );
166
167 /* For the purpose of this API, treat messages with unexpected CID
168 * as well as such from future epochs as unexpected. */
169 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
170 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
171 {
172 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
173 }
174
175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
176 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100177}
178#endif /* MBEDTLS_SSL_RECORD_CHECKING */
179
Hanno Becker67bc7c32018-08-06 11:33:50 +0100180#define SSL_DONT_FORCE_FLUSH 0
181#define SSL_FORCE_FLUSH 1
182
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200183#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100184
Hanno Beckera0e20d02019-05-15 14:03:01 +0100185#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100186/* Top-level Connection ID API */
187
Hanno Becker8367ccc2019-05-14 11:30:10 +0100188int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
189 size_t len,
190 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100191{
192 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
194
Hanno Becker611ac772019-05-14 11:45:26 +0100195 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
196 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
197 {
198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
199 }
200
201 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100202 conf->cid_len = len;
203 return( 0 );
204}
205
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100206int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
207 int enable,
208 unsigned char const *own_cid,
209 size_t own_cid_len )
210{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100211 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
213
Hanno Beckerca092242019-04-25 16:01:49 +0100214 ssl->negotiate_cid = enable;
215 if( enable == MBEDTLS_SSL_CID_DISABLED )
216 {
217 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
218 return( 0 );
219 }
220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100221 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100222
Hanno Beckerad4a1372019-05-03 13:06:44 +0100223 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100224 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
226 (unsigned) own_cid_len,
227 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
229 }
230
231 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100232 /* Truncation is not an issue here because
233 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
234 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100235
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100236 return( 0 );
237}
238
239int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
240 int *enabled,
241 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
242 size_t *peer_cid_len )
243{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100244 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100245
Hanno Becker76a79ab2019-05-03 14:38:32 +0100246 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
247 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
248 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100250 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100251
Hanno Beckerc5f24222019-05-03 12:54:52 +0100252 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
253 * were used, but client and server requested the empty CID.
254 * This is indistinguishable from not using the CID extension
255 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100256 if( ssl->transform_in->in_cid_len == 0 &&
257 ssl->transform_in->out_cid_len == 0 )
258 {
259 return( 0 );
260 }
261
Hanno Becker615ef172019-05-22 16:50:35 +0100262 if( peer_cid_len != NULL )
263 {
264 *peer_cid_len = ssl->transform_in->out_cid_len;
265 if( peer_cid != NULL )
266 {
267 memcpy( peer_cid, ssl->transform_in->out_cid,
268 ssl->transform_in->out_cid_len );
269 }
270 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100271
272 *enabled = MBEDTLS_SSL_CID_ENABLED;
273
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100274 return( 0 );
275}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100276#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100277
Hanno Beckerd5847772018-08-28 10:09:23 +0100278/* Forward declarations for functions related to message buffering. */
279static void ssl_buffering_free( mbedtls_ssl_context *ssl );
280static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
281 uint8_t slot );
282static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
283static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
284static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
285static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100286static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
287 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100288static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100289
Hanno Beckera67dee22018-08-22 10:05:20 +0100290static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100291static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100292{
Hanno Becker11682cc2018-08-22 14:41:02 +0100293 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100294
295 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100296 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100297
298 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
299}
300
Hanno Becker67bc7c32018-08-06 11:33:50 +0100301static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
302{
Hanno Becker11682cc2018-08-22 14:41:02 +0100303 size_t const bytes_written = ssl->out_left;
304 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100305
306 /* Double-check that the write-index hasn't gone
307 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100308 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100309 {
310 /* Should never happen... */
311 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
312 }
313
314 return( (int) ( mtu - bytes_written ) );
315}
316
317static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
318{
319 int ret;
320 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400321 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100322
323#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
324 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
325
326 if( max_len > mfl )
327 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100328
329 /* By the standard (RFC 6066 Sect. 4), the MFL extension
330 * only limits the maximum record payload size, so in theory
331 * we would be allowed to pack multiple records of payload size
332 * MFL into a single datagram. However, this would mean that there's
333 * no way to explicitly communicate MTU restrictions to the peer.
334 *
335 * The following reduction of max_len makes sure that we never
336 * write datagrams larger than MFL + Record Expansion Overhead.
337 */
338 if( max_len <= ssl->out_left )
339 return( 0 );
340
341 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100342#endif
343
344 ret = ssl_get_remaining_space_in_datagram( ssl );
345 if( ret < 0 )
346 return( ret );
347 remaining = (size_t) ret;
348
349 ret = mbedtls_ssl_get_record_expansion( ssl );
350 if( ret < 0 )
351 return( ret );
352 expansion = (size_t) ret;
353
354 if( remaining <= expansion )
355 return( 0 );
356
357 remaining -= expansion;
358 if( remaining >= max_len )
359 remaining = max_len;
360
361 return( (int) remaining );
362}
363
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200364/*
365 * Double the retransmit timeout value, within the allowed range,
366 * returning -1 if the maximum value has already been reached.
367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200369{
370 uint32_t new_timeout;
371
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200372 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200373 return( -1 );
374
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200375 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
376 * in the following way: after the initial transmission and a first
377 * retransmission, back off to a temporary estimated MTU of 508 bytes.
378 * This value is guaranteed to be deliverable (if not guaranteed to be
379 * delivered) of any compliant IPv4 (and IPv6) network, and should work
380 * on most non-IP stacks too. */
381 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400382 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200383 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
385 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200386
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200387 new_timeout = 2 * ssl->handshake->retransmit_timeout;
388
389 /* Avoid arithmetic overflow and range overflow */
390 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200391 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200392 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200393 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200394 }
395
396 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200398 ssl->handshake->retransmit_timeout ) );
399
400 return( 0 );
401}
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200404{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200405 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200407 ssl->handshake->retransmit_timeout ) );
408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200412/*
413 * Convert max_fragment_length codes to length.
414 * RFC 6066 says:
415 * enum{
416 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
417 * } MaxFragmentLength;
418 * and we add 0 -> extension unused
419 */
Angus Grattond8213d02016-05-25 20:56:48 +1000420static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200421{
Angus Grattond8213d02016-05-25 20:56:48 +1000422 switch( mfl )
423 {
424 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
425 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
426 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
427 return 512;
428 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
429 return 1024;
430 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
431 return 2048;
432 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
433 return 4096;
434 default:
435 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
436 }
437}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200439
Hanno Becker52055ae2019-02-06 14:30:46 +0000440int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
441 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_ssl_session_free( dst );
444 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000447
448#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200449 if( src->peer_cert != NULL )
450 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200451 int ret;
452
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200453 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200454 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200455 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200460 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200463 dst->peer_cert = NULL;
464 return( ret );
465 }
466 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000467#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000468 if( src->peer_cert_digest != NULL )
469 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000470 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000471 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000472 if( dst->peer_cert_digest == NULL )
473 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
474
475 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
476 src->peer_cert_digest_len );
477 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000478 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000479 }
480#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200483
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200484#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200485 if( src->ticket != NULL )
486 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200487 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200488 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200489 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200490
491 memcpy( dst->ticket, src->ticket, src->ticket_len );
492 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200493#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200494
495 return( 0 );
496}
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
499int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200500 const unsigned char *key_enc, const unsigned char *key_dec,
501 size_t keylen,
502 const unsigned char *iv_enc, const unsigned char *iv_dec,
503 size_t ivlen,
504 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200505 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
507int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
508int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
509int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
510int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
511#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000512
Paul Bakker5121ce52009-01-03 21:22:43 +0000513/*
514 * Key material generation
515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200517static int ssl3_prf( const unsigned char *secret, size_t slen,
518 const char *label,
519 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000520 unsigned char *dstbuf, size_t dlen )
521{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100522 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000523 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200524 mbedtls_md5_context md5;
525 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000526 unsigned char padding[16];
527 unsigned char sha1sum[20];
528 ((void)label);
529
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200530 mbedtls_md5_init( &md5 );
531 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200532
Paul Bakker5f70b252012-09-13 14:23:06 +0000533 /*
534 * SSLv3:
535 * block =
536 * MD5( secret + SHA1( 'A' + secret + random ) ) +
537 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
538 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
539 * ...
540 */
541 for( i = 0; i < dlen / 16; i++ )
542 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200543 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000544
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100545 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100546 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100547 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100548 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100549 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100550 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100551 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100552 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100553 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100554 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000555
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100556 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100557 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100558 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100559 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100560 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100561 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100562 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100563 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000564 }
565
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100566exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200567 mbedtls_md5_free( &md5 );
568 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000569
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500570 mbedtls_platform_zeroize( padding, sizeof( padding ) );
571 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000572
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100573 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200578static int tls1_prf( const unsigned char *secret, size_t slen,
579 const char *label,
580 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000581 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000582{
Paul Bakker23986e52011-04-24 08:57:21 +0000583 size_t nb, hs;
584 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200585 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300586 unsigned char *tmp;
587 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 const mbedtls_md_info_t *md_info;
590 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100591 int ret;
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Ron Eldor3b350852019-05-07 18:31:49 +0300595 tmp_len = 20 + strlen( label ) + rlen;
596 tmp = mbedtls_calloc( 1, tmp_len );
597 if( tmp == NULL )
598 {
599 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
600 goto exit;
601 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
603 hs = ( slen + 1 ) / 2;
604 S1 = secret;
605 S2 = secret + slen - hs;
606
607 nb = strlen( label );
608 memcpy( tmp + 20, label, nb );
609 memcpy( tmp + 20 + nb, random, rlen );
610 nb += rlen;
611
612 /*
613 * First compute P_md5(secret,label+random)[0..dlen]
614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300616 {
617 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
618 goto exit;
619 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300622 {
623 goto exit;
624 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
627 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
628 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630 for( i = 0; i < dlen; i += 16 )
631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
634 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_md_hmac_reset ( &md_ctx );
637 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
638 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
641
642 for( j = 0; j < k; j++ )
643 dstbuf[i + j] = h_i[j];
644 }
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100647
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 /*
649 * XOR out with P_sha1(secret,label+random)[0..dlen]
650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300652 {
653 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
654 goto exit;
655 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300658 {
659 goto exit;
660 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
663 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
664 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
666 for( i = 0; i < dlen; i += 20 )
667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_hmac_reset ( &md_ctx );
669 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
670 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_md_hmac_reset ( &md_ctx );
673 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
674 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
677
678 for( j = 0; j < k; j++ )
679 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
680 }
681
Ron Eldor3b350852019-05-07 18:31:49 +0300682exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100684
Ron Eldor3b350852019-05-07 18:31:49 +0300685 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500686 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000687
Ron Eldor3b350852019-05-07 18:31:49 +0300688 mbedtls_free( tmp );
689 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500694#if defined(MBEDTLS_USE_PSA_CRYPTO)
695static int tls_prf_generic( mbedtls_md_type_t md_type,
696 const unsigned char *secret, size_t slen,
697 const char *label,
698 const unsigned char *random, size_t rlen,
699 unsigned char *dstbuf, size_t dlen )
700{
701 psa_status_t status;
702 psa_algorithm_t alg;
703 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500704 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500705 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
706
Andrzej Kurek2f760752019-01-28 08:08:15 -0500707 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500708 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500709
Andrzej Kurekc929a822019-01-14 03:51:11 -0500710 if( md_type == MBEDTLS_MD_SHA384 )
711 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
712 else
713 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
714
Andrzej Kurek2f760752019-01-28 08:08:15 -0500715 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500716 psa_key_policy_set_usage( &policy,
717 PSA_KEY_USAGE_DERIVE,
718 alg );
719 status = psa_set_key_policy( master_slot, &policy );
720 if( status != PSA_SUCCESS )
721 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
722
723 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
724 if( status != PSA_SUCCESS )
725 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
726
727 status = psa_key_derivation( &generator,
728 master_slot, alg,
729 random, rlen,
730 (unsigned char const *) label,
731 (size_t) strlen( label ),
732 dlen );
733 if( status != PSA_SUCCESS )
734 {
735 psa_generator_abort( &generator );
736 psa_destroy_key( master_slot );
737 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
738 }
739
740 status = psa_generator_read( &generator, dstbuf, dlen );
741 if( status != PSA_SUCCESS )
742 {
743 psa_generator_abort( &generator );
744 psa_destroy_key( master_slot );
745 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
746 }
747
748 status = psa_generator_abort( &generator );
749 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500750 {
751 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500752 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500753 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500754
755 status = psa_destroy_key( master_slot );
756 if( status != PSA_SUCCESS )
757 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
758
Andrzej Kurek33171262019-01-15 03:25:18 -0500759 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500760}
761
762#else /* MBEDTLS_USE_PSA_CRYPTO */
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100765 const unsigned char *secret, size_t slen,
766 const char *label,
767 const unsigned char *random, size_t rlen,
768 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000769{
770 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100771 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300772 unsigned char *tmp;
773 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
775 const mbedtls_md_info_t *md_info;
776 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100777 int ret;
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
782 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100785
Ron Eldor3b350852019-05-07 18:31:49 +0300786 tmp_len = md_len + strlen( label ) + rlen;
787 tmp = mbedtls_calloc( 1, tmp_len );
788 if( tmp == NULL )
789 {
790 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
791 goto exit;
792 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000793
794 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100795 memcpy( tmp + md_len, label, nb );
796 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000797 nb += rlen;
798
799 /*
800 * Compute P_<hash>(secret, label + random)[0..dlen]
801 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300803 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
806 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
807 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100808
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100809 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_md_hmac_reset ( &md_ctx );
812 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
813 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_md_hmac_reset ( &md_ctx );
816 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
817 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000818
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100819 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000820
821 for( j = 0; j < k; j++ )
822 dstbuf[i + j] = h_i[j];
823 }
824
Ron Eldor3b350852019-05-07 18:31:49 +0300825exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100827
Ron Eldor3b350852019-05-07 18:31:49 +0300828 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500829 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000830
Ron Eldor3b350852019-05-07 18:31:49 +0300831 mbedtls_free( tmp );
832
833 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000834}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500835#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100837static int tls_prf_sha256( const unsigned char *secret, size_t slen,
838 const char *label,
839 const unsigned char *random, size_t rlen,
840 unsigned char *dstbuf, size_t dlen )
841{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100843 label, random, rlen, dstbuf, dlen ) );
844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200848static int tls_prf_sha384( const unsigned char *secret, size_t slen,
849 const char *label,
850 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000851 unsigned char *dstbuf, size_t dlen )
852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100854 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000855}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#endif /* MBEDTLS_SHA512_C */
857#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
862 defined(MBEDTLS_SSL_PROTO_TLS1_1)
863static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200864#endif
Paul Bakker380da532012-04-18 16:10:25 +0000865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if defined(MBEDTLS_SSL_PROTO_SSL3)
867static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
868static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200869#endif
870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
872static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
873static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200874#endif
875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
877#if defined(MBEDTLS_SHA256_C)
878static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
879static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
880static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200881#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883#if defined(MBEDTLS_SHA512_C)
884static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
885static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
886static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100887#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000889
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100890#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100891 defined(MBEDTLS_USE_PSA_CRYPTO)
892static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
893{
894 if( ssl->conf->f_psk != NULL )
895 {
896 /* If we've used a callback to select the PSK,
897 * the static configuration is irrelevant. */
898 if( ssl->handshake->psk_opaque != 0 )
899 return( 1 );
900
901 return( 0 );
902 }
903
904 if( ssl->conf->psk_opaque != 0 )
905 return( 1 );
906
907 return( 0 );
908}
909#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100910 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100911
Ron Eldorcf280092019-05-14 20:19:13 +0300912#if defined(MBEDTLS_SSL_EXPORT_KEYS)
913static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
914{
915#if defined(MBEDTLS_SSL_PROTO_SSL3)
916 if( tls_prf == ssl3_prf )
917 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300918 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300919 }
920 else
921#endif
922#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
923 if( tls_prf == tls1_prf )
924 {
925 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
926 }
927 else
928#endif
929#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
930#if defined(MBEDTLS_SHA512_C)
931 if( tls_prf == tls_prf_sha384 )
932 {
933 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
934 }
935 else
936#endif
937#if defined(MBEDTLS_SHA256_C)
938 if( tls_prf == tls_prf_sha256 )
939 {
940 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
941 }
942 else
943#endif
944#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
945 return( MBEDTLS_SSL_TLS_PRF_NONE );
946}
947#endif /* MBEDTLS_SSL_EXPORT_KEYS */
948
Ron Eldor51d3ab52019-05-12 14:54:30 +0300949int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
950 const unsigned char *secret, size_t slen,
951 const char *label,
952 const unsigned char *random, size_t rlen,
953 unsigned char *dstbuf, size_t dlen )
954{
955 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
956
957 switch( prf )
958 {
959#if defined(MBEDTLS_SSL_PROTO_SSL3)
960 case MBEDTLS_SSL_TLS_PRF_SSL3:
961 tls_prf = ssl3_prf;
962 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300963#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300964#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
965 case MBEDTLS_SSL_TLS_PRF_TLS1:
966 tls_prf = tls1_prf;
967 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300968#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
969
970#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300971#if defined(MBEDTLS_SHA512_C)
972 case MBEDTLS_SSL_TLS_PRF_SHA384:
973 tls_prf = tls_prf_sha384;
974 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300975#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300976#if defined(MBEDTLS_SHA256_C)
977 case MBEDTLS_SSL_TLS_PRF_SHA256:
978 tls_prf = tls_prf_sha256;
979 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300980#endif /* MBEDTLS_SHA256_C */
981#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300982 default:
983 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
984 }
985
986 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
987}
988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000990{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200991 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000992#if defined(MBEDTLS_USE_PSA_CRYPTO)
993 int psa_fallthrough;
994#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 unsigned char keyblk[256];
997 unsigned char *key1;
998 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100999 unsigned char *mac_enc;
1000 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001001 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001002 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001003 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001004 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 const mbedtls_cipher_info_t *cipher_info;
1006 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001007
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001008 /* cf. RFC 5246, Section 8.1:
1009 * "The master secret is always exactly 48 bytes in length." */
1010 size_t const master_secret_len = 48;
1011
Hanno Becker35b23c72018-10-23 12:10:41 +01001012#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1013 unsigned char session_hash[48];
1014#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_ssl_session *session = ssl->session_negotiate;
1017 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
1018 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +00001019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Jaeden Amero2de07f12019-06-05 13:32:08 +01001022#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1023 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001024 transform->encrypt_then_mac = session->encrypt_then_mac;
1025#endif
1026 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001027
1028 ciphersuite_info = handshake->ciphersuite_info;
1029 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001030 if( cipher_info == NULL )
1031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001033 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001035 }
1036
Hanno Beckere694c3e2017-12-27 21:34:08 +00001037 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001038 if( md_info == NULL )
1039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001041 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001043 }
1044
Hanno Beckera0e20d02019-05-15 14:03:01 +01001045#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001046 /* Copy own and peer's CID if the use of the CID
1047 * extension has been negotiated. */
1048 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1049 {
1050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001051
Hanno Becker05154c32019-05-03 15:23:51 +01001052 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001053 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001054 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001055 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001056
1057 transform->out_cid_len = ssl->handshake->peer_cid_len;
1058 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1059 ssl->handshake->peer_cid_len );
1060 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1061 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001062 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001063#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001064
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +00001066 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +00001067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_SSL_PROTO_SSL3)
1069 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001070 {
Paul Bakker48916f92012-09-16 19:57:18 +00001071 handshake->tls_prf = ssl3_prf;
1072 handshake->calc_verify = ssl_calc_verify_ssl;
1073 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001074 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001075 else
1076#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1078 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001079 {
Paul Bakker48916f92012-09-16 19:57:18 +00001080 handshake->tls_prf = tls1_prf;
1081 handshake->calc_verify = ssl_calc_verify_tls;
1082 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001083 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001084 else
1085#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1087#if defined(MBEDTLS_SHA512_C)
1088 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001089 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001090 {
Paul Bakker48916f92012-09-16 19:57:18 +00001091 handshake->tls_prf = tls_prf_sha384;
1092 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1093 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001094 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001095 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001096#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_SHA256_C)
1098 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001099 {
Paul Bakker48916f92012-09-16 19:57:18 +00001100 handshake->tls_prf = tls_prf_sha256;
1101 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1102 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001103 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001104 else
1105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1109 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001110 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001111
1112 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001113 * SSLv3:
1114 * master =
1115 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1116 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1117 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001118 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001119 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 * master = PRF( premaster, "master secret", randbytes )[0..47]
1121 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001122 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1125 }
1126 else
1127 {
1128 /* The label for the KDF used for key expansion.
1129 * This is either "master secret" or "extended master secret"
1130 * depending on whether the Extended Master Secret extension
1131 * is used. */
1132 char const *lbl = "master secret";
1133
1134 /* The salt for the KDF used for key expansion.
1135 * - If the Extended Master Secret extension is not used,
1136 * this is ClientHello.Random + ServerHello.Random
1137 * (see Sect. 8.1 in RFC 5246).
1138 * - If the Extended Master Secret extension is used,
1139 * this is the transcript of the handshake so far.
1140 * (see Sect. 4 in RFC 7627). */
1141 unsigned char const *salt = handshake->randbytes;
1142 size_t salt_len = 64;
1143
1144#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001148
Hanno Becker35b23c72018-10-23 12:10:41 +01001149 lbl = "extended master secret";
1150 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001151 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1153 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001156 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1157 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001158 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001159 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001160 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001161#endif /* MBEDTLS_SHA512_C */
1162 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001163 }
1164 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001166 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001167
Hanno Becker35b23c72018-10-23 12:10:41 +01001168 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001169 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001170#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1171
Hanno Becker7d0a5692018-10-23 15:26:22 +01001172#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1173 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1174 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1175 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1176 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001177 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001178 /* Perform PSK-to-MS expansion in a single step. */
1179 psa_status_t status;
1180 psa_algorithm_t alg;
1181 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001182 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001183
1184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1185
1186 psk = ssl->conf->psk_opaque;
1187 if( ssl->handshake->psk_opaque != 0 )
1188 psk = ssl->handshake->psk_opaque;
1189
Hanno Becker22bf1452019-04-05 11:21:08 +01001190 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001191 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1192 else
1193 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1194
1195 status = psa_key_derivation( &generator, psk, alg,
1196 salt, salt_len,
1197 (unsigned char const *) lbl,
1198 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001199 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001200 if( status != PSA_SUCCESS )
1201 {
1202 psa_generator_abort( &generator );
1203 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1204 }
1205
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001206 status = psa_generator_read( &generator, session->master,
1207 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001208 if( status != PSA_SUCCESS )
1209 {
1210 psa_generator_abort( &generator );
1211 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1212 }
1213
1214 status = psa_generator_abort( &generator );
1215 if( status != PSA_SUCCESS )
1216 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001217 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001218 else
1219#endif
1220 {
1221 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1222 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001223 session->master,
1224 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001225 if( ret != 0 )
1226 {
1227 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1228 return( ret );
1229 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001230
Hanno Becker7d0a5692018-10-23 15:26:22 +01001231 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1232 handshake->premaster,
1233 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001234
Hanno Becker7d0a5692018-10-23 15:26:22 +01001235 mbedtls_platform_zeroize( handshake->premaster,
1236 sizeof(handshake->premaster) );
1237 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
1240 /*
1241 * Swap the client and server random values.
1242 */
Paul Bakker48916f92012-09-16 19:57:18 +00001243 memcpy( tmp, handshake->randbytes, 64 );
1244 memcpy( handshake->randbytes, tmp + 32, 32 );
1245 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001246 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
1248 /*
1249 * SSLv3:
1250 * key block =
1251 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1252 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1253 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1254 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1255 * ...
1256 *
1257 * TLSv1:
1258 * key block = PRF( master, "key expansion", randbytes )
1259 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001260 ret = handshake->tls_prf( session->master, 48, "key expansion",
1261 handshake->randbytes, 64, keyblk, 256 );
1262 if( ret != 0 )
1263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001265 return( ret );
1266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1269 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1270 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1271 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1272 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 /*
1275 * Determine the appropriate key, IV and MAC length.
1276 */
Paul Bakker68884e32013-01-07 18:20:04 +01001277
Hanno Becker88aaf652017-12-27 08:17:40 +00001278 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001279
Hanno Becker8031d062018-01-03 15:32:31 +00001280#if defined(MBEDTLS_GCM_C) || \
1281 defined(MBEDTLS_CCM_C) || \
1282 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001284 cipher_info->mode == MBEDTLS_MODE_CCM ||
1285 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001287 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001288
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001289 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001290 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001291 transform->taglen =
1292 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001293
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001294 /* All modes haves 96-bit IVs;
1295 * GCM and CCM has 4 implicit and 8 explicit bytes
1296 * ChachaPoly has all 12 bytes implicit
1297 */
Paul Bakker68884e32013-01-07 18:20:04 +01001298 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001299 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1300 transform->fixed_ivlen = 12;
1301 else
1302 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001303
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001304 /* Minimum length of encrypted record */
1305 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001306 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001307 }
1308 else
Hanno Becker8031d062018-01-03 15:32:31 +00001309#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1310#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1311 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1312 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001313 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001314 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1316 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001319 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001320 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001322 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001323 mac_key_len = mbedtls_md_get_size( md_info );
1324 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001327 /*
1328 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1329 * (rfc 6066 page 13 or rfc 2104 section 4),
1330 * so we only need to adjust the length here.
1331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001335
1336#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1337 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001338 * HMAC implementation which also truncates the key
1339 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001340 mac_key_len = transform->maclen;
1341#endif
1342 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001344
1345 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001346 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001347
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001348 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001350 transform->minlen = transform->maclen;
1351 else
Paul Bakker68884e32013-01-07 18:20:04 +01001352 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001353 /*
1354 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001355 * 1. if EtM is in use: one block plus MAC
1356 * otherwise: * first multiple of blocklen greater than maclen
1357 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1360 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001361 {
1362 transform->minlen = transform->maclen
1363 + cipher_info->block_size;
1364 }
1365 else
1366#endif
1367 {
1368 transform->minlen = transform->maclen
1369 + cipher_info->block_size
1370 - transform->maclen % cipher_info->block_size;
1371 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1374 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1375 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001376 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001377 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001378#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1380 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1381 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001382 {
1383 transform->minlen += transform->ivlen;
1384 }
1385 else
1386#endif
1387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001389 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1390 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001391 }
Paul Bakker68884e32013-01-07 18:20:04 +01001392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 }
Hanno Becker8031d062018-01-03 15:32:31 +00001394 else
1395#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1396 {
1397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Hanno Becker88aaf652017-12-27 08:17:40 +00001401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1402 (unsigned) keylen,
1403 (unsigned) transform->minlen,
1404 (unsigned) transform->ivlen,
1405 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
1407 /*
1408 * Finally setup the cipher contexts, IVs and MAC secrets.
1409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001411 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001413 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001414 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Paul Bakker68884e32013-01-07 18:20:04 +01001416 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001417 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001419 /*
1420 * This is not used in TLS v1.1.
1421 */
Paul Bakker48916f92012-09-16 19:57:18 +00001422 iv_copy_len = ( transform->fixed_ivlen ) ?
1423 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001424 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1425 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001426 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 }
1428 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#endif /* MBEDTLS_SSL_CLI_C */
1430#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001431 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001433 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001434 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
Hanno Becker81c7b182017-11-09 18:39:33 +00001436 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001437 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001438
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001439 /*
1440 * This is not used in TLS v1.1.
1441 */
Paul Bakker48916f92012-09-16 19:57:18 +00001442 iv_copy_len = ( transform->fixed_ivlen ) ?
1443 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001444 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1445 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001446 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001448 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001452 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1453 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001454 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001455
Hanno Beckerd56ed242018-01-03 15:32:51 +00001456#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_SSL_PROTO_SSL3)
1458 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001459 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001460 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001463 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1464 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001465 }
1466
Hanno Becker81c7b182017-11-09 18:39:33 +00001467 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1468 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001469 }
1470 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1472#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1473 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1474 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001475 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001476 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1477 For AEAD-based ciphersuites, there is nothing to do here. */
1478 if( mac_key_len != 0 )
1479 {
1480 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1481 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1482 }
Paul Bakker68884e32013-01-07 18:20:04 +01001483 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001484 else
1485#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001488 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1489 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001490 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001491#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1494 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001495 {
1496 int ret = 0;
1497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001499
Hanno Becker88aaf652017-12-27 08:17:40 +00001500 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001501 transform->iv_enc, transform->iv_dec,
1502 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001503 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001504 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001507 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1508 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001509 }
1510 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001511#else
1512 ((void) mac_dec);
1513 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001515
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001516#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1517 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001518 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001519 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1520 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001521 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001522 iv_copy_len );
1523 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001524
1525 if( ssl->conf->f_export_keys_ext != NULL )
1526 {
1527 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1528 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001529 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001530 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001531 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001532 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001533 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001534 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001535#endif
1536
Hanno Beckerf704bef2018-11-16 15:21:18 +00001537#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001538
1539 /* Only use PSA-based ciphers for TLS-1.2.
1540 * That's relevant at least for TLS-1.0, where
1541 * we assume that mbedtls_cipher_crypt() updates
1542 * the structure field for the IV, which the PSA-based
1543 * implementation currently doesn't. */
1544#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1545 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001546 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001547 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001548 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001549 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1550 {
1551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001552 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001553 }
1554
1555 if( ret == 0 )
1556 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001558 psa_fallthrough = 0;
1559 }
1560 else
1561 {
1562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1563 psa_fallthrough = 1;
1564 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001565 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001566 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001567 psa_fallthrough = 1;
1568#else
1569 psa_fallthrough = 1;
1570#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001571
Hanno Beckercb1cc802018-11-17 22:27:38 +00001572 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001573#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001574 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001575 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001576 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001578 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001580
Hanno Beckerf704bef2018-11-16 15:21:18 +00001581#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001582 /* Only use PSA-based ciphers for TLS-1.2.
1583 * That's relevant at least for TLS-1.0, where
1584 * we assume that mbedtls_cipher_crypt() updates
1585 * the structure field for the IV, which the PSA-based
1586 * implementation currently doesn't. */
1587#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1588 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001589 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001590 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001591 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001592 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1593 {
1594 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001595 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001596 }
1597
1598 if( ret == 0 )
1599 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001601 psa_fallthrough = 0;
1602 }
1603 else
1604 {
1605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1606 psa_fallthrough = 1;
1607 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001608 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001609 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001610 psa_fallthrough = 1;
1611#else
1612 psa_fallthrough = 1;
1613#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001614
Hanno Beckercb1cc802018-11-17 22:27:38 +00001615 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001616#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001617 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001618 cipher_info ) ) != 0 )
1619 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001621 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001622 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001625 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001629 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001630 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001633 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001637 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001638 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#if defined(MBEDTLS_CIPHER_MODE_CBC)
1641 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1644 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001647 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001648 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1651 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001654 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001661 // Initialize compression
1662 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001664 {
Paul Bakker16770332013-10-11 09:59:44 +02001665 if( ssl->compress_buf == NULL )
1666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001668 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001669 if( ssl->compress_buf == NULL )
1670 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001672 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001673 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1674 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001675 }
1676 }
1677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001679
Paul Bakker48916f92012-09-16 19:57:18 +00001680 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1681 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001682
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001683 if( deflateInit( &transform->ctx_deflate,
1684 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001685 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001688 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1689 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001690 }
1691 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001695end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001696 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1697 mbedtls_platform_zeroize( handshake->randbytes,
1698 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001699 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001700}
1701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#if defined(MBEDTLS_SSL_PROTO_SSL3)
1703void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001704{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001705 mbedtls_md5_context md5;
1706 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 unsigned char pad_1[48];
1708 unsigned char pad_2[48];
1709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001711
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001712 mbedtls_md5_init( &md5 );
1713 mbedtls_sha1_init( &sha1 );
1714
1715 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1716 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001717
Paul Bakker380da532012-04-18 16:10:25 +00001718 memset( pad_1, 0x36, 48 );
1719 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001721 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1722 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1723 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001724
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001725 mbedtls_md5_starts_ret( &md5 );
1726 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1727 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1728 mbedtls_md5_update_ret( &md5, hash, 16 );
1729 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001731 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1732 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1733 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001734
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001735 mbedtls_sha1_starts_ret( &sha1 );
1736 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1737 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1738 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1739 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001743
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001744 mbedtls_md5_free( &md5 );
1745 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001746
Paul Bakker380da532012-04-18 16:10:25 +00001747 return;
1748}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1752void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001753{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001754 mbedtls_md5_context md5;
1755 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001758
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001759 mbedtls_md5_init( &md5 );
1760 mbedtls_sha1_init( &sha1 );
1761
1762 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1763 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001764
Andrzej Kurekeb342242019-01-29 09:14:33 -05001765 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001766 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001770
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001771 mbedtls_md5_free( &md5 );
1772 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001773
Paul Bakker380da532012-04-18 16:10:25 +00001774 return;
1775}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1779#if defined(MBEDTLS_SHA256_C)
1780void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001781{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001782#if defined(MBEDTLS_USE_PSA_CRYPTO)
1783 size_t hash_size;
1784 psa_status_t status;
1785 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1786
1787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1788 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1789 if( status != PSA_SUCCESS )
1790 {
1791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1792 return;
1793 }
1794
1795 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1796 if( status != PSA_SUCCESS )
1797 {
1798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1799 return;
1800 }
1801 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1803#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001804 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001805
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001806 mbedtls_sha256_init( &sha256 );
1807
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001809
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001810 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001811 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001815
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001816 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001817#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001818 return;
1819}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#if defined(MBEDTLS_SHA512_C)
1823void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001824{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001825#if defined(MBEDTLS_USE_PSA_CRYPTO)
1826 size_t hash_size;
1827 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001828 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001829
1830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001831 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001832 if( status != PSA_SUCCESS )
1833 {
1834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1835 return;
1836 }
1837
Andrzej Kurek972fba52019-01-30 03:29:12 -05001838 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001839 if( status != PSA_SUCCESS )
1840 {
1841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1842 return;
1843 }
1844 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1846#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001847 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001848
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001849 mbedtls_sha512_init( &sha512 );
1850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001852
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001853 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001854 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001859 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001860#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001861 return;
1862}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#endif /* MBEDTLS_SHA512_C */
1864#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1867int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001868{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001869 unsigned char *p = ssl->handshake->premaster;
1870 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001871 const unsigned char *psk = ssl->conf->psk;
1872 size_t psk_len = ssl->conf->psk_len;
1873
1874 /* If the psk callback was called, use its result */
1875 if( ssl->handshake->psk != NULL )
1876 {
1877 psk = ssl->handshake->psk;
1878 psk_len = ssl->handshake->psk_len;
1879 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001880
1881 /*
1882 * PMS = struct {
1883 * opaque other_secret<0..2^16-1>;
1884 * opaque psk<0..2^16-1>;
1885 * };
1886 * with "other_secret" depending on the particular key exchange
1887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1889 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001890 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001891 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001893
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001894 *(p++) = (unsigned char)( psk_len >> 8 );
1895 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001896
1897 if( end < p || (size_t)( end - p ) < psk_len )
1898 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1899
1900 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001901 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001902 }
1903 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1905#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1906 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001907 {
1908 /*
1909 * other_secret already set by the ClientKeyExchange message,
1910 * and is 48 bytes long
1911 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001912 if( end - p < 2 )
1913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1914
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001915 *p++ = 0;
1916 *p++ = 48;
1917 p += 48;
1918 }
1919 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1921#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1922 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001923 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001924 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001925 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001926
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001927 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001929 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001930 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001933 return( ret );
1934 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001935 *(p++) = (unsigned char)( len >> 8 );
1936 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001937 p += len;
1938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001940 }
1941 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1943#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1944 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001945 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001946 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001947 size_t zlen;
1948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001950 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001951 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001954 return( ret );
1955 }
1956
1957 *(p++) = (unsigned char)( zlen >> 8 );
1958 *(p++) = (unsigned char)( zlen );
1959 p += zlen;
1960
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001961 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1962 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001963 }
1964 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001969 }
1970
1971 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001972 if( end - p < 2 )
1973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001974
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001975 *(p++) = (unsigned char)( psk_len >> 8 );
1976 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001977
1978 if( end < p || (size_t)( end - p ) < psk_len )
1979 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1980
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001981 memcpy( p, psk, psk_len );
1982 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001983
1984 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1985
1986 return( 0 );
1987}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001991/*
1992 * SSLv3.0 MAC functions
1993 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001994#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001995static void ssl_mac( mbedtls_md_context_t *md_ctx,
1996 const unsigned char *secret,
1997 const unsigned char *buf, size_t len,
1998 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001999 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002000{
2001 unsigned char header[11];
2002 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002003 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2005 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002006
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002007 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002009 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002010 else
Paul Bakker68884e32013-01-07 18:20:04 +01002011 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002012
2013 memcpy( header, ctr, 8 );
2014 header[ 8] = (unsigned char) type;
2015 header[ 9] = (unsigned char)( len >> 8 );
2016 header[10] = (unsigned char)( len );
2017
Paul Bakker68884e32013-01-07 18:20:04 +01002018 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 mbedtls_md_starts( md_ctx );
2020 mbedtls_md_update( md_ctx, secret, md_size );
2021 mbedtls_md_update( md_ctx, padding, padlen );
2022 mbedtls_md_update( md_ctx, header, 11 );
2023 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002024 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002025
Paul Bakker68884e32013-01-07 18:20:04 +01002026 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 mbedtls_md_starts( md_ctx );
2028 mbedtls_md_update( md_ctx, secret, md_size );
2029 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002030 mbedtls_md_update( md_ctx, out, md_size );
2031 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002032}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002034
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002035/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002036 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002037#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002038 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2039 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2040 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2041/* This function makes sure every byte in the memory region is accessed
2042 * (in ascending addresses order) */
2043static void ssl_read_memory( unsigned char *p, size_t len )
2044{
2045 unsigned char acc = 0;
2046 volatile unsigned char force;
2047
2048 for( ; len != 0; p++, len-- )
2049 acc ^= *p;
2050
2051 force = acc;
2052 (void) force;
2053}
2054#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2055
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002056/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002058 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002059
Hanno Beckera0e20d02019-05-15 14:03:01 +01002060#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002061/* This functions transforms a DTLS plaintext fragment and a record content
2062 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002063 *
2064 * struct {
2065 * opaque content[DTLSPlaintext.length];
2066 * ContentType real_type;
2067 * uint8 zeros[length_of_padding];
2068 * } DTLSInnerPlaintext;
2069 *
2070 * Input:
2071 * - `content`: The beginning of the buffer holding the
2072 * plaintext to be wrapped.
2073 * - `*content_size`: The length of the plaintext in Bytes.
2074 * - `max_len`: The number of Bytes available starting from
2075 * `content`. This must be `>= *content_size`.
2076 * - `rec_type`: The desired record content type.
2077 *
2078 * Output:
2079 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2080 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2081 *
2082 * Returns:
2083 * - `0` on success.
2084 * - A negative error code if `max_len` didn't offer enough space
2085 * for the expansion.
2086 */
2087static int ssl_cid_build_inner_plaintext( unsigned char *content,
2088 size_t *content_size,
2089 size_t remaining,
2090 uint8_t rec_type )
2091{
2092 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002093 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2094 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2095 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002096
2097 /* Write real content type */
2098 if( remaining == 0 )
2099 return( -1 );
2100 content[ len ] = rec_type;
2101 len++;
2102 remaining--;
2103
2104 if( remaining < pad )
2105 return( -1 );
2106 memset( content + len, 0, pad );
2107 len += pad;
2108 remaining -= pad;
2109
2110 *content_size = len;
2111 return( 0 );
2112}
2113
Hanno Becker07dc97d2019-05-20 15:08:01 +01002114/* This function parses a DTLSInnerPlaintext structure.
2115 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002116static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2117 size_t *content_size,
2118 uint8_t *rec_type )
2119{
2120 size_t remaining = *content_size;
2121
2122 /* Determine length of padding by skipping zeroes from the back. */
2123 do
2124 {
2125 if( remaining == 0 )
2126 return( -1 );
2127 remaining--;
2128 } while( content[ remaining ] == 0 );
2129
2130 *content_size = remaining;
2131 *rec_type = content[ remaining ];
2132
2133 return( 0 );
2134}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002135#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002136
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002137/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002138 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002139static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002140 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002141 mbedtls_record *rec )
2142{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002143 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002144 *
2145 * additional_data = seq_num + TLSCompressed.type +
2146 * TLSCompressed.version + TLSCompressed.length;
2147 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002148 * For the CID extension, this is extended as follows
2149 * (quoting draft-ietf-tls-dtls-connection-id-05,
2150 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002151 *
2152 * additional_data = seq_num + DTLSPlaintext.type +
2153 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002154 * cid +
2155 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002156 * length_of_DTLSInnerPlaintext;
2157 */
2158
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002159 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2160 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002161 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002162
Hanno Beckera0e20d02019-05-15 14:03:01 +01002163#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002164 if( rec->cid_len != 0 )
2165 {
2166 memcpy( add_data + 11, rec->cid, rec->cid_len );
2167 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2168 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2169 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2170 *add_data_len = 13 + 1 + rec->cid_len;
2171 }
2172 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002173#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002174 {
2175 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2176 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2177 *add_data_len = 13;
2178 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002179}
2180
Hanno Beckera18d1322018-01-03 14:27:32 +00002181int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2182 mbedtls_ssl_transform *transform,
2183 mbedtls_record *rec,
2184 int (*f_rng)(void *, unsigned char *, size_t),
2185 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002188 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002189 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002190 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002191 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002192 size_t post_avail;
2193
2194 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002195#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002196 ((void) ssl);
2197#endif
2198
2199 /* The PRNG is used for dynamic IV generation that's used
2200 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2201#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2202 ( defined(MBEDTLS_AES_C) || \
2203 defined(MBEDTLS_ARIA_C) || \
2204 defined(MBEDTLS_CAMELLIA_C) ) && \
2205 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2206 ((void) f_rng);
2207 ((void) p_rng);
2208#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002212 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002213 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2216 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002217 if( rec == NULL
2218 || rec->buf == NULL
2219 || rec->buf_len < rec->data_offset
2220 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002221#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002222 || rec->cid_len != 0
2223#endif
2224 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002225 {
2226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002228 }
2229
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002230 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002231 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233 data, rec->data_len );
2234
2235 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2236
2237 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2238 {
2239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2240 (unsigned) rec->data_len,
2241 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2243 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002244
Hanno Beckera0e20d02019-05-15 14:03:01 +01002245#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002246 /*
2247 * Add CID information
2248 */
2249 rec->cid_len = transform->out_cid_len;
2250 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2251 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002252
2253 if( rec->cid_len != 0 )
2254 {
2255 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002256 * Wrap plaintext into DTLSInnerPlaintext structure.
2257 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002258 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002259 * Note that this changes `rec->data_len`, and hence
2260 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002261 */
2262 if( ssl_cid_build_inner_plaintext( data,
2263 &rec->data_len,
2264 post_avail,
2265 rec->type ) != 0 )
2266 {
2267 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2268 }
2269
2270 rec->type = MBEDTLS_SSL_MSG_CID;
2271 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002272#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002273
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002274 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2275
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002277 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 */
Hanno Becker52344c22018-01-03 15:24:20 +00002279#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 if( mode == MBEDTLS_MODE_STREAM ||
2281 ( mode == MBEDTLS_MODE_CBC
2282#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002283 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002284#endif
2285 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002287 if( post_avail < transform->maclen )
2288 {
2289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2290 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2291 }
2292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002294 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002295 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002296 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002297 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2298 data, rec->data_len, rec->ctr, rec->type, mac );
2299 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002300 }
2301 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2304 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002305 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002306 {
Hanno Becker992b6872017-11-09 18:57:39 +00002307 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2308
Hanno Beckercab87e62019-04-29 13:52:53 +01002309 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002310
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002311 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002312 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002313 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2314 data, rec->data_len );
2315 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2316 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2317
2318 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002319 }
2320 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002321#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2324 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002325 }
2326
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002327 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2328 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002329
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002330 rec->data_len += transform->maclen;
2331 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002332 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002333 }
Hanno Becker52344c22018-01-03 15:24:20 +00002334#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002335
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002336 /*
2337 * Encrypt
2338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2340 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002341 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002342 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002343 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002345 "including %d bytes of padding",
2346 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002347
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002348 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2349 transform->iv_enc, transform->ivlen,
2350 data, rec->data_len,
2351 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002354 return( ret );
2355 }
2356
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002357 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2360 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002361 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002362 }
Paul Bakker68884e32013-01-07 18:20:04 +01002363 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002365
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002366#if defined(MBEDTLS_GCM_C) || \
2367 defined(MBEDTLS_CCM_C) || \
2368 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002370 mode == MBEDTLS_MODE_CCM ||
2371 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002372 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002373 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002374 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002375 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002376
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002377 /* Check that there's space for both the authentication tag
2378 * and the explicit IV before and after the record content. */
2379 if( post_avail < transform->taglen ||
2380 rec->data_offset < explicit_iv_len )
2381 {
2382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2383 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2384 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002385
Paul Bakker68884e32013-01-07 18:20:04 +01002386 /*
2387 * Generate IV
2388 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002389 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2390 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002391 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002392 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002393 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2394 explicit_iv_len );
2395 /* Prefix record content with explicit IV. */
2396 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002397 }
2398 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2399 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002400 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002401 unsigned char i;
2402
2403 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2404
2405 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002406 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002407 }
2408 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002409 {
2410 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2412 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002413 }
2414
Hanno Beckercab87e62019-04-29 13:52:53 +01002415 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002416
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002417 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2418 iv, transform->ivlen );
2419 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002420 data - explicit_iv_len, explicit_iv_len );
2421 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002422 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002424 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002425 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002426
Paul Bakker68884e32013-01-07 18:20:04 +01002427 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002428 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002429 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002430
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002431 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002432 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002433 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002434 data, rec->data_len, /* source */
2435 data, &rec->data_len, /* destination */
2436 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002439 return( ret );
2440 }
2441
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002442 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2443 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002444
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002445 rec->data_len += transform->taglen + explicit_iv_len;
2446 rec->data_offset -= explicit_iv_len;
2447 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002448 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002449 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2452#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002453 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002456 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002457 size_t padlen, i;
2458 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002459
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002460 /* Currently we're always using minimal padding
2461 * (up to 255 bytes would be allowed). */
2462 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2463 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 padlen = 0;
2465
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002466 /* Check there's enough space in the buffer for the padding. */
2467 if( post_avail < padlen + 1 )
2468 {
2469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2470 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2471 }
2472
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002474 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002475
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002476 rec->data_len += padlen + 1;
2477 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002480 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002481 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2482 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002483 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002484 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002485 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002486 if( f_rng == NULL )
2487 {
2488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2490 }
2491
2492 if( rec->data_offset < transform->ivlen )
2493 {
2494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2495 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2496 }
2497
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002498 /*
2499 * Generate IV
2500 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002501 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002502 if( ret != 0 )
2503 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002504
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002505 memcpy( data - transform->ivlen, transform->iv_enc,
2506 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002507
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002508 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002512 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002513 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002514 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002516 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2517 transform->iv_enc,
2518 transform->ivlen,
2519 data, rec->data_len,
2520 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002523 return( ret );
2524 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002525
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002526 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002530 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002533 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002534 {
2535 /*
2536 * Save IV in SSL3 and TLS1
2537 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002538 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2539 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002541 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002542#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002543 {
2544 data -= transform->ivlen;
2545 rec->data_offset -= transform->ivlen;
2546 rec->data_len += transform->ivlen;
2547 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002550 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002551 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002552 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2553
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002554 /*
2555 * MAC(MAC_write_key, seq_num +
2556 * TLSCipherText.type +
2557 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002558 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002559 * IV + // except for TLS 1.0
2560 * ENC(content + padding + padding_length));
2561 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002562
2563 if( post_avail < transform->maclen)
2564 {
2565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2566 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2567 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002568
Hanno Beckercab87e62019-04-29 13:52:53 +01002569 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002572 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002573 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002574
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002575 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002576 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002577 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2578 data, rec->data_len );
2579 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2580 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002581
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002582 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002583
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002584 rec->data_len += transform->maclen;
2585 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002586 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002587 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002590 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002592 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002598 /* Make extra sure authentication was performed, exactly once */
2599 if( auth_done != 1 )
2600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2602 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002603 }
2604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
2607 return( 0 );
2608}
2609
Hanno Becker605949f2019-07-12 08:23:59 +01002610int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002611 mbedtls_ssl_transform *transform,
2612 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002613{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002614 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002616 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002617#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002618 size_t padlen = 0, correct = 1;
2619#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002620 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002621 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002622 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002623
Hanno Beckera18d1322018-01-03 14:27:32 +00002624#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002625 ((void) ssl);
2626#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002629 if( rec == NULL ||
2630 rec->buf == NULL ||
2631 rec->buf_len < rec->data_offset ||
2632 rec->buf_len - rec->data_offset < rec->data_len )
2633 {
2634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002636 }
2637
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002638 data = rec->buf + rec->data_offset;
2639 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Hanno Beckera0e20d02019-05-15 14:03:01 +01002641#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002642 /*
2643 * Match record's CID with incoming CID.
2644 */
Hanno Becker938489a2019-05-08 13:02:22 +01002645 if( rec->cid_len != transform->in_cid_len ||
2646 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2647 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002648 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002649 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002650#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2653 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002654 {
2655 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002656 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2657 transform->iv_dec,
2658 transform->ivlen,
2659 data, rec->data_len,
2660 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002663 return( ret );
2664 }
2665
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002666 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2669 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 }
Paul Bakker68884e32013-01-07 18:20:04 +01002672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002674#if defined(MBEDTLS_GCM_C) || \
2675 defined(MBEDTLS_CCM_C) || \
2676 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002678 mode == MBEDTLS_MODE_CCM ||
2679 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002680 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002681 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002682 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002683
Hanno Beckerd96a6522019-07-10 13:55:25 +01002684 /*
2685 * Prepare IV from explicit and implicit data.
2686 */
2687
2688 /* Check that there's enough space for the explicit IV
2689 * (at the beginning of the record) and the MAC (at the
2690 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002691 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002694 "+ taglen (%d)", rec->data_len,
2695 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002697 }
Paul Bakker68884e32013-01-07 18:20:04 +01002698
Hanno Beckerd96a6522019-07-10 13:55:25 +01002699#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002700 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2701 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002702 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002703
Hanno Beckerd96a6522019-07-10 13:55:25 +01002704 /* Fixed */
2705 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2706 /* Explicit */
2707 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002708 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002709 else
2710#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2711#if defined(MBEDTLS_CHACHAPOLY_C)
2712 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002713 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002714 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002715 unsigned char i;
2716
2717 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2718
2719 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002720 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002721 }
2722 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002723#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002724 {
2725 /* Reminder if we ever add an AEAD mode with a different size */
2726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2728 }
2729
Hanno Beckerd96a6522019-07-10 13:55:25 +01002730 /* Group changes to data, data_len, and add_data, because
2731 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002732 data += explicit_iv_len;
2733 rec->data_offset += explicit_iv_len;
2734 rec->data_len -= explicit_iv_len + transform->taglen;
2735
Hanno Beckercab87e62019-04-29 13:52:53 +01002736 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002737 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002738 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002739
Hanno Beckerd96a6522019-07-10 13:55:25 +01002740 /* Because of the check above, we know that there are
2741 * explicit_iv_len Bytes preceeding data, and taglen
2742 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002743 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002744 * mbedtls_cipher_auth_decrypt() below. */
2745
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002746 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002747 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002748 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002749
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002750 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002751 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002752 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002753 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2754 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002755 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002756 data, rec->data_len,
2757 data, &olen,
2758 data + rec->data_len,
2759 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2764 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002765
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002766 return( ret );
2767 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002768 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002769
Hanno Beckerd96a6522019-07-10 13:55:25 +01002770 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002771 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2774 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002775 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002776 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2779#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002780 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002782 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002783 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002784
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 /*
Paul Bakker45829992013-01-03 14:52:21 +01002786 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002789 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2790 {
2791 /* The ciphertext is prefixed with the CBC IV. */
2792 minlen += transform->ivlen;
2793 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794#endif
Paul Bakker45829992013-01-03 14:52:21 +01002795
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002796 /* Size considerations:
2797 *
2798 * - The CBC cipher text must not be empty and hence
2799 * at least of size transform->ivlen.
2800 *
2801 * Together with the potential IV-prefix, this explains
2802 * the first of the two checks below.
2803 *
2804 * - The record must contain a MAC, either in plain or
2805 * encrypted, depending on whether Encrypt-then-MAC
2806 * is used or not.
2807 * - If it is, the message contains the IV-prefix,
2808 * the CBC ciphertext, and the MAC.
2809 * - If it is not, the padded plaintext, and hence
2810 * the CBC ciphertext, has at least length maclen + 1
2811 * because there is at least the padding length byte.
2812 *
2813 * As the CBC ciphertext is not empty, both cases give the
2814 * lower bound minlen + maclen + 1 on the record size, which
2815 * we test for in the second check below.
2816 */
2817 if( rec->data_len < minlen + transform->ivlen ||
2818 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002821 "+ 1 ) ( + expl IV )", rec->data_len,
2822 transform->ivlen,
2823 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002825 }
2826
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002827 /*
2828 * Authenticate before decrypt if enabled
2829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002831 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002832 {
Hanno Becker992b6872017-11-09 18:57:39 +00002833 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002836
Hanno Beckerd96a6522019-07-10 13:55:25 +01002837 /* Update data_len in tandem with add_data.
2838 *
2839 * The subtraction is safe because of the previous check
2840 * data_len >= minlen + maclen + 1.
2841 *
2842 * Afterwards, we know that data + data_len is followed by at
2843 * least maclen Bytes, which justifies the call to
2844 * mbedtls_ssl_safer_memcmp() below.
2845 *
2846 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002847 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002848 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002849
Hanno Beckerd96a6522019-07-10 13:55:25 +01002850 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002851 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2852 add_data_len );
2853 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2854 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002855 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2856 data, rec->data_len );
2857 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2858 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002859
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002860 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2861 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002862 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002863 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002864
Hanno Beckerd96a6522019-07-10 13:55:25 +01002865 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002866 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2867 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002871 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002872 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002873 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002875
2876 /*
2877 * Check length sanity
2878 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002879
2880 /* We know from above that data_len > minlen >= 0,
2881 * so the following check in particular implies that
2882 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002883 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002886 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002888 }
2889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002891 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002892 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002893 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002894 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002895 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002896 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002897 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002898
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002899 data += transform->ivlen;
2900 rec->data_offset += transform->ivlen;
2901 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002902 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002904
Hanno Beckerd96a6522019-07-10 13:55:25 +01002905 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2906
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002907 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2908 transform->iv_dec, transform->ivlen,
2909 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002912 return( ret );
2913 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002914
Hanno Beckerd96a6522019-07-10 13:55:25 +01002915 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002916 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002920 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002923 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002924 {
2925 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002926 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
2927 * records is equivalent to CBC decryption of the concatenation
2928 * of the records; in other words, IVs are maintained across
2929 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02002930 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002931 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2932 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002934#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002936 /* Safe since data_len >= minlen + maclen + 1, so after having
2937 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01002938 * data_len > 0 (because of data_len % ivlen == 0, it's actually
2939 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002940 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002941
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002942 if( auth_done == 1 )
2943 {
2944 correct *= ( rec->data_len >= padlen + 1 );
2945 padlen *= ( rec->data_len >= padlen + 1 );
2946 }
2947 else
Paul Bakker45829992013-01-03 14:52:21 +01002948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002950 if( rec->data_len < transform->maclen + padlen + 1 )
2951 {
2952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2953 rec->data_len,
2954 transform->maclen,
2955 padlen + 1 ) );
2956 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002957#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002958
2959 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2960 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002963 padlen++;
2964
2965 /* Regardless of the validity of the padding,
2966 * we have data_len >= padlen here. */
2967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002969 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002970 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002971 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973#if defined(MBEDTLS_SSL_DEBUG_ALL)
2974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002975 "should be no more than %d",
2976 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002977#endif
Paul Bakker45829992013-01-03 14:52:21 +01002978 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 }
2980 }
2981 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2983#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2984 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002985 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002986 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002987 /* The padding check involves a series of up to 256
2988 * consecutive memory reads at the end of the record
2989 * plaintext buffer. In order to hide the length and
2990 * validity of the padding, always perform exactly
2991 * `min(256,plaintext_len)` reads (but take into account
2992 * only the last `padlen` bytes for the padding check). */
2993 size_t pad_count = 0;
2994 size_t real_count = 0;
2995 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002996
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002997 /* Index of first padding byte; it has been ensured above
2998 * that the subtraction is safe. */
2999 size_t const padding_idx = rec->data_len - padlen;
3000 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3001 size_t const start_idx = rec->data_len - num_checks;
3002 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003003
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003004 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003005 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003006 real_count |= ( idx >= padding_idx );
3007 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003008 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003009 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003012 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003014#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003015 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003016 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003017 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3019 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003023 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003024
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003025 /* If the padding was found to be invalid, padlen == 0
3026 * and the subtraction is safe. If the padding was found valid,
3027 * padlen hasn't been changed and the previous assertion
3028 * data_len >= padlen still holds. */
3029 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003030 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003031 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003033 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003037 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003039#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003041 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
3044 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003045 * Authenticate if not done yet.
3046 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 */
Hanno Becker52344c22018-01-03 15:24:20 +00003048#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003049 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003050 {
Hanno Becker992b6872017-11-09 18:57:39 +00003051 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003052
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003053 /* If the initial value of padlen was such that
3054 * data_len < maclen + padlen + 1, then padlen
3055 * got reset to 1, and the initial check
3056 * data_len >= minlen + maclen + 1
3057 * guarantees that at this point we still
3058 * have at least data_len >= maclen.
3059 *
3060 * If the initial value of padlen was such that
3061 * data_len >= maclen + padlen + 1, then we have
3062 * subtracted either padlen + 1 (if the padding was correct)
3063 * or 0 (if the padding was incorrect) since then,
3064 * hence data_len >= maclen in any case.
3065 */
3066 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003067 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003070 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003071 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003072 ssl_mac( &transform->md_ctx_dec,
3073 transform->mac_dec,
3074 data, rec->data_len,
3075 rec->ctr, rec->type,
3076 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003077 }
3078 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3080#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3081 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003082 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003083 {
3084 /*
3085 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003086 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003087 *
3088 * Known timing attacks:
3089 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3090 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003091 * To compensate for different timings for the MAC calculation
3092 * depending on how much padding was removed (which is determined
3093 * by padlen), process extra_run more blocks through the hash
3094 * function.
3095 *
3096 * The formula in the paper is
3097 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3098 * where L1 is the size of the header plus the decrypted message
3099 * plus CBC padding and L2 is the size of the header plus the
3100 * decrypted message. This is for an underlying hash function
3101 * with 64-byte blocks.
3102 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3103 * correctly. We round down instead of up, so -56 is the correct
3104 * value for our calculations instead of -55.
3105 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003106 * Repeat the formula rather than defining a block_size variable.
3107 * This avoids requiring division by a variable at runtime
3108 * (which would be marginally less efficient and would require
3109 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003110 */
3111 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003112 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003113
3114 /*
3115 * The next two sizes are the minimum and maximum values of
3116 * in_msglen over all padlen values.
3117 *
3118 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003119 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003120 *
3121 * Note that max_len + maclen is never more than the buffer
3122 * length, as we previously did in_msglen -= maclen too.
3123 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003124 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003125 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3126
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003127 memset( tmp, 0, sizeof( tmp ) );
3128
3129 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003130 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003131#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3132 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003133 case MBEDTLS_MD_MD5:
3134 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003135 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003136 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003137 extra_run =
3138 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3139 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003140 break;
3141#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003142#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003143 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003144 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003145 extra_run =
3146 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3147 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003148 break;
3149#endif
3150 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003152 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3153 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003154
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003155 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003156
Hanno Beckercab87e62019-04-29 13:52:53 +01003157 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3158 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003159 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3160 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003161 /* Make sure we access everything even when padlen > 0. This
3162 * makes the synchronisation requirements for just-in-time
3163 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003164 ssl_read_memory( data + rec->data_len, padlen );
3165 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003166
3167 /* Call mbedtls_md_process at least once due to cache attacks
3168 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003169 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003170 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003171
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003172 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003173
3174 /* Make sure we access all the memory that could contain the MAC,
3175 * before we check it in the next code block. This makes the
3176 * synchronisation requirements for just-in-time Prime+Probe
3177 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003178 ssl_read_memory( data + min_len,
3179 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003180 }
3181 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3183 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3186 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003188
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003189#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003190 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3191 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003192#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003193
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003194 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3195 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197#if defined(MBEDTLS_SSL_DEBUG_ALL)
3198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003199#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003200 correct = 0;
3201 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003202 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003203 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003204
3205 /*
3206 * Finally check the correct flag
3207 */
3208 if( correct == 0 )
3209 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003210#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003211
3212 /* Make extra sure authentication was performed, exactly once */
3213 if( auth_done != 1 )
3214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3216 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003217 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003218
Hanno Beckera0e20d02019-05-15 14:03:01 +01003219#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003220 if( rec->cid_len != 0 )
3221 {
3222 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3223 &rec->type );
3224 if( ret != 0 )
3225 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3226 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003227#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003230
3231 return( 0 );
3232}
3233
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003234#undef MAC_NONE
3235#undef MAC_PLAINTEXT
3236#undef MAC_CIPHERTEXT
3237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003239/*
3240 * Compression/decompression functions
3241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003243{
3244 int ret;
3245 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003246 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003247 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003248 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003251
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003252 if( len_pre == 0 )
3253 return( 0 );
3254
Paul Bakker2770fbd2012-07-03 13:30:23 +00003255 memcpy( msg_pre, ssl->out_msg, len_pre );
3256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003258 ssl->out_msglen ) );
3259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003261 ssl->out_msg, ssl->out_msglen );
3262
Paul Bakker48916f92012-09-16 19:57:18 +00003263 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3264 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3265 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003266 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003267
Paul Bakker48916f92012-09-16 19:57:18 +00003268 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003269 if( ret != Z_OK )
3270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3272 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003273 }
3274
Angus Grattond8213d02016-05-25 20:56:48 +10003275 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003276 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003279 ssl->out_msglen ) );
3280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003282 ssl->out_msg, ssl->out_msglen );
3283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003285
3286 return( 0 );
3287}
3288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003290{
3291 int ret;
3292 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003293 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003294 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003295 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003298
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003299 if( len_pre == 0 )
3300 return( 0 );
3301
Paul Bakker2770fbd2012-07-03 13:30:23 +00003302 memcpy( msg_pre, ssl->in_msg, len_pre );
3303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003305 ssl->in_msglen ) );
3306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003308 ssl->in_msg, ssl->in_msglen );
3309
Paul Bakker48916f92012-09-16 19:57:18 +00003310 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3311 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3312 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003313 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003314 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003315
Paul Bakker48916f92012-09-16 19:57:18 +00003316 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003317 if( ret != Z_OK )
3318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3320 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003321 }
3322
Angus Grattond8213d02016-05-25 20:56:48 +10003323 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003324 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003327 ssl->in_msglen ) );
3328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003330 ssl->in_msg, ssl->in_msglen );
3331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003333
3334 return( 0 );
3335}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3339static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341#if defined(MBEDTLS_SSL_PROTO_DTLS)
3342static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003343{
3344 /* If renegotiation is not enforced, retransmit until we would reach max
3345 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003346 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003347 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003348 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003349 unsigned char doublings = 1;
3350
3351 while( ratio != 0 )
3352 {
3353 ++doublings;
3354 ratio >>= 1;
3355 }
3356
3357 if( ++ssl->renego_records_seen > doublings )
3358 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003360 return( 0 );
3361 }
3362 }
3363
3364 return( ssl_write_hello_request( ssl ) );
3365}
3366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003368
Paul Bakker5121ce52009-01-03 21:22:43 +00003369/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003370 * Fill the input message buffer by appending data to it.
3371 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003372 *
3373 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3374 * available (from this read and/or a previous one). Otherwise, an error code
3375 * is returned (possibly EOF or WANT_READ).
3376 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003377 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3378 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3379 * since we always read a whole datagram at once.
3380 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003381 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003382 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003385{
Paul Bakker23986e52011-04-24 08:57:21 +00003386 int ret;
3387 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003390
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003391 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003394 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003396 }
3397
Angus Grattond8213d02016-05-25 20:56:48 +10003398 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003402 }
3403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003405 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003406 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003407 uint32_t timeout;
3408
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003409 /* Just to be sure */
3410 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3411 {
3412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3413 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3415 }
3416
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003417 /*
3418 * The point is, we need to always read a full datagram at once, so we
3419 * sometimes read more then requested, and handle the additional data.
3420 * It could be the rest of the current record (while fetching the
3421 * header) and/or some other records in the same datagram.
3422 */
3423
3424 /*
3425 * Move to the next record in the already read datagram if applicable
3426 */
3427 if( ssl->next_record_offset != 0 )
3428 {
3429 if( ssl->in_left < ssl->next_record_offset )
3430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3432 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003433 }
3434
3435 ssl->in_left -= ssl->next_record_offset;
3436
3437 if( ssl->in_left != 0 )
3438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003440 ssl->next_record_offset ) );
3441 memmove( ssl->in_hdr,
3442 ssl->in_hdr + ssl->next_record_offset,
3443 ssl->in_left );
3444 }
3445
3446 ssl->next_record_offset = 0;
3447 }
3448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003451
3452 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003453 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003454 */
3455 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003458 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003459 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003460
3461 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003462 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003463 * are not at the beginning of a new record, the caller did something
3464 * wrong.
3465 */
3466 if( ssl->in_left != 0 )
3467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003470 }
3471
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003472 /*
3473 * Don't even try to read if time's out already.
3474 * This avoids by-passing the timer when repeatedly receiving messages
3475 * that will end up being dropped.
3476 */
3477 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003478 {
3479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003480 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003481 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003482 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003483 {
Angus Grattond8213d02016-05-25 20:56:48 +10003484 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003487 timeout = ssl->handshake->retransmit_timeout;
3488 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003489 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003492
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003493 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003494 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3495 timeout );
3496 else
3497 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003500
3501 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003503 }
3504
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003505 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003508 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003511 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003512 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003515 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003516 }
3517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003521 return( ret );
3522 }
3523
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003524 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003527 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003529 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003530 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003533 return( ret );
3534 }
3535
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003536 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003537 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003539 }
3540
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 if( ret < 0 )
3542 return( ret );
3543
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003544 ssl->in_left = ret;
3545 }
3546 else
3547#endif
3548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003550 ssl->in_left, nb_want ) );
3551
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003552 while( ssl->in_left < nb_want )
3553 {
3554 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003555
3556 if( ssl_check_timer( ssl ) != 0 )
3557 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3558 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003559 {
3560 if( ssl->f_recv_timeout != NULL )
3561 {
3562 ret = ssl->f_recv_timeout( ssl->p_bio,
3563 ssl->in_hdr + ssl->in_left, len,
3564 ssl->conf->read_timeout );
3565 }
3566 else
3567 {
3568 ret = ssl->f_recv( ssl->p_bio,
3569 ssl->in_hdr + ssl->in_left, len );
3570 }
3571 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003574 ssl->in_left, nb_want ) );
3575 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003576
3577 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003579
3580 if( ret < 0 )
3581 return( ret );
3582
mohammad160352aecb92018-03-28 23:41:40 -07003583 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003584 {
Darryl Green11999bb2018-03-13 15:22:58 +00003585 MBEDTLS_SSL_DEBUG_MSG( 1,
3586 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003587 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3589 }
3590
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003591 ssl->in_left += ret;
3592 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 }
3594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003596
3597 return( 0 );
3598}
3599
3600/*
3601 * Flush any data not yet written
3602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003604{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003605 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003606 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003610 if( ssl->f_send == NULL )
3611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003613 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003615 }
3616
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003617 /* Avoid incrementing counter if data is flushed */
3618 if( ssl->out_left == 0 )
3619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003621 return( 0 );
3622 }
3623
Paul Bakker5121ce52009-01-03 21:22:43 +00003624 while( ssl->out_left > 0 )
3625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003627 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003628
Hanno Becker2b1e3542018-08-06 11:19:13 +01003629 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003630 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003633
3634 if( ret <= 0 )
3635 return( ret );
3636
mohammad160352aecb92018-03-28 23:41:40 -07003637 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003638 {
Darryl Green11999bb2018-03-13 15:22:58 +00003639 MBEDTLS_SSL_DEBUG_MSG( 1,
3640 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003641 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3643 }
3644
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 ssl->out_left -= ret;
3646 }
3647
Hanno Becker2b1e3542018-08-06 11:19:13 +01003648#if defined(MBEDTLS_SSL_PROTO_DTLS)
3649 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003650 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003651 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003652 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003653 else
3654#endif
3655 {
3656 ssl->out_hdr = ssl->out_buf + 8;
3657 }
3658 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003661
3662 return( 0 );
3663}
3664
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003665/*
3666 * Functions to handle the DTLS retransmission state machine
3667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003669/*
3670 * Append current handshake message to current outgoing flight
3671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003673{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3676 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3677 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003678
3679 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003680 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003681 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003684 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003685 }
3686
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003687 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003688 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003691 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003692 }
3693
3694 /* Copy current handshake message with headers */
3695 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3696 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003697 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003698 msg->next = NULL;
3699
3700 /* Append to the current flight */
3701 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003702 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003703 else
3704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003706 while( cur->next != NULL )
3707 cur = cur->next;
3708 cur->next = msg;
3709 }
3710
Hanno Becker3b235902018-08-06 09:54:53 +01003711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003712 return( 0 );
3713}
3714
3715/*
3716 * Free the current flight of handshake messages
3717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003719{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 mbedtls_ssl_flight_item *cur = flight;
3721 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003722
3723 while( cur != NULL )
3724 {
3725 next = cur->next;
3726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 mbedtls_free( cur->p );
3728 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003729
3730 cur = next;
3731 }
3732}
3733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3735static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003736#endif
3737
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003738/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003739 * Swap transform_out and out_ctr with the alternative ones
3740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003742{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003744 unsigned char tmp_out_ctr[8];
3745
3746 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003749 return;
3750 }
3751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003753
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003754 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003755 tmp_transform = ssl->transform_out;
3756 ssl->transform_out = ssl->handshake->alt_transform_out;
3757 ssl->handshake->alt_transform_out = tmp_transform;
3758
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003759 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003760 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3761 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003762 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003763
3764 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003765 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3768 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3773 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003774 }
3775 }
3776#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003777}
3778
3779/*
3780 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003781 */
3782int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3783{
3784 int ret = 0;
3785
3786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3787
3788 ret = mbedtls_ssl_flight_transmit( ssl );
3789
3790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3791
3792 return( ret );
3793}
3794
3795/*
3796 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003797 *
3798 * Need to remember the current message in case flush_output returns
3799 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003800 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003801 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003802int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003803{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003804 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003807 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003808 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003810
3811 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003812 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003813 ssl_swap_epochs( ssl );
3814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003816 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003817
3818 while( ssl->handshake->cur_msg != NULL )
3819 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003821 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003822
Hanno Beckere1dcb032018-08-17 16:47:58 +01003823 int const is_finished =
3824 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3825 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3826
Hanno Becker04da1892018-08-14 13:22:10 +01003827 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3828 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3829
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003830 /* Swap epochs before sending Finished: we can't do it after
3831 * sending ChangeCipherSpec, in case write returns WANT_READ.
3832 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003833 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003834 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003836 ssl_swap_epochs( ssl );
3837 }
3838
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 ret = ssl_get_remaining_payload_in_datagram( ssl );
3840 if( ret < 0 )
3841 return( ret );
3842 max_frag_len = (size_t) ret;
3843
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003844 /* CCS is copied as is, while HS messages may need fragmentation */
3845 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3846 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003847 if( max_frag_len == 0 )
3848 {
3849 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3850 return( ret );
3851
3852 continue;
3853 }
3854
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003855 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003856 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003857 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003858
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003859 /* Update position inside current message */
3860 ssl->handshake->cur_msg_p += cur->len;
3861 }
3862 else
3863 {
3864 const unsigned char * const p = ssl->handshake->cur_msg_p;
3865 const size_t hs_len = cur->len - 12;
3866 const size_t frag_off = p - ( cur->p + 12 );
3867 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003868 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003869
Hanno Beckere1dcb032018-08-17 16:47:58 +01003870 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003871 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003872 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003873 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003874
Hanno Becker67bc7c32018-08-06 11:33:50 +01003875 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3876 return( ret );
3877
3878 continue;
3879 }
3880 max_hs_frag_len = max_frag_len - 12;
3881
3882 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3883 max_hs_frag_len : rem_len;
3884
3885 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003886 {
3887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003888 (unsigned) cur_hs_frag_len,
3889 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003890 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003891
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003892 /* Messages are stored with handshake headers as if not fragmented,
3893 * copy beginning of headers then fill fragmentation fields.
3894 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3895 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003896
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003897 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3898 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3899 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3900
Hanno Becker67bc7c32018-08-06 11:33:50 +01003901 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3902 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3903 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003904
3905 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3906
Hanno Becker3f7b9732018-08-28 09:53:25 +01003907 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003908 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3909 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003910 ssl->out_msgtype = cur->type;
3911
3912 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003913 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003914 }
3915
3916 /* If done with the current message move to the next one if any */
3917 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3918 {
3919 if( cur->next != NULL )
3920 {
3921 ssl->handshake->cur_msg = cur->next;
3922 ssl->handshake->cur_msg_p = cur->next->p + 12;
3923 }
3924 else
3925 {
3926 ssl->handshake->cur_msg = NULL;
3927 ssl->handshake->cur_msg_p = NULL;
3928 }
3929 }
3930
3931 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003932 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003935 return( ret );
3936 }
3937 }
3938
Hanno Becker67bc7c32018-08-06 11:33:50 +01003939 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3940 return( ret );
3941
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003942 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3944 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003945 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003948 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3949 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003950
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003952
3953 return( 0 );
3954}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003955
3956/*
3957 * To be called when the last message of an incoming flight is received.
3958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003960{
3961 /* We won't need to resend that one any more */
3962 ssl_flight_free( ssl->handshake->flight );
3963 ssl->handshake->flight = NULL;
3964 ssl->handshake->cur_msg = NULL;
3965
3966 /* The next incoming flight will start with this msg_seq */
3967 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3968
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003969 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003970 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003971
Hanno Becker0271f962018-08-16 13:23:47 +01003972 /* Clear future message buffering structure. */
3973 ssl_buffering_free( ssl );
3974
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003975 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003976 ssl_set_timer( ssl, 0 );
3977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3979 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003982 }
3983 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003985}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003986
3987/*
3988 * To be called when the last message of an outgoing flight is send.
3989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003991{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003992 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003993 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3996 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003999 }
4000 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004002}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004004
Paul Bakker5121ce52009-01-03 21:22:43 +00004005/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004006 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004007 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004008
4009/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004010 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004011 *
4012 * - fill in handshake headers
4013 * - update handshake checksum
4014 * - DTLS: save message for resending
4015 * - then pass to the record layer
4016 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004017 * DTLS: except for HelloRequest, messages are only queued, and will only be
4018 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004019 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004020 * Inputs:
4021 * - ssl->out_msglen: 4 + actual handshake message len
4022 * (4 is the size of handshake headers for TLS)
4023 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4024 * - ssl->out_msg + 4: the handshake message body
4025 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004026 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004027 * - ssl->out_msglen: the length of the record contents
4028 * (including handshake headers but excluding record headers)
4029 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004030 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004031int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004032{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004033 int ret;
4034 const size_t hs_len = ssl->out_msglen - 4;
4035 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004036
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4038
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004039 /*
4040 * Sanity checks
4041 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004042 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004043 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4044 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004045 /* In SSLv3, the client might send a NoCertificate alert. */
4046#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4047 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4048 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4049 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4050#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4051 {
4052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4054 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004056
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004057 /* Whenever we send anything different from a
4058 * HelloRequest we should be in a handshake - double check. */
4059 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4060 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004061 ssl->handshake == NULL )
4062 {
4063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4065 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004068 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004069 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004071 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004074 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004075#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004076
Hanno Beckerb50a2532018-08-06 11:52:54 +01004077 /* Double-check that we did not exceed the bounds
4078 * of the outgoing record buffer.
4079 * This should never fail as the various message
4080 * writing functions must obey the bounds of the
4081 * outgoing record buffer, but better be safe.
4082 *
4083 * Note: We deliberately do not check for the MTU or MFL here.
4084 */
4085 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4086 {
4087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4088 "size %u, maximum %u",
4089 (unsigned) ssl->out_msglen,
4090 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4091 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4092 }
4093
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004094 /*
4095 * Fill handshake headers
4096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004098 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004099 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4100 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4101 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004102
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004103 /*
4104 * DTLS has additional fields in the Handshake layer,
4105 * between the length field and the actual payload:
4106 * uint16 message_seq;
4107 * uint24 fragment_offset;
4108 * uint24 fragment_length;
4109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004111 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004112 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004113 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004114 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004115 {
4116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4117 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004118 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004119 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4121 }
4122
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004123 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004124 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004125
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004126 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004127 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004128 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004129 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4130 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4131 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004132 }
4133 else
4134 {
4135 ssl->out_msg[4] = 0;
4136 ssl->out_msg[5] = 0;
4137 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004138
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004139 /* Handshake hashes are computed without fragmentation,
4140 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004141 memset( ssl->out_msg + 6, 0x00, 3 );
4142 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004143 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004145
Hanno Becker0207e532018-08-28 10:28:28 +01004146 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004147 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4148 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004149 }
4150
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004151 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004153 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004154 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4155 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004156 {
4157 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004160 return( ret );
4161 }
4162 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004163 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004164#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004165 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004166 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004167 {
4168 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4169 return( ret );
4170 }
4171 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004172
4173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4174
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004175 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004176}
4177
4178/*
4179 * Record layer functions
4180 */
4181
4182/*
4183 * Write current record.
4184 *
4185 * Uses:
4186 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4187 * - ssl->out_msglen: length of the record content (excl headers)
4188 * - ssl->out_msg: record content
4189 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004190int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004191{
4192 int ret, done = 0;
4193 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004194 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004195
4196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004199 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004201 {
4202 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004205 return( ret );
4206 }
4207
4208 len = ssl->out_msglen;
4209 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4213 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217 ret = mbedtls_ssl_hw_record_write( ssl );
4218 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4221 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004222 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004223
4224 if( ret == 0 )
4225 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004226 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004228 if( !done )
4229 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004230 unsigned i;
4231 size_t protected_record_size;
4232
Hanno Becker6430faf2019-05-08 11:57:13 +01004233 /* Skip writing the record content type to after the encryption,
4234 * as it may change when using the CID extension. */
4235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004236 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004237 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004238
Hanno Becker19859472018-08-06 09:40:20 +01004239 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004240 ssl->out_len[0] = (unsigned char)( len >> 8 );
4241 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004242
Paul Bakker48916f92012-09-16 19:57:18 +00004243 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004244 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004245 mbedtls_record rec;
4246
4247 rec.buf = ssl->out_iv;
4248 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4249 ( ssl->out_iv - ssl->out_buf );
4250 rec.data_len = ssl->out_msglen;
4251 rec.data_offset = ssl->out_msg - rec.buf;
4252
4253 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4254 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4255 ssl->conf->transport, rec.ver );
4256 rec.type = ssl->out_msgtype;
4257
Hanno Beckera0e20d02019-05-15 14:03:01 +01004258#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004259 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004260 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004261#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004262
Hanno Beckera18d1322018-01-03 14:27:32 +00004263 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004264 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004267 return( ret );
4268 }
4269
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004270 if( rec.data_offset != 0 )
4271 {
4272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4273 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4274 }
4275
Hanno Becker6430faf2019-05-08 11:57:13 +01004276 /* Update the record content type and CID. */
4277 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004278#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004279 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004280#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004281 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004282 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4283 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004284 }
4285
Hanno Becker5903de42019-05-03 14:46:38 +01004286 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004287
4288#if defined(MBEDTLS_SSL_PROTO_DTLS)
4289 /* In case of DTLS, double-check that we don't exceed
4290 * the remaining space in the datagram. */
4291 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4292 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004293 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004294 if( ret < 0 )
4295 return( ret );
4296
4297 if( protected_record_size > (size_t) ret )
4298 {
4299 /* Should never happen */
4300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4301 }
4302 }
4303#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004304
Hanno Becker6430faf2019-05-08 11:57:13 +01004305 /* Now write the potentially updated record content type. */
4306 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004309 "version = [%d:%d], msglen = %d",
4310 ssl->out_hdr[0], ssl->out_hdr[1],
4311 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004314 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004315
4316 ssl->out_left += protected_record_size;
4317 ssl->out_hdr += protected_record_size;
4318 ssl_update_out_pointers( ssl, ssl->transform_out );
4319
Hanno Becker04484622018-08-06 09:49:38 +01004320 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4321 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4322 break;
4323
4324 /* The loop goes to its end iff the counter is wrapping */
4325 if( i == ssl_ep_len( ssl ) )
4326 {
4327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4328 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4329 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004330 }
4331
Hanno Becker67bc7c32018-08-06 11:33:50 +01004332#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004333 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4334 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004335 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004336 size_t remaining;
4337 ret = ssl_get_remaining_payload_in_datagram( ssl );
4338 if( ret < 0 )
4339 {
4340 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4341 ret );
4342 return( ret );
4343 }
4344
4345 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004346 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004347 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004348 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004349 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004350 else
4351 {
Hanno Becker513815a2018-08-20 11:56:09 +01004352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004353 }
4354 }
4355#endif /* MBEDTLS_SSL_PROTO_DTLS */
4356
4357 if( ( flush == SSL_FORCE_FLUSH ) &&
4358 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004361 return( ret );
4362 }
4363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004365
4366 return( 0 );
4367}
4368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004370
4371static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4372{
4373 if( ssl->in_msglen < ssl->in_hslen ||
4374 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4375 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4376 {
4377 return( 1 );
4378 }
4379 return( 0 );
4380}
Hanno Becker44650b72018-08-16 12:51:11 +01004381
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004382static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004383{
4384 return( ( ssl->in_msg[9] << 16 ) |
4385 ( ssl->in_msg[10] << 8 ) |
4386 ssl->in_msg[11] );
4387}
4388
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004389static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004390{
4391 return( ( ssl->in_msg[6] << 16 ) |
4392 ( ssl->in_msg[7] << 8 ) |
4393 ssl->in_msg[8] );
4394}
4395
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004396static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004397{
4398 uint32_t msg_len, frag_off, frag_len;
4399
4400 msg_len = ssl_get_hs_total_len( ssl );
4401 frag_off = ssl_get_hs_frag_off( ssl );
4402 frag_len = ssl_get_hs_frag_len( ssl );
4403
4404 if( frag_off > msg_len )
4405 return( -1 );
4406
4407 if( frag_len > msg_len - frag_off )
4408 return( -1 );
4409
4410 if( frag_len + 12 > ssl->in_msglen )
4411 return( -1 );
4412
4413 return( 0 );
4414}
4415
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004416/*
4417 * Mark bits in bitmask (used for DTLS HS reassembly)
4418 */
4419static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4420{
4421 unsigned int start_bits, end_bits;
4422
4423 start_bits = 8 - ( offset % 8 );
4424 if( start_bits != 8 )
4425 {
4426 size_t first_byte_idx = offset / 8;
4427
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004428 /* Special case */
4429 if( len <= start_bits )
4430 {
4431 for( ; len != 0; len-- )
4432 mask[first_byte_idx] |= 1 << ( start_bits - len );
4433
4434 /* Avoid potential issues with offset or len becoming invalid */
4435 return;
4436 }
4437
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004438 offset += start_bits; /* Now offset % 8 == 0 */
4439 len -= start_bits;
4440
4441 for( ; start_bits != 0; start_bits-- )
4442 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4443 }
4444
4445 end_bits = len % 8;
4446 if( end_bits != 0 )
4447 {
4448 size_t last_byte_idx = ( offset + len ) / 8;
4449
4450 len -= end_bits; /* Now len % 8 == 0 */
4451
4452 for( ; end_bits != 0; end_bits-- )
4453 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4454 }
4455
4456 memset( mask + offset / 8, 0xFF, len / 8 );
4457}
4458
4459/*
4460 * Check that bitmask is full
4461 */
4462static int ssl_bitmask_check( unsigned char *mask, size_t len )
4463{
4464 size_t i;
4465
4466 for( i = 0; i < len / 8; i++ )
4467 if( mask[i] != 0xFF )
4468 return( -1 );
4469
4470 for( i = 0; i < len % 8; i++ )
4471 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4472 return( -1 );
4473
4474 return( 0 );
4475}
4476
Hanno Becker56e205e2018-08-16 09:06:12 +01004477/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004478static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004479 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004480{
Hanno Becker56e205e2018-08-16 09:06:12 +01004481 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004482
Hanno Becker56e205e2018-08-16 09:06:12 +01004483 alloc_len = 12; /* Handshake header */
4484 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004485
Hanno Beckerd07df862018-08-16 09:14:58 +01004486 if( add_bitmap )
4487 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004488
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004489 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004490}
Hanno Becker56e205e2018-08-16 09:06:12 +01004491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004493
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004494static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004495{
4496 return( ( ssl->in_msg[1] << 16 ) |
4497 ( ssl->in_msg[2] << 8 ) |
4498 ssl->in_msg[3] );
4499}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004500
Simon Butcher99000142016-10-13 17:21:01 +01004501int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004503 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004506 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004508 }
4509
Hanno Becker12555c62018-08-16 12:47:53 +01004510 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004513 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004514 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004517 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004518 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004519 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004520 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004521
Hanno Becker44650b72018-08-16 12:51:11 +01004522 if( ssl_check_hs_header( ssl ) != 0 )
4523 {
4524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4525 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4526 }
4527
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004528 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004529 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4530 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4531 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4532 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004533 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004534 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4535 {
4536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4537 recv_msg_seq,
4538 ssl->handshake->in_msg_seq ) );
4539 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4540 }
4541
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004542 /* Retransmit only on last message from previous flight, to avoid
4543 * too many retransmissions.
4544 * Besides, No sane server ever retransmits HelloVerifyRequest */
4545 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004549 "message_seq = %d, start_of_flight = %d",
4550 recv_msg_seq,
4551 ssl->handshake->in_flight_start_seq ) );
4552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004556 return( ret );
4557 }
4558 }
4559 else
4560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004562 "message_seq = %d, expected = %d",
4563 recv_msg_seq,
4564 ssl->handshake->in_msg_seq ) );
4565 }
4566
Hanno Becker90333da2017-10-10 11:27:13 +01004567 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004568 }
4569 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004570
Hanno Becker6d97ef52018-08-16 13:09:04 +01004571 /* Message reassembly is handled alongside buffering of future
4572 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004573 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004574 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004575 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004578 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004579 }
4580 }
4581 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004583 /* With TLS we don't handle fragmentation (for now) */
4584 if( ssl->in_msglen < ssl->in_hslen )
4585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4587 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004588 }
4589
Simon Butcher99000142016-10-13 17:21:01 +01004590 return( 0 );
4591}
4592
4593void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4594{
Hanno Becker0271f962018-08-16 13:23:47 +01004595 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004596
Hanno Becker0271f962018-08-16 13:23:47 +01004597 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004598 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004599 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004600 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004601
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004602 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004604 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004605 ssl->handshake != NULL )
4606 {
Hanno Becker0271f962018-08-16 13:23:47 +01004607 unsigned offset;
4608 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004609
Hanno Becker0271f962018-08-16 13:23:47 +01004610 /* Increment handshake sequence number */
4611 hs->in_msg_seq++;
4612
4613 /*
4614 * Clear up handshake buffering and reassembly structure.
4615 */
4616
4617 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004618 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004619
4620 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004621 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4622 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004623 offset++, hs_buf++ )
4624 {
4625 *hs_buf = *(hs_buf + 1);
4626 }
4627
4628 /* Create a fresh last entry */
4629 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004630 }
4631#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004632}
4633
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004634/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004635 * DTLS anti-replay: RFC 6347 4.1.2.6
4636 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004637 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4638 * Bit n is set iff record number in_window_top - n has been seen.
4639 *
4640 * Usually, in_window_top is the last record number seen and the lsb of
4641 * in_window is set. The only exception is the initial state (record number 0
4642 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4645static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004646{
4647 ssl->in_window_top = 0;
4648 ssl->in_window = 0;
4649}
4650
4651static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4652{
4653 return( ( (uint64_t) buf[0] << 40 ) |
4654 ( (uint64_t) buf[1] << 32 ) |
4655 ( (uint64_t) buf[2] << 24 ) |
4656 ( (uint64_t) buf[3] << 16 ) |
4657 ( (uint64_t) buf[4] << 8 ) |
4658 ( (uint64_t) buf[5] ) );
4659}
4660
4661/*
4662 * Return 0 if sequence number is acceptable, -1 otherwise
4663 */
Hanno Becker0183d692019-07-12 08:50:37 +01004664int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004665{
4666 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4667 uint64_t bit;
4668
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004669 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004670 return( 0 );
4671
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004672 if( rec_seqnum > ssl->in_window_top )
4673 return( 0 );
4674
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004675 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004676
4677 if( bit >= 64 )
4678 return( -1 );
4679
4680 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4681 return( -1 );
4682
4683 return( 0 );
4684}
4685
4686/*
4687 * Update replay window on new validated record
4688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004690{
4691 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4692
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004693 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004694 return;
4695
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004696 if( rec_seqnum > ssl->in_window_top )
4697 {
4698 /* Update window_top and the contents of the window */
4699 uint64_t shift = rec_seqnum - ssl->in_window_top;
4700
4701 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004702 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004703 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004704 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004705 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004706 ssl->in_window |= 1;
4707 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004708
4709 ssl->in_window_top = rec_seqnum;
4710 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004711 else
4712 {
4713 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004714 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004715
4716 if( bit < 64 ) /* Always true, but be extra sure */
4717 ssl->in_window |= (uint64_t) 1 << bit;
4718 }
4719}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004721
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004722#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004723/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004724static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4725
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004726/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004727 * Without any SSL context, check if a datagram looks like a ClientHello with
4728 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004729 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004730 *
4731 * - if cookie is valid, return 0
4732 * - if ClientHello looks superficially valid but cookie is not,
4733 * fill obuf and set olen, then
4734 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4735 * - otherwise return a specific error code
4736 */
4737static int ssl_check_dtls_clihlo_cookie(
4738 mbedtls_ssl_cookie_write_t *f_cookie_write,
4739 mbedtls_ssl_cookie_check_t *f_cookie_check,
4740 void *p_cookie,
4741 const unsigned char *cli_id, size_t cli_id_len,
4742 const unsigned char *in, size_t in_len,
4743 unsigned char *obuf, size_t buf_len, size_t *olen )
4744{
4745 size_t sid_len, cookie_len;
4746 unsigned char *p;
4747
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004748 /*
4749 * Structure of ClientHello with record and handshake headers,
4750 * and expected values. We don't need to check a lot, more checks will be
4751 * done when actually parsing the ClientHello - skipping those checks
4752 * avoids code duplication and does not make cookie forging any easier.
4753 *
4754 * 0-0 ContentType type; copied, must be handshake
4755 * 1-2 ProtocolVersion version; copied
4756 * 3-4 uint16 epoch; copied, must be 0
4757 * 5-10 uint48 sequence_number; copied
4758 * 11-12 uint16 length; (ignored)
4759 *
4760 * 13-13 HandshakeType msg_type; (ignored)
4761 * 14-16 uint24 length; (ignored)
4762 * 17-18 uint16 message_seq; copied
4763 * 19-21 uint24 fragment_offset; copied, must be 0
4764 * 22-24 uint24 fragment_length; (ignored)
4765 *
4766 * 25-26 ProtocolVersion client_version; (ignored)
4767 * 27-58 Random random; (ignored)
4768 * 59-xx SessionID session_id; 1 byte len + sid_len content
4769 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4770 * ...
4771 *
4772 * Minimum length is 61 bytes.
4773 */
4774 if( in_len < 61 ||
4775 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4776 in[3] != 0 || in[4] != 0 ||
4777 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4778 {
4779 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4780 }
4781
4782 sid_len = in[59];
4783 if( sid_len > in_len - 61 )
4784 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4785
4786 cookie_len = in[60 + sid_len];
4787 if( cookie_len > in_len - 60 )
4788 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4789
4790 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4791 cli_id, cli_id_len ) == 0 )
4792 {
4793 /* Valid cookie */
4794 return( 0 );
4795 }
4796
4797 /*
4798 * If we get here, we've got an invalid cookie, let's prepare HVR.
4799 *
4800 * 0-0 ContentType type; copied
4801 * 1-2 ProtocolVersion version; copied
4802 * 3-4 uint16 epoch; copied
4803 * 5-10 uint48 sequence_number; copied
4804 * 11-12 uint16 length; olen - 13
4805 *
4806 * 13-13 HandshakeType msg_type; hello_verify_request
4807 * 14-16 uint24 length; olen - 25
4808 * 17-18 uint16 message_seq; copied
4809 * 19-21 uint24 fragment_offset; copied
4810 * 22-24 uint24 fragment_length; olen - 25
4811 *
4812 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4813 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4814 *
4815 * Minimum length is 28.
4816 */
4817 if( buf_len < 28 )
4818 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4819
4820 /* Copy most fields and adapt others */
4821 memcpy( obuf, in, 25 );
4822 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4823 obuf[25] = 0xfe;
4824 obuf[26] = 0xff;
4825
4826 /* Generate and write actual cookie */
4827 p = obuf + 28;
4828 if( f_cookie_write( p_cookie,
4829 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4830 {
4831 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4832 }
4833
4834 *olen = p - obuf;
4835
4836 /* Go back and fill length fields */
4837 obuf[27] = (unsigned char)( *olen - 28 );
4838
4839 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4840 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4841 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4842
4843 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4844 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4845
4846 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4847}
4848
4849/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004850 * Handle possible client reconnect with the same UDP quadruplet
4851 * (RFC 6347 Section 4.2.8).
4852 *
4853 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4854 * that looks like a ClientHello.
4855 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004856 * - if the input looks like a ClientHello without cookies,
4857 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004858 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004859 * - if the input looks like a ClientHello with a valid cookie,
4860 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004861 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004862 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004863 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004864 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004865 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4866 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004867 */
4868static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4869{
4870 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004871 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004872
Hanno Becker2fddd372019-07-10 14:37:41 +01004873 if( ssl->conf->f_cookie_write == NULL ||
4874 ssl->conf->f_cookie_check == NULL )
4875 {
4876 /* If we can't use cookies to verify reachability of the peer,
4877 * drop the record. */
4878 return( 0 );
4879 }
4880
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004881 ret = ssl_check_dtls_clihlo_cookie(
4882 ssl->conf->f_cookie_write,
4883 ssl->conf->f_cookie_check,
4884 ssl->conf->p_cookie,
4885 ssl->cli_id, ssl->cli_id_len,
4886 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004887 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004888
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004889 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4890
4891 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004892 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004893 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004894 * If the error is permanent we'll catch it later,
4895 * if it's not, then hopefully it'll work next time. */
4896 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004897 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004898 }
4899
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004900 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004901 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004902 /* Got a valid cookie, partially reset context */
4903 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4904 {
4905 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4906 return( ret );
4907 }
4908
4909 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004910 }
4911
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004912 return( ret );
4913}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004914#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004915
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004916static int ssl_check_record_type( uint8_t record_type )
4917{
4918 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4919 record_type != MBEDTLS_SSL_MSG_ALERT &&
4920 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4921 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4922 {
4923 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4924 }
4925
4926 return( 0 );
4927}
4928
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004929/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004930 * ContentType type;
4931 * ProtocolVersion version;
4932 * uint16 epoch; // DTLS only
4933 * uint48 sequence_number; // DTLS only
4934 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004935 *
4936 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004937 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004938 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4939 *
4940 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004941 * 1. proceed with the record if this function returns 0
4942 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4943 * 3. return CLIENT_RECONNECT if this function return that value
4944 * 4. drop the whole datagram if this function returns anything else.
4945 * Point 2 is needed when the peer is resending, and we have already received
4946 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004947 */
Hanno Becker331de3d2019-07-12 11:10:16 +01004948static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01004949 unsigned char *buf,
4950 size_t len,
4951 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00004952{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004953 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004954
Hanno Beckere5e7e782019-07-11 12:29:35 +01004955 size_t const rec_hdr_type_offset = 0;
4956 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004957
Hanno Beckere5e7e782019-07-11 12:29:35 +01004958 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
4959 rec_hdr_type_len;
4960 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00004961
Hanno Beckere5e7e782019-07-11 12:29:35 +01004962 size_t const rec_hdr_ctr_len = 8;
4963#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01004964 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004965 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
4966 rec_hdr_version_len;
4967
Hanno Beckera0e20d02019-05-15 14:03:01 +01004968#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01004969 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
4970 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01004971 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004972#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4973#endif /* MBEDTLS_SSL_PROTO_DTLS */
4974
4975 size_t rec_hdr_len_offset; /* To be determined */
4976 size_t const rec_hdr_len_len = 2;
4977
4978 /*
4979 * Check minimum lengths for record header.
4980 */
4981
4982#if defined(MBEDTLS_SSL_PROTO_DTLS)
4983 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4984 {
4985 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
4986 }
4987 else
4988#endif /* MBEDTLS_SSL_PROTO_DTLS */
4989 {
4990 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
4991 }
4992
4993 if( len < rec_hdr_len_offset + rec_hdr_len_len )
4994 {
4995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
4996 (unsigned) len,
4997 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
4998 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4999 }
5000
5001 /*
5002 * Parse and validate record content type
5003 */
5004
5005 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005006
5007 /* Check record content type */
5008#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5009 rec->cid_len = 0;
5010
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005011 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005012 ssl->conf->cid_len != 0 &&
5013 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005014 {
5015 /* Shift pointers to account for record header including CID
5016 * struct {
5017 * ContentType special_type = tls12_cid;
5018 * ProtocolVersion version;
5019 * uint16 epoch;
5020 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005021 * opaque cid[cid_length]; // Additional field compared to
5022 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005023 * uint16 length;
5024 * opaque enc_content[DTLSCiphertext.length];
5025 * } DTLSCiphertext;
5026 */
5027
5028 /* So far, we only support static CID lengths
5029 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005030 rec_hdr_cid_len = ssl->conf->cid_len;
5031 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005032
Hanno Beckere5e7e782019-07-11 12:29:35 +01005033 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005034 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5036 (unsigned) len,
5037 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005038 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005039 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005040
5041 rec->cid_len = rec_hdr_cid_len;
5042 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005043 }
5044 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005045#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005046 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005047 if( ssl_check_record_type( rec->type ) )
5048 {
Hanno Becker54229812019-07-12 14:40:00 +01005049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5050 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005051 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5052 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005053 }
5054
Hanno Beckere5e7e782019-07-11 12:29:35 +01005055 /*
5056 * Parse and validate record version
5057 */
5058
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005059 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5060 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005061 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5062 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005063 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005064
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005065 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5068 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005069 }
5070
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005071 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5074 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005075 }
5076
Hanno Beckere5e7e782019-07-11 12:29:35 +01005077 /*
5078 * Parse/Copy record sequence number.
5079 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005080
Hanno Beckere5e7e782019-07-11 12:29:35 +01005081#if defined(MBEDTLS_SSL_PROTO_DTLS)
5082 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5083 {
5084 /* Copy explicit record sequence number from input buffer. */
5085 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5086 rec_hdr_ctr_len );
5087 }
5088 else
5089#endif /* MBEDTLS_SSL_PROTO_DTLS */
5090 {
5091 /* Copy implicit record sequence number from SSL context structure. */
5092 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5093 }
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005094
Hanno Beckere5e7e782019-07-11 12:29:35 +01005095 /*
5096 * Parse record length.
5097 */
5098
Hanno Beckere5e7e782019-07-11 12:29:35 +01005099 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005100 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5101 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005102 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5103
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005105 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005106 rec->type,
5107 major_ver, minor_ver, rec->data_len ) );
5108
5109 rec->buf = buf;
5110 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005111
Hanno Beckerd417cc92019-07-26 08:20:27 +01005112 if( rec->data_len == 0 )
5113 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5114
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005115 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005116 * DTLS-related tests.
5117 * Check epoch before checking length constraint because
5118 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5119 * message gets duplicated before the corresponding Finished message,
5120 * the second ChangeCipherSpec should be discarded because it belongs
5121 * to an old epoch, but not because its length is shorter than
5122 * the minimum record length for packets using the new record transform.
5123 * Note that these two kinds of failures are handled differently,
5124 * as an unexpected record is silently skipped but an invalid
5125 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005126 */
5127#if defined(MBEDTLS_SSL_PROTO_DTLS)
5128 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5129 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005130 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005131
Hanno Becker955a5c92019-07-10 17:12:07 +01005132 /* Check that the datagram is large enough to contain a record
5133 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005134 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005135 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5137 (unsigned) len,
5138 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005139 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5140 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005141
Hanno Becker37cfe732019-07-10 17:20:01 +01005142 /* Records from other, non-matching epochs are silently discarded.
5143 * (The case of same-port Client reconnects must be considered in
5144 * the caller). */
Hanno Becker552f7472019-07-19 10:59:12 +01005145 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005146 {
5147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5148 "expected %d, received %d",
5149 ssl->in_epoch, rec_epoch ) );
Hanno Becker552f7472019-07-19 10:59:12 +01005150
5151 /* Records from the next epoch are considered for buffering
5152 * (concretely: early Finished messages). */
5153 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5154 {
5155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5156 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5157 }
5158
Hanno Becker2fddd372019-07-10 14:37:41 +01005159 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005160 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005161#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005162 /* For records from the correct epoch, check whether their
5163 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005164 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005165 {
5166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5167 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5168 }
5169#endif
5170 }
5171#endif /* MBEDTLS_SSL_PROTO_DTLS */
5172
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005173 return( 0 );
5174}
Paul Bakker5121ce52009-01-03 21:22:43 +00005175
Hanno Becker2fddd372019-07-10 14:37:41 +01005176
5177#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5178static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5179{
5180 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5181
5182 /*
5183 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5184 * access the first byte of record content (handshake type), as we
5185 * have an active transform (possibly iv_len != 0), so use the
5186 * fact that the record header len is 13 instead.
5187 */
5188 if( rec_epoch == 0 &&
5189 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5190 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5191 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5192 ssl->in_left > 13 &&
5193 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5194 {
5195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5196 "from the same port" ) );
5197 return( ssl_handle_possible_reconnect( ssl ) );
5198 }
5199
5200 return( 0 );
5201}
5202#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5203
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005204/*
5205 * If applicable, decrypt (and decompress) record content
5206 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005207static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5208 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005209{
5210 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005213 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5216 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 ret = mbedtls_ssl_hw_record_read( ssl );
5221 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5224 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005225 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005226
5227 if( ret == 0 )
5228 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005229 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005231 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005232 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005233 unsigned char const old_msg_type = rec->type;
5234
Hanno Beckera18d1322018-01-03 14:27:32 +00005235 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005236 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005238 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005239
Hanno Beckera0e20d02019-05-15 14:03:01 +01005240#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005241 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5242 ssl->conf->ignore_unexpected_cid
5243 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5244 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005246 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005247 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005248#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005249
Paul Bakker5121ce52009-01-03 21:22:43 +00005250 return( ret );
5251 }
5252
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005253 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005254 {
5255 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005256 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005257 }
5258
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005259 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005260 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005261
Hanno Beckera0e20d02019-05-15 14:03:01 +01005262#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005263 /* We have already checked the record content type
5264 * in ssl_parse_record_header(), failing or silently
5265 * dropping the record in the case of an unknown type.
5266 *
5267 * Since with the use of CIDs, the record content type
5268 * might change during decryption, re-check the record
5269 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005270 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005271 {
5272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5273 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5274 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005275#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005276
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005277 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005278 {
5279#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5280 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005281 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005282 {
5283 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5285 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5286 }
5287#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5288
5289 ssl->nb_zero++;
5290
5291 /*
5292 * Three or more empty messages may be a DoS attack
5293 * (excessive CPU consumption).
5294 */
5295 if( ssl->nb_zero > 3 )
5296 {
5297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005298 "messages, possible DoS attack" ) );
5299 /* Treat the records as if they were not properly authenticated,
5300 * thereby failing the connection if we see more than allowed
5301 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005302 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5303 }
5304 }
5305 else
5306 ssl->nb_zero = 0;
5307
5308#if defined(MBEDTLS_SSL_PROTO_DTLS)
5309 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5310 {
5311 ; /* in_ctr read from peer, not maintained internally */
5312 }
5313 else
5314#endif
5315 {
5316 unsigned i;
5317 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5318 if( ++ssl->in_ctr[i - 1] != 0 )
5319 break;
5320
5321 /* The loop goes to its end iff the counter is wrapping */
5322 if( i == ssl_ep_len( ssl ) )
5323 {
5324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5325 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5326 }
5327 }
5328
Paul Bakker5121ce52009-01-03 21:22:43 +00005329 }
5330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005332 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005334 {
5335 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005338 return( ret );
5339 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005340 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005344 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005346 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005347 }
5348#endif
5349
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005350 /* Check actual (decrypted) record content length against
5351 * configured maximum. */
5352 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5353 {
5354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5355 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5356 }
5357
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005358 return( 0 );
5359}
5360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005362
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005363/*
5364 * Read a record.
5365 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005366 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5367 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5368 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005369 */
Hanno Becker1097b342018-08-15 14:09:41 +01005370
5371/* Helper functions for mbedtls_ssl_read_record(). */
5372static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005373static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5374static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005375
Hanno Becker327c93b2018-08-15 13:56:18 +01005376int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005377 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005378{
5379 int ret;
5380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005382
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005383 if( ssl->keep_current_message == 0 )
5384 {
5385 do {
Simon Butcher99000142016-10-13 17:21:01 +01005386
Hanno Becker26994592018-08-15 14:14:59 +01005387 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005388 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005389 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005390
Hanno Beckere74d5562018-08-15 14:26:08 +01005391 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005392 {
Hanno Becker40f50842018-08-15 14:48:01 +01005393#if defined(MBEDTLS_SSL_PROTO_DTLS)
5394 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005395
Hanno Becker40f50842018-08-15 14:48:01 +01005396 /* We only check for buffered messages if the
5397 * current datagram is fully consumed. */
5398 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005399 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005400 {
Hanno Becker40f50842018-08-15 14:48:01 +01005401 if( ssl_load_buffered_message( ssl ) == 0 )
5402 have_buffered = 1;
5403 }
5404
5405 if( have_buffered == 0 )
5406#endif /* MBEDTLS_SSL_PROTO_DTLS */
5407 {
5408 ret = ssl_get_next_record( ssl );
5409 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5410 continue;
5411
5412 if( ret != 0 )
5413 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005414 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005415 return( ret );
5416 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005417 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005418 }
5419
5420 ret = mbedtls_ssl_handle_message_type( ssl );
5421
Hanno Becker40f50842018-08-15 14:48:01 +01005422#if defined(MBEDTLS_SSL_PROTO_DTLS)
5423 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5424 {
5425 /* Buffer future message */
5426 ret = ssl_buffer_message( ssl );
5427 if( ret != 0 )
5428 return( ret );
5429
5430 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5431 }
5432#endif /* MBEDTLS_SSL_PROTO_DTLS */
5433
Hanno Becker90333da2017-10-10 11:27:13 +01005434 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5435 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005436
5437 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005438 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005439 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005440 return( ret );
5441 }
5442
Hanno Becker327c93b2018-08-15 13:56:18 +01005443 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005444 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005445 {
5446 mbedtls_ssl_update_handshake_status( ssl );
5447 }
Simon Butcher99000142016-10-13 17:21:01 +01005448 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005449 else
Simon Butcher99000142016-10-13 17:21:01 +01005450 {
Hanno Becker02f59072018-08-15 14:00:24 +01005451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005452 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005453 }
5454
5455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5456
5457 return( 0 );
5458}
5459
Hanno Becker40f50842018-08-15 14:48:01 +01005460#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005461static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005462{
Hanno Becker40f50842018-08-15 14:48:01 +01005463 if( ssl->in_left > ssl->next_record_offset )
5464 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005465
Hanno Becker40f50842018-08-15 14:48:01 +01005466 return( 0 );
5467}
5468
5469static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5470{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005471 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005472 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005473 int ret = 0;
5474
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005475 if( hs == NULL )
5476 return( -1 );
5477
Hanno Beckere00ae372018-08-20 09:39:42 +01005478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5479
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005480 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5481 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5482 {
5483 /* Check if we have seen a ChangeCipherSpec before.
5484 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005485 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005486 {
5487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5488 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005489 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005490 }
5491
Hanno Becker39b8bc92018-08-28 17:17:13 +01005492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005493 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5494 ssl->in_msglen = 1;
5495 ssl->in_msg[0] = 1;
5496
5497 /* As long as they are equal, the exact value doesn't matter. */
5498 ssl->in_left = 0;
5499 ssl->next_record_offset = 0;
5500
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005501 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005502 goto exit;
5503 }
Hanno Becker37f95322018-08-16 13:55:32 +01005504
Hanno Beckerb8f50142018-08-28 10:01:34 +01005505#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005506 /* Debug only */
5507 {
5508 unsigned offset;
5509 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5510 {
5511 hs_buf = &hs->buffering.hs[offset];
5512 if( hs_buf->is_valid == 1 )
5513 {
5514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5515 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005516 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005517 }
5518 }
5519 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005520#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005521
5522 /* Check if we have buffered and/or fully reassembled the
5523 * next handshake message. */
5524 hs_buf = &hs->buffering.hs[0];
5525 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5526 {
5527 /* Synthesize a record containing the buffered HS message. */
5528 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5529 ( hs_buf->data[2] << 8 ) |
5530 hs_buf->data[3];
5531
5532 /* Double-check that we haven't accidentally buffered
5533 * a message that doesn't fit into the input buffer. */
5534 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5535 {
5536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5538 }
5539
5540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5541 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5542 hs_buf->data, msg_len + 12 );
5543
5544 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5545 ssl->in_hslen = msg_len + 12;
5546 ssl->in_msglen = msg_len + 12;
5547 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5548
5549 ret = 0;
5550 goto exit;
5551 }
5552 else
5553 {
5554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5555 hs->in_msg_seq ) );
5556 }
5557
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005558 ret = -1;
5559
5560exit:
5561
5562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5563 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005564}
5565
Hanno Beckera02b0b42018-08-21 17:20:27 +01005566static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5567 size_t desired )
5568{
5569 int offset;
5570 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5572 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005573
Hanno Becker01315ea2018-08-21 17:22:17 +01005574 /* Get rid of future records epoch first, if such exist. */
5575 ssl_free_buffered_record( ssl );
5576
5577 /* Check if we have enough space available now. */
5578 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5579 hs->buffering.total_bytes_buffered ) )
5580 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005582 return( 0 );
5583 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005584
Hanno Becker4f432ad2018-08-28 10:02:32 +01005585 /* We don't have enough space to buffer the next expected handshake
5586 * message. Remove buffers used for future messages to gain space,
5587 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005588 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5589 offset >= 0; offset-- )
5590 {
5591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5592 offset ) );
5593
Hanno Beckerb309b922018-08-23 13:18:05 +01005594 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005595
5596 /* Check if we have enough space available now. */
5597 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5598 hs->buffering.total_bytes_buffered ) )
5599 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005601 return( 0 );
5602 }
5603 }
5604
5605 return( -1 );
5606}
5607
Hanno Becker40f50842018-08-15 14:48:01 +01005608static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5609{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005610 int ret = 0;
5611 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5612
5613 if( hs == NULL )
5614 return( 0 );
5615
5616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5617
5618 switch( ssl->in_msgtype )
5619 {
5620 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005622
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005623 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005624 break;
5625
5626 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005627 {
5628 unsigned recv_msg_seq_offset;
5629 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5630 mbedtls_ssl_hs_buffer *hs_buf;
5631 size_t msg_len = ssl->in_hslen - 12;
5632
5633 /* We should never receive an old handshake
5634 * message - double-check nonetheless. */
5635 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5636 {
5637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5638 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5639 }
5640
5641 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5642 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5643 {
5644 /* Silently ignore -- message too far in the future */
5645 MBEDTLS_SSL_DEBUG_MSG( 2,
5646 ( "Ignore future HS message with sequence number %u, "
5647 "buffering window %u - %u",
5648 recv_msg_seq, ssl->handshake->in_msg_seq,
5649 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5650
5651 goto exit;
5652 }
5653
5654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5655 recv_msg_seq, recv_msg_seq_offset ) );
5656
5657 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5658
5659 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005660 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005661 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005662 size_t reassembly_buf_sz;
5663
Hanno Becker37f95322018-08-16 13:55:32 +01005664 hs_buf->is_fragmented =
5665 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5666
5667 /* We copy the message back into the input buffer
5668 * after reassembly, so check that it's not too large.
5669 * This is an implementation-specific limitation
5670 * and not one from the standard, hence it is not
5671 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005672 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005673 {
5674 /* Ignore message */
5675 goto exit;
5676 }
5677
Hanno Beckere0b150f2018-08-21 15:51:03 +01005678 /* Check if we have enough space to buffer the message. */
5679 if( hs->buffering.total_bytes_buffered >
5680 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5681 {
5682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5684 }
5685
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005686 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5687 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005688
5689 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5690 hs->buffering.total_bytes_buffered ) )
5691 {
5692 if( recv_msg_seq_offset > 0 )
5693 {
5694 /* If we can't buffer a future message because
5695 * of space limitations -- ignore. */
5696 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",
5697 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5698 (unsigned) hs->buffering.total_bytes_buffered ) );
5699 goto exit;
5700 }
Hanno Beckere1801392018-08-21 16:51:05 +01005701 else
5702 {
5703 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",
5704 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5705 (unsigned) hs->buffering.total_bytes_buffered ) );
5706 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005707
Hanno Beckera02b0b42018-08-21 17:20:27 +01005708 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005709 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005710 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",
5711 (unsigned) msg_len,
5712 (unsigned) reassembly_buf_sz,
5713 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005714 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005715 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5716 goto exit;
5717 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005718 }
5719
5720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5721 msg_len ) );
5722
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005723 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5724 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005725 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005726 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005727 goto exit;
5728 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005729 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005730
5731 /* Prepare final header: copy msg_type, length and message_seq,
5732 * then add standardised fragment_offset and fragment_length */
5733 memcpy( hs_buf->data, ssl->in_msg, 6 );
5734 memset( hs_buf->data + 6, 0, 3 );
5735 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5736
5737 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005738
5739 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005740 }
5741 else
5742 {
5743 /* Make sure msg_type and length are consistent */
5744 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5745 {
5746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5747 /* Ignore */
5748 goto exit;
5749 }
5750 }
5751
Hanno Becker4422bbb2018-08-20 09:40:19 +01005752 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005753 {
5754 size_t frag_len, frag_off;
5755 unsigned char * const msg = hs_buf->data + 12;
5756
5757 /*
5758 * Check and copy current fragment
5759 */
5760
5761 /* Validation of header fields already done in
5762 * mbedtls_ssl_prepare_handshake_record(). */
5763 frag_off = ssl_get_hs_frag_off( ssl );
5764 frag_len = ssl_get_hs_frag_len( ssl );
5765
5766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5767 frag_off, frag_len ) );
5768 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5769
5770 if( hs_buf->is_fragmented )
5771 {
5772 unsigned char * const bitmask = msg + msg_len;
5773 ssl_bitmask_set( bitmask, frag_off, frag_len );
5774 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5775 msg_len ) == 0 );
5776 }
5777 else
5778 {
5779 hs_buf->is_complete = 1;
5780 }
5781
5782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5783 hs_buf->is_complete ? "" : "not yet " ) );
5784 }
5785
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005786 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005787 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005788
5789 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005790 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005791 break;
5792 }
5793
5794exit:
5795
5796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5797 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005798}
5799#endif /* MBEDTLS_SSL_PROTO_DTLS */
5800
Hanno Becker1097b342018-08-15 14:09:41 +01005801static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005802{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005803 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005804 * Consume last content-layer message and potentially
5805 * update in_msglen which keeps track of the contents'
5806 * consumption state.
5807 *
5808 * (1) Handshake messages:
5809 * Remove last handshake message, move content
5810 * and adapt in_msglen.
5811 *
5812 * (2) Alert messages:
5813 * Consume whole record content, in_msglen = 0.
5814 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005815 * (3) Change cipher spec:
5816 * Consume whole record content, in_msglen = 0.
5817 *
5818 * (4) Application data:
5819 * Don't do anything - the record layer provides
5820 * the application data as a stream transport
5821 * and consumes through mbedtls_ssl_read only.
5822 *
5823 */
5824
5825 /* Case (1): Handshake messages */
5826 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005827 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005828 /* Hard assertion to be sure that no application data
5829 * is in flight, as corrupting ssl->in_msglen during
5830 * ssl->in_offt != NULL is fatal. */
5831 if( ssl->in_offt != NULL )
5832 {
5833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5835 }
5836
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005837 /*
5838 * Get next Handshake message in the current record
5839 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005840
Hanno Becker4a810fb2017-05-24 16:27:30 +01005841 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005842 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005843 * current handshake content: If DTLS handshake
5844 * fragmentation is used, that's the fragment
5845 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005846 * size here is faulty and should be changed at
5847 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005848 * (2) While it doesn't seem to cause problems, one
5849 * has to be very careful not to assume that in_hslen
5850 * is always <= in_msglen in a sensible communication.
5851 * Again, it's wrong for DTLS handshake fragmentation.
5852 * The following check is therefore mandatory, and
5853 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005854 * Additionally, ssl->in_hslen might be arbitrarily out of
5855 * bounds after handling a DTLS message with an unexpected
5856 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005857 */
5858 if( ssl->in_hslen < ssl->in_msglen )
5859 {
5860 ssl->in_msglen -= ssl->in_hslen;
5861 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5862 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005863
Hanno Becker4a810fb2017-05-24 16:27:30 +01005864 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5865 ssl->in_msg, ssl->in_msglen );
5866 }
5867 else
5868 {
5869 ssl->in_msglen = 0;
5870 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005871
Hanno Becker4a810fb2017-05-24 16:27:30 +01005872 ssl->in_hslen = 0;
5873 }
5874 /* Case (4): Application data */
5875 else if( ssl->in_offt != NULL )
5876 {
5877 return( 0 );
5878 }
5879 /* Everything else (CCS & Alerts) */
5880 else
5881 {
5882 ssl->in_msglen = 0;
5883 }
5884
Hanno Becker1097b342018-08-15 14:09:41 +01005885 return( 0 );
5886}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005887
Hanno Beckere74d5562018-08-15 14:26:08 +01005888static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5889{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005890 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005891 return( 1 );
5892
5893 return( 0 );
5894}
5895
Hanno Becker5f066e72018-08-16 14:56:31 +01005896#if defined(MBEDTLS_SSL_PROTO_DTLS)
5897
5898static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5899{
5900 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5901 if( hs == NULL )
5902 return;
5903
Hanno Becker01315ea2018-08-21 17:22:17 +01005904 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005905 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005906 hs->buffering.total_bytes_buffered -=
5907 hs->buffering.future_record.len;
5908
5909 mbedtls_free( hs->buffering.future_record.data );
5910 hs->buffering.future_record.data = NULL;
5911 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005912}
5913
5914static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5915{
5916 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5917 unsigned char * rec;
5918 size_t rec_len;
5919 unsigned rec_epoch;
5920
5921 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5922 return( 0 );
5923
5924 if( hs == NULL )
5925 return( 0 );
5926
Hanno Becker5f066e72018-08-16 14:56:31 +01005927 rec = hs->buffering.future_record.data;
5928 rec_len = hs->buffering.future_record.len;
5929 rec_epoch = hs->buffering.future_record.epoch;
5930
5931 if( rec == NULL )
5932 return( 0 );
5933
Hanno Becker4cb782d2018-08-20 11:19:05 +01005934 /* Only consider loading future records if the
5935 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005936 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005937 return( 0 );
5938
Hanno Becker5f066e72018-08-16 14:56:31 +01005939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5940
5941 if( rec_epoch != ssl->in_epoch )
5942 {
5943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5944 goto exit;
5945 }
5946
5947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5948
5949 /* Double-check that the record is not too large */
5950 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5951 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5952 {
5953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5955 }
5956
5957 memcpy( ssl->in_hdr, rec, rec_len );
5958 ssl->in_left = rec_len;
5959 ssl->next_record_offset = 0;
5960
5961 ssl_free_buffered_record( ssl );
5962
5963exit:
5964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5965 return( 0 );
5966}
5967
Hanno Becker519f15d2019-07-11 12:43:20 +01005968static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
5969 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01005970{
5971 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01005972
5973 /* Don't buffer future records outside handshakes. */
5974 if( hs == NULL )
5975 return( 0 );
5976
5977 /* Only buffer handshake records (we are only interested
5978 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01005979 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01005980 return( 0 );
5981
5982 /* Don't buffer more than one future epoch record. */
5983 if( hs->buffering.future_record.data != NULL )
5984 return( 0 );
5985
Hanno Becker01315ea2018-08-21 17:22:17 +01005986 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01005987 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01005988 hs->buffering.total_bytes_buffered ) )
5989 {
5990 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 +01005991 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01005992 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005993 return( 0 );
5994 }
5995
Hanno Becker5f066e72018-08-16 14:56:31 +01005996 /* Buffer record */
5997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5998 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01005999 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006000
6001 /* ssl_parse_record_header() only considers records
6002 * of the next epoch as candidates for buffering. */
6003 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006004 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006005
6006 hs->buffering.future_record.data =
6007 mbedtls_calloc( 1, hs->buffering.future_record.len );
6008 if( hs->buffering.future_record.data == NULL )
6009 {
6010 /* If we run out of RAM trying to buffer a
6011 * record from the next epoch, just ignore. */
6012 return( 0 );
6013 }
6014
Hanno Becker519f15d2019-07-11 12:43:20 +01006015 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006016
Hanno Becker519f15d2019-07-11 12:43:20 +01006017 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006018 return( 0 );
6019}
6020
6021#endif /* MBEDTLS_SSL_PROTO_DTLS */
6022
Hanno Beckere74d5562018-08-15 14:26:08 +01006023static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006024{
6025 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006026 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006027
Hanno Becker5f066e72018-08-16 14:56:31 +01006028#if defined(MBEDTLS_SSL_PROTO_DTLS)
6029 /* We might have buffered a future record; if so,
6030 * and if the epoch matches now, load it.
6031 * On success, this call will set ssl->in_left to
6032 * the length of the buffered record, so that
6033 * the calls to ssl_fetch_input() below will
6034 * essentially be no-ops. */
6035 ret = ssl_load_buffered_record( ssl );
6036 if( ret != 0 )
6037 return( ret );
6038#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006039
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006040 /* Ensure that we have enough space available for the default form
6041 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6042 * with no space for CIDs counted in). */
6043 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6044 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006047 return( ret );
6048 }
6049
Hanno Beckere5e7e782019-07-11 12:29:35 +01006050 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6051 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006054 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006055 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006056 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6057 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006058 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006059 if( ret != 0 )
6060 return( ret );
6061
6062 /* Fall through to handling of unexpected records */
6063 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6064 }
6065
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006066 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6067 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006068#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006069 /* Reset in pointers to default state for TLS/DTLS records,
6070 * assuming no CID and no offset between record content and
6071 * record plaintext. */
6072 ssl_update_in_pointers( ssl );
6073
Hanno Becker7ae20e02019-07-12 08:33:49 +01006074 /* Setup internal message pointers from record structure. */
6075 ssl->in_msgtype = rec.type;
6076#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6077 ssl->in_len = ssl->in_cid + rec.cid_len;
6078#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6079 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6080 ssl->in_msglen = rec.data_len;
6081
Hanno Becker2fddd372019-07-10 14:37:41 +01006082 ret = ssl_check_client_reconnect( ssl );
6083 if( ret != 0 )
6084 return( ret );
6085#endif
6086
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006087 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006088 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006089
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6091 "(header)" ) );
6092 }
6093 else
6094 {
6095 /* Skip invalid record and the rest of the datagram */
6096 ssl->next_record_offset = 0;
6097 ssl->in_left = 0;
6098
6099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6100 "(header)" ) );
6101 }
6102
6103 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006104 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006105 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006106 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006107#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006108 {
6109 return( ret );
6110 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006111 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006114 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006115 {
Hanno Beckera8814792019-07-10 15:01:45 +01006116 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006117 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006118 if( ssl->next_record_offset < ssl->in_left )
6119 {
6120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6121 }
6122 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006123 else
6124#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006125 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006126 /*
6127 * Fetch record contents from underlying transport.
6128 */
Hanno Beckera3175662019-07-11 12:50:29 +01006129 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006130 if( ret != 0 )
6131 {
6132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6133 return( ret );
6134 }
6135
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006136 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006137 }
6138
6139 /*
6140 * Decrypt record contents.
6141 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006142
Hanno Beckerfdf66042019-07-11 13:07:45 +01006143 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006146 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006147 {
6148 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006149 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006150 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006151 /* Except when waiting for Finished as a bad mac here
6152 * probably means something went wrong in the handshake
6153 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6154 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6155 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6156 {
6157#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6158 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6159 {
6160 mbedtls_ssl_send_alert_message( ssl,
6161 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6162 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6163 }
6164#endif
6165 return( ret );
6166 }
6167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006169 if( ssl->conf->badmac_limit != 0 &&
6170 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6173 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006174 }
6175#endif
6176
Hanno Becker4a810fb2017-05-24 16:27:30 +01006177 /* As above, invalid records cause
6178 * dismissal of the whole datagram. */
6179
6180 ssl->next_record_offset = 0;
6181 ssl->in_left = 0;
6182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006184 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006185 }
6186
6187 return( ret );
6188 }
6189 else
6190#endif
6191 {
6192 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6194 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196 mbedtls_ssl_send_alert_message( ssl,
6197 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6198 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006199 }
6200#endif
6201 return( ret );
6202 }
6203 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006204
Hanno Becker44d89b22019-07-12 09:40:44 +01006205
6206 /* Reset in pointers to default state for TLS/DTLS records,
6207 * assuming no CID and no offset between record content and
6208 * record plaintext. */
6209 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006210#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6211 ssl->in_len = ssl->in_cid + rec.cid_len;
6212#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6213 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006214
Hanno Becker8685c822019-07-12 09:37:30 +01006215 /* The record content type may change during decryption,
6216 * so re-read it. */
6217 ssl->in_msgtype = rec.type;
6218 /* Also update the input buffer, because unfortunately
6219 * the server-side ssl_parse_client_hello() reparses the
6220 * record header when receiving a ClientHello initiating
6221 * a renegotiation. */
6222 ssl->in_hdr[0] = rec.type;
6223 ssl->in_msg = rec.buf + rec.data_offset;
6224 ssl->in_msglen = rec.data_len;
6225 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6226 ssl->in_len[1] = (unsigned char)( rec.data_len );
6227
Simon Butcher99000142016-10-13 17:21:01 +01006228 return( 0 );
6229}
6230
6231int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6232{
6233 int ret;
6234
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006235 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006236 * Handle particular types of records
6237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006239 {
Simon Butcher99000142016-10-13 17:21:01 +01006240 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6241 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006242 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006243 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006244 }
6245
Hanno Beckere678eaa2018-08-21 14:57:46 +01006246 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006247 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006248 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006249 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6251 ssl->in_msglen ) );
6252 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006253 }
6254
Hanno Beckere678eaa2018-08-21 14:57:46 +01006255 if( ssl->in_msg[0] != 1 )
6256 {
6257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6258 ssl->in_msg[0] ) );
6259 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6260 }
6261
6262#if defined(MBEDTLS_SSL_PROTO_DTLS)
6263 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6264 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6265 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6266 {
6267 if( ssl->handshake == NULL )
6268 {
6269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6270 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6271 }
6272
6273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6274 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6275 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006276#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006277 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006280 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006281 if( ssl->in_msglen != 2 )
6282 {
6283 /* Note: Standard allows for more than one 2 byte alert
6284 to be packed in a single message, but Mbed TLS doesn't
6285 currently support this. */
6286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6287 ssl->in_msglen ) );
6288 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6289 }
6290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006292 ssl->in_msg[0], ssl->in_msg[1] ) );
6293
6294 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006295 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006300 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006302 }
6303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6305 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6308 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006309 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006310
6311#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6312 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6313 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6314 {
Hanno Becker90333da2017-10-10 11:27:13 +01006315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006316 /* Will be handled when trying to parse ServerHello */
6317 return( 0 );
6318 }
6319#endif
6320
6321#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6322 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6323 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6324 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6325 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6326 {
6327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6328 /* Will be handled in mbedtls_ssl_parse_certificate() */
6329 return( 0 );
6330 }
6331#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6332
6333 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006334 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006335 }
6336
Hanno Beckerc76c6192017-06-06 10:03:17 +01006337#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006338 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006339 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006340 /* Drop unexpected ApplicationData records,
6341 * except at the beginning of renegotiations */
6342 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6343 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6344#if defined(MBEDTLS_SSL_RENEGOTIATION)
6345 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6346 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006347#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006348 )
6349 {
6350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6351 return( MBEDTLS_ERR_SSL_NON_FATAL );
6352 }
6353
6354 if( ssl->handshake != NULL &&
6355 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6356 {
6357 ssl_handshake_wrapup_free_hs_transform( ssl );
6358 }
6359 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006360#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006361
Paul Bakker5121ce52009-01-03 21:22:43 +00006362 return( 0 );
6363}
6364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006366{
6367 int ret;
6368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6370 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6371 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006372 {
6373 return( ret );
6374 }
6375
6376 return( 0 );
6377}
6378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006380 unsigned char level,
6381 unsigned char message )
6382{
6383 int ret;
6384
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006385 if( ssl == NULL || ssl->conf == NULL )
6386 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006389 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006392 ssl->out_msglen = 2;
6393 ssl->out_msg[0] = level;
6394 ssl->out_msg[1] = message;
6395
Hanno Becker67bc7c32018-08-06 11:33:50 +01006396 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006398 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006399 return( ret );
6400 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006402
6403 return( 0 );
6404}
6405
Hanno Beckerb9d44792019-02-08 07:19:04 +00006406#if defined(MBEDTLS_X509_CRT_PARSE_C)
6407static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6408{
6409#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6410 if( session->peer_cert != NULL )
6411 {
6412 mbedtls_x509_crt_free( session->peer_cert );
6413 mbedtls_free( session->peer_cert );
6414 session->peer_cert = NULL;
6415 }
6416#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6417 if( session->peer_cert_digest != NULL )
6418 {
6419 /* Zeroization is not necessary. */
6420 mbedtls_free( session->peer_cert_digest );
6421 session->peer_cert_digest = NULL;
6422 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6423 session->peer_cert_digest_len = 0;
6424 }
6425#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6426}
6427#endif /* MBEDTLS_X509_CRT_PARSE_C */
6428
Paul Bakker5121ce52009-01-03 21:22:43 +00006429/*
6430 * Handshake functions
6431 */
Hanno Becker21489932019-02-05 13:20:55 +00006432#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006433/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006435{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006436 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6437 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006440
Hanno Becker7177a882019-02-05 13:36:46 +00006441 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006444 ssl->state++;
6445 return( 0 );
6446 }
6447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6449 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006450}
6451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006453{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006454 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6455 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006458
Hanno Becker7177a882019-02-05 13:36:46 +00006459 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006462 ssl->state++;
6463 return( 0 );
6464 }
6465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006468}
Gilles Peskinef9828522017-05-03 12:28:43 +02006469
Hanno Becker21489932019-02-05 13:20:55 +00006470#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006471/* Some certificate support -> implement write and parse */
6472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006474{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006476 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006478 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6479 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006482
Hanno Becker7177a882019-02-05 13:36:46 +00006483 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006486 ssl->state++;
6487 return( 0 );
6488 }
6489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006491 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006492 {
6493 if( ssl->client_auth == 0 )
6494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006496 ssl->state++;
6497 return( 0 );
6498 }
6499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006501 /*
6502 * If using SSLv3 and got no cert, send an Alert message
6503 * (otherwise an empty Certificate message will be sent).
6504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6506 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006507 {
6508 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6510 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6511 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006514 goto write_msg;
6515 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006517 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518#endif /* MBEDTLS_SSL_CLI_C */
6519#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006520 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6525 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006526 }
6527 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006528#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006531
6532 /*
6533 * 0 . 0 handshake type
6534 * 1 . 3 handshake length
6535 * 4 . 6 length of all certs
6536 * 7 . 9 length of cert. 1
6537 * 10 . n-1 peer certificate
6538 * n . n+2 length of cert. 2
6539 * n+3 . ... upper level cert, etc.
6540 */
6541 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006543
Paul Bakker29087132010-03-21 21:03:34 +00006544 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006545 {
6546 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006547 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006550 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006552 }
6553
6554 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6555 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6556 ssl->out_msg[i + 2] = (unsigned char)( n );
6557
6558 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6559 i += n; crt = crt->next;
6560 }
6561
6562 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6563 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6564 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6565
6566 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6568 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006569
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006570#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006571write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006572#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006573
6574 ssl->state++;
6575
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006576 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006579 return( ret );
6580 }
6581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006583
Paul Bakkered27a042013-04-18 22:46:23 +02006584 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006585}
6586
Hanno Becker84879e32019-01-31 07:44:03 +00006587#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006588
6589#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006590static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6591 unsigned char *crt_buf,
6592 size_t crt_buf_len )
6593{
6594 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6595
6596 if( peer_crt == NULL )
6597 return( -1 );
6598
6599 if( peer_crt->raw.len != crt_buf_len )
6600 return( -1 );
6601
Hanno Becker46f34d02019-02-08 14:00:04 +00006602 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006603}
Hanno Becker177475a2019-02-05 17:02:46 +00006604#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6605static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6606 unsigned char *crt_buf,
6607 size_t crt_buf_len )
6608{
6609 int ret;
6610 unsigned char const * const peer_cert_digest =
6611 ssl->session->peer_cert_digest;
6612 mbedtls_md_type_t const peer_cert_digest_type =
6613 ssl->session->peer_cert_digest_type;
6614 mbedtls_md_info_t const * const digest_info =
6615 mbedtls_md_info_from_type( peer_cert_digest_type );
6616 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6617 size_t digest_len;
6618
6619 if( peer_cert_digest == NULL || digest_info == NULL )
6620 return( -1 );
6621
6622 digest_len = mbedtls_md_get_size( digest_info );
6623 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6624 return( -1 );
6625
6626 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6627 if( ret != 0 )
6628 return( -1 );
6629
6630 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6631}
6632#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006633#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006634
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006635/*
6636 * Once the certificate message is read, parse it into a cert chain and
6637 * perform basic checks, but leave actual verification to the caller
6638 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006639static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6640 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006641{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006642 int ret;
6643#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6644 int crt_cnt=0;
6645#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006646 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006647 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006652 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6653 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 }
6656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6658 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006661 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6662 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006664 }
6665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006667
Paul Bakker5121ce52009-01-03 21:22:43 +00006668 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006670 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006671 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006672
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006673 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006677 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6678 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006680 }
6681
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006682 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6683 i += 3;
6684
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006685 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006686 while( i < ssl->in_hslen )
6687 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006688 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006689 if ( i + 3 > ssl->in_hslen ) {
6690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006691 mbedtls_ssl_send_alert_message( ssl,
6692 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6693 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006694 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6695 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006696 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6697 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006698 if( ssl->in_msg[i] != 0 )
6699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006701 mbedtls_ssl_send_alert_message( ssl,
6702 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6703 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006705 }
6706
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006707 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006708 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6709 | (unsigned int) ssl->in_msg[i + 2];
6710 i += 3;
6711
6712 if( n < 128 || i + n > ssl->in_hslen )
6713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006715 mbedtls_ssl_send_alert_message( ssl,
6716 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6717 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006719 }
6720
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006721 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006722#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6723 if( crt_cnt++ == 0 &&
6724 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6725 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006726 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006727 /* During client-side renegotiation, check that the server's
6728 * end-CRTs hasn't changed compared to the initial handshake,
6729 * mitigating the triple handshake attack. On success, reuse
6730 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6732 if( ssl_check_peer_crt_unchanged( ssl,
6733 &ssl->in_msg[i],
6734 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006735 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6737 mbedtls_ssl_send_alert_message( ssl,
6738 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6739 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6740 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006741 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006742
6743 /* Now we can safely free the original chain. */
6744 ssl_clear_peer_cert( ssl->session );
6745 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006746#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6747
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006748 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006749#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006750 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006751#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006752 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006753 * it in-place from the input buffer instead of making a copy. */
6754 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6755#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006756 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006757 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006758 case 0: /*ok*/
6759 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6760 /* Ignore certificate with an unknown algorithm: maybe a
6761 prior certificate was already trusted. */
6762 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006763
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006764 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6765 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6766 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006767
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006768 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6769 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6770 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006771
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006772 default:
6773 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6774 crt_parse_der_failed:
6775 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6776 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6777 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006778 }
6779
6780 i += n;
6781 }
6782
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006783 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006784 return( 0 );
6785}
6786
Hanno Becker4a55f632019-02-05 12:49:06 +00006787#if defined(MBEDTLS_SSL_SRV_C)
6788static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6789{
6790 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6791 return( -1 );
6792
6793#if defined(MBEDTLS_SSL_PROTO_SSL3)
6794 /*
6795 * Check if the client sent an empty certificate
6796 */
6797 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6798 {
6799 if( ssl->in_msglen == 2 &&
6800 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6801 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6802 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6803 {
6804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6805 return( 0 );
6806 }
6807
6808 return( -1 );
6809 }
6810#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6811
6812#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6813 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6814 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6815 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6816 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6817 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6818 {
6819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6820 return( 0 );
6821 }
6822
6823 return( -1 );
6824#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6825 MBEDTLS_SSL_PROTO_TLS1_2 */
6826}
6827#endif /* MBEDTLS_SSL_SRV_C */
6828
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006829/* Check if a certificate message is expected.
6830 * Return either
6831 * - SSL_CERTIFICATE_EXPECTED, or
6832 * - SSL_CERTIFICATE_SKIP
6833 * indicating whether a Certificate message is expected or not.
6834 */
6835#define SSL_CERTIFICATE_EXPECTED 0
6836#define SSL_CERTIFICATE_SKIP 1
6837static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6838 int authmode )
6839{
6840 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006841 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006842
6843 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6844 return( SSL_CERTIFICATE_SKIP );
6845
6846#if defined(MBEDTLS_SSL_SRV_C)
6847 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6848 {
6849 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6850 return( SSL_CERTIFICATE_SKIP );
6851
6852 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6853 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006854 ssl->session_negotiate->verify_result =
6855 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6856 return( SSL_CERTIFICATE_SKIP );
6857 }
6858 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006859#else
6860 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006861#endif /* MBEDTLS_SSL_SRV_C */
6862
6863 return( SSL_CERTIFICATE_EXPECTED );
6864}
6865
Hanno Becker68636192019-02-05 14:36:34 +00006866static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6867 int authmode,
6868 mbedtls_x509_crt *chain,
6869 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006870{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006871 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006872 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006873 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006874 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006875
Hanno Becker8927c832019-04-03 12:52:50 +01006876 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6877 void *p_vrfy;
6878
Hanno Becker68636192019-02-05 14:36:34 +00006879 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6880 return( 0 );
6881
Hanno Becker8927c832019-04-03 12:52:50 +01006882 if( ssl->f_vrfy != NULL )
6883 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006884 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006885 f_vrfy = ssl->f_vrfy;
6886 p_vrfy = ssl->p_vrfy;
6887 }
6888 else
6889 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006890 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006891 f_vrfy = ssl->conf->f_vrfy;
6892 p_vrfy = ssl->conf->p_vrfy;
6893 }
6894
Hanno Becker68636192019-02-05 14:36:34 +00006895 /*
6896 * Main check: verify certificate
6897 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006898#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6899 if( ssl->conf->f_ca_cb != NULL )
6900 {
6901 ((void) rs_ctx);
6902 have_ca_chain = 1;
6903
6904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006905 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006906 chain,
6907 ssl->conf->f_ca_cb,
6908 ssl->conf->p_ca_cb,
6909 ssl->conf->cert_profile,
6910 ssl->hostname,
6911 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006912 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006913 }
6914 else
6915#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6916 {
6917 mbedtls_x509_crt *ca_chain;
6918 mbedtls_x509_crl *ca_crl;
6919
6920#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6921 if( ssl->handshake->sni_ca_chain != NULL )
6922 {
6923 ca_chain = ssl->handshake->sni_ca_chain;
6924 ca_crl = ssl->handshake->sni_ca_crl;
6925 }
6926 else
6927#endif
6928 {
6929 ca_chain = ssl->conf->ca_chain;
6930 ca_crl = ssl->conf->ca_crl;
6931 }
6932
6933 if( ca_chain != NULL )
6934 have_ca_chain = 1;
6935
6936 ret = mbedtls_x509_crt_verify_restartable(
6937 chain,
6938 ca_chain, ca_crl,
6939 ssl->conf->cert_profile,
6940 ssl->hostname,
6941 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006942 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006943 }
Hanno Becker68636192019-02-05 14:36:34 +00006944
6945 if( ret != 0 )
6946 {
6947 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6948 }
6949
6950#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6951 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6952 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6953#endif
6954
6955 /*
6956 * Secondary checks: always done, but change 'ret' only if it was 0
6957 */
6958
6959#if defined(MBEDTLS_ECP_C)
6960 {
6961 const mbedtls_pk_context *pk = &chain->pk;
6962
6963 /* If certificate uses an EC key, make sure the curve is OK */
6964 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6965 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6966 {
6967 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6968
6969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6970 if( ret == 0 )
6971 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6972 }
6973 }
6974#endif /* MBEDTLS_ECP_C */
6975
6976 if( mbedtls_ssl_check_cert_usage( chain,
6977 ciphersuite_info,
6978 ! ssl->conf->endpoint,
6979 &ssl->session_negotiate->verify_result ) != 0 )
6980 {
6981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6982 if( ret == 0 )
6983 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6984 }
6985
6986 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6987 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6988 * with details encoded in the verification flags. All other kinds
6989 * of error codes, including those from the user provided f_vrfy
6990 * functions, are treated as fatal and lead to a failure of
6991 * ssl_parse_certificate even if verification was optional. */
6992 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6993 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6994 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6995 {
6996 ret = 0;
6997 }
6998
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006999 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007000 {
7001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7002 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7003 }
7004
7005 if( ret != 0 )
7006 {
7007 uint8_t alert;
7008
7009 /* The certificate may have been rejected for several reasons.
7010 Pick one and send the corresponding alert. Which alert to send
7011 may be a subject of debate in some cases. */
7012 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7013 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7014 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7015 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7016 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7017 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7018 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7019 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7020 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7021 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7022 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7023 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7024 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7025 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7026 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7027 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7028 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7029 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7030 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7031 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7032 else
7033 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7034 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7035 alert );
7036 }
7037
7038#if defined(MBEDTLS_DEBUG_C)
7039 if( ssl->session_negotiate->verify_result != 0 )
7040 {
7041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7042 ssl->session_negotiate->verify_result ) );
7043 }
7044 else
7045 {
7046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7047 }
7048#endif /* MBEDTLS_DEBUG_C */
7049
7050 return( ret );
7051}
7052
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007053#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7054static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7055 unsigned char *start, size_t len )
7056{
7057 int ret;
7058 /* Remember digest of the peer's end-CRT. */
7059 ssl->session_negotiate->peer_cert_digest =
7060 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7061 if( ssl->session_negotiate->peer_cert_digest == NULL )
7062 {
7063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7064 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7065 mbedtls_ssl_send_alert_message( ssl,
7066 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7067 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7068
7069 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7070 }
7071
7072 ret = mbedtls_md( mbedtls_md_info_from_type(
7073 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7074 start, len,
7075 ssl->session_negotiate->peer_cert_digest );
7076
7077 ssl->session_negotiate->peer_cert_digest_type =
7078 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7079 ssl->session_negotiate->peer_cert_digest_len =
7080 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7081
7082 return( ret );
7083}
7084
7085static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7086 unsigned char *start, size_t len )
7087{
7088 unsigned char *end = start + len;
7089 int ret;
7090
7091 /* Make a copy of the peer's raw public key. */
7092 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7093 ret = mbedtls_pk_parse_subpubkey( &start, end,
7094 &ssl->handshake->peer_pubkey );
7095 if( ret != 0 )
7096 {
7097 /* We should have parsed the public key before. */
7098 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7099 }
7100
7101 return( 0 );
7102}
7103#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7104
Hanno Becker68636192019-02-05 14:36:34 +00007105int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7106{
7107 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007108 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007109#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7110 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7111 ? ssl->handshake->sni_authmode
7112 : ssl->conf->authmode;
7113#else
7114 const int authmode = ssl->conf->authmode;
7115#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007116 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007117 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007118
7119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7120
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007121 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7122 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007123 {
7124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007125 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007126 }
7127
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007128#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7129 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007130 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007131 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007132 chain = ssl->handshake->ecrs_peer_cert;
7133 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007134 goto crt_verify;
7135 }
7136#endif
7137
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007138 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007139 {
7140 /* mbedtls_ssl_read_record may have sent an alert already. We
7141 let it decide whether to alert. */
7142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007143 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007144 }
7145
Hanno Becker4a55f632019-02-05 12:49:06 +00007146#if defined(MBEDTLS_SSL_SRV_C)
7147 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7148 {
7149 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007150
7151 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007152 ret = 0;
7153 else
7154 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007155
Hanno Becker6bdfab22019-02-05 13:11:17 +00007156 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007157 }
7158#endif /* MBEDTLS_SSL_SRV_C */
7159
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007160 /* Clear existing peer CRT structure in case we tried to
7161 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007162 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007163
7164 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7165 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007166 {
7167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7168 sizeof( mbedtls_x509_crt ) ) );
7169 mbedtls_ssl_send_alert_message( ssl,
7170 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7171 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007172
Hanno Becker3dad3112019-02-05 17:19:52 +00007173 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7174 goto exit;
7175 }
7176 mbedtls_x509_crt_init( chain );
7177
7178 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007179 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007180 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007181
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007182#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7183 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007184 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007185
7186crt_verify:
7187 if( ssl->handshake->ecrs_enabled)
7188 rs_ctx = &ssl->handshake->ecrs_ctx;
7189#endif
7190
Hanno Becker68636192019-02-05 14:36:34 +00007191 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007192 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007193 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007194 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007195
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007196#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007197 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007198 unsigned char *crt_start, *pk_start;
7199 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007200
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007201 /* We parse the CRT chain without copying, so
7202 * these pointers point into the input buffer,
7203 * and are hence still valid after freeing the
7204 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007205
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007206 crt_start = chain->raw.p;
7207 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007208
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007209 pk_start = chain->pk_raw.p;
7210 pk_len = chain->pk_raw.len;
7211
7212 /* Free the CRT structures before computing
7213 * digest and copying the peer's public key. */
7214 mbedtls_x509_crt_free( chain );
7215 mbedtls_free( chain );
7216 chain = NULL;
7217
7218 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007219 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007220 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007221
7222 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7223 if( ret != 0 )
7224 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007225 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007226#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7227 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007228 ssl->session_negotiate->peer_cert = chain;
7229 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007230#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007233
Hanno Becker6bdfab22019-02-05 13:11:17 +00007234exit:
7235
Hanno Becker3dad3112019-02-05 17:19:52 +00007236 if( ret == 0 )
7237 ssl->state++;
7238
7239#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7240 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7241 {
7242 ssl->handshake->ecrs_peer_cert = chain;
7243 chain = NULL;
7244 }
7245#endif
7246
7247 if( chain != NULL )
7248 {
7249 mbedtls_x509_crt_free( chain );
7250 mbedtls_free( chain );
7251 }
7252
Paul Bakker5121ce52009-01-03 21:22:43 +00007253 return( ret );
7254}
Hanno Becker21489932019-02-05 13:20:55 +00007255#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007258{
7259 int ret;
7260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007264 ssl->out_msglen = 1;
7265 ssl->out_msg[0] = 1;
7266
Paul Bakker5121ce52009-01-03 21:22:43 +00007267 ssl->state++;
7268
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007269 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007270 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007272 return( ret );
7273 }
7274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007276
7277 return( 0 );
7278}
7279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007281{
7282 int ret;
7283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007285
Hanno Becker327c93b2018-08-15 13:56:18 +01007286 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007289 return( ret );
7290 }
7291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007295 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7296 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007298 }
7299
Hanno Beckere678eaa2018-08-21 14:57:46 +01007300 /* CCS records are only accepted if they have length 1 and content '1',
7301 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007302
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007303 /*
7304 * Switch to our negotiated transform and session parameters for inbound
7305 * data.
7306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007308 ssl->transform_in = ssl->transform_negotiate;
7309 ssl->session_in = ssl->session_negotiate;
7310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007312 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007315 ssl_dtls_replay_reset( ssl );
7316#endif
7317
7318 /* Increment epoch */
7319 if( ++ssl->in_epoch == 0 )
7320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007322 /* This is highly unlikely to happen for legitimate reasons, so
7323 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007325 }
7326 }
7327 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007329 memset( ssl->in_ctr, 0, 8 );
7330
Hanno Becker79594fd2019-05-08 09:38:41 +01007331 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7334 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007339 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7340 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007342 }
7343 }
7344#endif
7345
Paul Bakker5121ce52009-01-03 21:22:43 +00007346 ssl->state++;
7347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007349
7350 return( 0 );
7351}
7352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7354 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007355{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007356 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7359 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7360 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007361 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007362 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7365#if defined(MBEDTLS_SHA512_C)
7366 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007367 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7368 else
7369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#if defined(MBEDTLS_SHA256_C)
7371 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007372 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007373 else
7374#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007378 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007379 }
Paul Bakker380da532012-04-18 16:10:25 +00007380}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007383{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7385 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007386 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7387 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007388#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7390#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007391#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007392 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007393 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7394#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007395 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007396#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007397#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007399#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007400 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007401 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007402#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007403 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007404#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007405#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007407}
7408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007410 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007411{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007412#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7413 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007414 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7415 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007416#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7418#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007419#if defined(MBEDTLS_USE_PSA_CRYPTO)
7420 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7421#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007422 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007423#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007424#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007426#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007427 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007428#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007429 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007430#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007433}
7434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7436 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7437static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007438 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007439{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007440 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7441 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007442}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007443#endif
Paul Bakker380da532012-04-18 16:10:25 +00007444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7446#if defined(MBEDTLS_SHA256_C)
7447static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007448 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007449{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007450#if defined(MBEDTLS_USE_PSA_CRYPTO)
7451 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7452#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007453 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007454#endif
Paul Bakker380da532012-04-18 16:10:25 +00007455}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007456#endif
Paul Bakker380da532012-04-18 16:10:25 +00007457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458#if defined(MBEDTLS_SHA512_C)
7459static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007460 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007461{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007462#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007463 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007464#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007465 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007466#endif
Paul Bakker380da532012-04-18 16:10:25 +00007467}
Paul Bakker769075d2012-11-24 11:26:46 +01007468#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007472static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007474{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007475 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007476 mbedtls_md5_context md5;
7477 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007478
Paul Bakker5121ce52009-01-03 21:22:43 +00007479 unsigned char padbuf[48];
7480 unsigned char md5sum[16];
7481 unsigned char sha1sum[20];
7482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007484 if( !session )
7485 session = ssl->session;
7486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007488
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007489 mbedtls_md5_init( &md5 );
7490 mbedtls_sha1_init( &sha1 );
7491
7492 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7493 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007494
7495 /*
7496 * SSLv3:
7497 * hash =
7498 * MD5( master + pad2 +
7499 * MD5( handshake + sender + master + pad1 ) )
7500 * + SHA1( master + pad2 +
7501 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007502 */
7503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007505 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7506 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007507#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007510 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7511 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007512#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007515 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007516
Paul Bakker1ef83d62012-04-11 12:09:53 +00007517 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007518
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007519 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7520 mbedtls_md5_update_ret( &md5, session->master, 48 );
7521 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7522 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007523
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007524 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7525 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7526 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7527 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007528
Paul Bakker1ef83d62012-04-11 12:09:53 +00007529 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007530
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007531 mbedtls_md5_starts_ret( &md5 );
7532 mbedtls_md5_update_ret( &md5, session->master, 48 );
7533 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7534 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7535 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007536
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007537 mbedtls_sha1_starts_ret( &sha1 );
7538 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7539 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7540 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7541 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007544
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007545 mbedtls_md5_free( &md5 );
7546 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007547
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007548 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7549 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7550 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007553}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007557static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007559{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007560 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007561 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007562 mbedtls_md5_context md5;
7563 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007564 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007567 if( !session )
7568 session = ssl->session;
7569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007571
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007572 mbedtls_md5_init( &md5 );
7573 mbedtls_sha1_init( &sha1 );
7574
7575 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7576 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007577
Paul Bakker1ef83d62012-04-11 12:09:53 +00007578 /*
7579 * TLSv1:
7580 * hash = PRF( master, finished_label,
7581 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7582 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007585 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7586 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007587#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007590 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7591 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007592#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007595 ? "client finished"
7596 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007597
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007598 mbedtls_md5_finish_ret( &md5, padbuf );
7599 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007600
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007601 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007602 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007605
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007606 mbedtls_md5_free( &md5 );
7607 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007608
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007609 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007612}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7616#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007617static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007619{
7620 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007621 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007622 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007623#if defined(MBEDTLS_USE_PSA_CRYPTO)
7624 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007625 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007626 psa_status_t status;
7627#else
7628 mbedtls_sha256_context sha256;
7629#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007632 if( !session )
7633 session = ssl->session;
7634
Andrzej Kurekeb342242019-01-29 09:14:33 -05007635 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7636 ? "client finished"
7637 : "server finished";
7638
7639#if defined(MBEDTLS_USE_PSA_CRYPTO)
7640 sha256_psa = psa_hash_operation_init();
7641
7642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7643
7644 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7645 if( status != PSA_SUCCESS )
7646 {
7647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7648 return;
7649 }
7650
7651 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7652 if( status != PSA_SUCCESS )
7653 {
7654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7655 return;
7656 }
7657 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7658#else
7659
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007660 mbedtls_sha256_init( &sha256 );
7661
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007663
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007664 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007665
7666 /*
7667 * TLSv1.2:
7668 * hash = PRF( master, finished_label,
7669 * Hash( handshake ) )[0.11]
7670 */
7671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672#if !defined(MBEDTLS_SHA256_ALT)
7673 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007674 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007675#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007676
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007677 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007678 mbedtls_sha256_free( &sha256 );
7679#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007680
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007681 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007682 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007685
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007686 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007693static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007695{
7696 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007697 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007698 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007699#if defined(MBEDTLS_USE_PSA_CRYPTO)
7700 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007701 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007702 psa_status_t status;
7703#else
7704 mbedtls_sha512_context sha512;
7705#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007708 if( !session )
7709 session = ssl->session;
7710
Andrzej Kurekeb342242019-01-29 09:14:33 -05007711 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7712 ? "client finished"
7713 : "server finished";
7714
7715#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007716 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007717
7718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7719
Andrzej Kurek972fba52019-01-30 03:29:12 -05007720 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007721 if( status != PSA_SUCCESS )
7722 {
7723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7724 return;
7725 }
7726
Andrzej Kurek972fba52019-01-30 03:29:12 -05007727 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007728 if( status != PSA_SUCCESS )
7729 {
7730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7731 return;
7732 }
7733 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7734#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007735 mbedtls_sha512_init( &sha512 );
7736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007738
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007739 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007740
7741 /*
7742 * TLSv1.2:
7743 * hash = PRF( master, finished_label,
7744 * Hash( handshake ) )[0.11]
7745 */
7746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007748 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7749 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007750#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007751
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007752 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007753 mbedtls_sha512_free( &sha512 );
7754#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007755
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007756 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007757 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007760
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007761 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007764}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765#endif /* MBEDTLS_SHA512_C */
7766#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007769{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007771
7772 /*
7773 * Free our handshake params
7774 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007775 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007777 ssl->handshake = NULL;
7778
7779 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007780 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007781 */
7782 if( ssl->transform )
7783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 mbedtls_ssl_transform_free( ssl->transform );
7785 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007786 }
7787 ssl->transform = ssl->transform_negotiate;
7788 ssl->transform_negotiate = NULL;
7789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007791}
7792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007794{
7795 int resume = ssl->handshake->resume;
7796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799#if defined(MBEDTLS_SSL_RENEGOTIATION)
7800 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007803 ssl->renego_records_seen = 0;
7804 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007805#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007806
7807 /*
7808 * Free the previous session and switch in the current one
7809 */
Paul Bakker0a597072012-09-25 21:55:46 +00007810 if( ssl->session )
7811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007813 /* RFC 7366 3.1: keep the EtM state */
7814 ssl->session_negotiate->encrypt_then_mac =
7815 ssl->session->encrypt_then_mac;
7816#endif
7817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818 mbedtls_ssl_session_free( ssl->session );
7819 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007820 }
7821 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007822 ssl->session_negotiate = NULL;
7823
Paul Bakker0a597072012-09-25 21:55:46 +00007824 /*
7825 * Add cache entry
7826 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007827 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007828 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007829 resume == 0 )
7830 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007831 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007833 }
Paul Bakker0a597072012-09-25 21:55:46 +00007834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007836 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007837 ssl->handshake->flight != NULL )
7838 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007839 /* Cancel handshake timer */
7840 ssl_set_timer( ssl, 0 );
7841
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007842 /* Keep last flight around in case we need to resend it:
7843 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007845 }
7846 else
7847#endif
7848 ssl_handshake_wrapup_free_hs_transform( ssl );
7849
Paul Bakker48916f92012-09-16 19:57:18 +00007850 ssl->state++;
7851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007853}
7854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007856{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007857 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007860
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007861 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007862
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007863 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007864
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007865 /*
7866 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7867 * may define some other value. Currently (early 2016), no defined
7868 * ciphersuite does this (and this is unlikely to change as activity has
7869 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007874 ssl->verify_data_len = hash_len;
7875 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007876#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007877
Paul Bakker5121ce52009-01-03 21:22:43 +00007878 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7880 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007881
7882 /*
7883 * In case of session resuming, invert the client and server
7884 * ChangeCipherSpec messages order.
7885 */
Paul Bakker0a597072012-09-25 21:55:46 +00007886 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007889 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007891#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007893 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007895#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007896 }
7897 else
7898 ssl->state++;
7899
Paul Bakker48916f92012-09-16 19:57:18 +00007900 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007901 * Switch to our negotiated transform and session parameters for outbound
7902 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007907 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007908 {
7909 unsigned char i;
7910
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007911 /* Remember current epoch settings for resending */
7912 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007913 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007914
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007915 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007916 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007917
7918 /* Increment epoch */
7919 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007920 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007921 break;
7922
7923 /* The loop goes to its end iff the counter is wrapping */
7924 if( i == 0 )
7925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7927 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007928 }
7929 }
7930 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007932 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007933
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007934 ssl->transform_out = ssl->transform_negotiate;
7935 ssl->session_out = ssl->session_negotiate;
7936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7938 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7943 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007944 }
7945 }
7946#endif
7947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007949 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007951#endif
7952
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007953 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007954 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007956 return( ret );
7957 }
7958
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007959#if defined(MBEDTLS_SSL_PROTO_DTLS)
7960 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7961 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7962 {
7963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7964 return( ret );
7965 }
7966#endif
7967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007969
7970 return( 0 );
7971}
7972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007974#define SSL_MAX_HASH_LEN 36
7975#else
7976#define SSL_MAX_HASH_LEN 12
7977#endif
7978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007980{
Paul Bakker23986e52011-04-24 08:57:21 +00007981 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007982 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007983 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007986
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007987 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007988
Hanno Becker327c93b2018-08-15 13:56:18 +01007989 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007992 return( ret );
7993 }
7994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007998 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7999 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008001 }
8002
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008003 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004#if defined(MBEDTLS_SSL_PROTO_SSL3)
8005 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008006 hash_len = 36;
8007 else
8008#endif
8009 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8012 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008015 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8016 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008018 }
8019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008021 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008024 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8025 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008027 }
8028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008030 ssl->verify_data_len = hash_len;
8031 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008032#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008033
Paul Bakker0a597072012-09-25 21:55:46 +00008034 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008037 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008040#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008041 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008042 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008044 }
8045 else
8046 ssl->state++;
8047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008049 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008051#endif
8052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008054
8055 return( 0 );
8056}
8057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8063 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8064 mbedtls_md5_init( &handshake->fin_md5 );
8065 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008066 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8067 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008068#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008069#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8070#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008071#if defined(MBEDTLS_USE_PSA_CRYPTO)
8072 handshake->fin_sha256_psa = psa_hash_operation_init();
8073 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8074#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008076 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008077#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008078#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008080#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008081 handshake->fin_sha384_psa = psa_hash_operation_init();
8082 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008083#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008084 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008085 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008086#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008089
8090 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008091
8092#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8093 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8094 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8095#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097#if defined(MBEDTLS_DHM_C)
8098 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008099#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100#if defined(MBEDTLS_ECDH_C)
8101 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008102#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008103#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008104 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008105#if defined(MBEDTLS_SSL_CLI_C)
8106 handshake->ecjpake_cache = NULL;
8107 handshake->ecjpake_cache_len = 0;
8108#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008109#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008110
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008111#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008112 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008113#endif
8114
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008115#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8116 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8117#endif
Hanno Becker75173122019-02-06 16:18:31 +00008118
8119#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8120 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8121 mbedtls_pk_init( &handshake->peer_pubkey );
8122#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008123}
8124
Hanno Beckera18d1322018-01-03 14:27:32 +00008125void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8130 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008131
Hanno Beckerd56ed242018-01-03 15:32:51 +00008132#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133 mbedtls_md_init( &transform->md_ctx_enc );
8134 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008135#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008136}
8137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008141}
8142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008144{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008145 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008146 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008148 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008150 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008151 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008152
8153 /*
8154 * Either the pointers are now NULL or cleared properly and can be freed.
8155 * Now allocate missing structures.
8156 */
8157 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008158 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008159 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008160 }
Paul Bakker48916f92012-09-16 19:57:18 +00008161
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008162 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008163 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008164 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008165 }
Paul Bakker48916f92012-09-16 19:57:18 +00008166
Paul Bakker82788fb2014-10-20 13:59:19 +02008167 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008168 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008169 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008170 }
Paul Bakker48916f92012-09-16 19:57:18 +00008171
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008172 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008173 if( ssl->handshake == NULL ||
8174 ssl->transform_negotiate == NULL ||
8175 ssl->session_negotiate == NULL )
8176 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179 mbedtls_free( ssl->handshake );
8180 mbedtls_free( ssl->transform_negotiate );
8181 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008182
8183 ssl->handshake = NULL;
8184 ssl->transform_negotiate = NULL;
8185 ssl->session_negotiate = NULL;
8186
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008187 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008188 }
8189
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008190 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008191 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008192 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008193 ssl_handshake_params_init( ssl->handshake );
8194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008196 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8197 {
8198 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008199
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008200 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8201 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8202 else
8203 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008204
8205 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008206 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008207#endif
8208
Paul Bakker48916f92012-09-16 19:57:18 +00008209 return( 0 );
8210}
8211
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008212#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008213/* Dummy cookie callbacks for defaults */
8214static int ssl_cookie_write_dummy( void *ctx,
8215 unsigned char **p, unsigned char *end,
8216 const unsigned char *cli_id, size_t cli_id_len )
8217{
8218 ((void) ctx);
8219 ((void) p);
8220 ((void) end);
8221 ((void) cli_id);
8222 ((void) cli_id_len);
8223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008224 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008225}
8226
8227static int ssl_cookie_check_dummy( void *ctx,
8228 const unsigned char *cookie, size_t cookie_len,
8229 const unsigned char *cli_id, size_t cli_id_len )
8230{
8231 ((void) ctx);
8232 ((void) cookie);
8233 ((void) cookie_len);
8234 ((void) cli_id);
8235 ((void) cli_id_len);
8236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008238}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008239#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008240
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008241/* Once ssl->out_hdr as the address of the beginning of the
8242 * next outgoing record is set, deduce the other pointers.
8243 *
8244 * Note: For TLS, we save the implicit record sequence number
8245 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8246 * and the caller has to make sure there's space for this.
8247 */
8248
8249static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8250 mbedtls_ssl_transform *transform )
8251{
8252#if defined(MBEDTLS_SSL_PROTO_DTLS)
8253 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8254 {
8255 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008256#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008257 ssl->out_cid = ssl->out_ctr + 8;
8258 ssl->out_len = ssl->out_cid;
8259 if( transform != NULL )
8260 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008261#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008262 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008263#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008264 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008265 }
8266 else
8267#endif
8268 {
8269 ssl->out_ctr = ssl->out_hdr - 8;
8270 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008271#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008272 ssl->out_cid = ssl->out_len;
8273#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008274 ssl->out_iv = ssl->out_hdr + 5;
8275 }
8276
8277 /* Adjust out_msg to make space for explicit IV, if used. */
8278 if( transform != NULL &&
8279 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8280 {
8281 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8282 }
8283 else
8284 ssl->out_msg = ssl->out_iv;
8285}
8286
8287/* Once ssl->in_hdr as the address of the beginning of the
8288 * next incoming record is set, deduce the other pointers.
8289 *
8290 * Note: For TLS, we save the implicit record sequence number
8291 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8292 * and the caller has to make sure there's space for this.
8293 */
8294
Hanno Becker79594fd2019-05-08 09:38:41 +01008295static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008296{
Hanno Becker79594fd2019-05-08 09:38:41 +01008297 /* This function sets the pointers to match the case
8298 * of unprotected TLS/DTLS records, with both ssl->in_iv
8299 * and ssl->in_msg pointing to the beginning of the record
8300 * content.
8301 *
8302 * When decrypting a protected record, ssl->in_msg
8303 * will be shifted to point to the beginning of the
8304 * record plaintext.
8305 */
8306
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008307#if defined(MBEDTLS_SSL_PROTO_DTLS)
8308 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8309 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008310 /* This sets the header pointers to match records
8311 * without CID. When we receive a record containing
8312 * a CID, the fields are shifted accordingly in
8313 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008314 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008315#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008316 ssl->in_cid = ssl->in_ctr + 8;
8317 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008318#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008319 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008320#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008321 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008322 }
8323 else
8324#endif
8325 {
8326 ssl->in_ctr = ssl->in_hdr - 8;
8327 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008328#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008329 ssl->in_cid = ssl->in_len;
8330#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008331 ssl->in_iv = ssl->in_hdr + 5;
8332 }
8333
Hanno Becker79594fd2019-05-08 09:38:41 +01008334 /* This will be adjusted at record decryption time. */
8335 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008336}
8337
Paul Bakker5121ce52009-01-03 21:22:43 +00008338/*
8339 * Initialize an SSL context
8340 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008341void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8342{
8343 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8344}
8345
8346/*
8347 * Setup an SSL context
8348 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008349
8350static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8351{
8352 /* Set the incoming and outgoing record pointers. */
8353#if defined(MBEDTLS_SSL_PROTO_DTLS)
8354 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8355 {
8356 ssl->out_hdr = ssl->out_buf;
8357 ssl->in_hdr = ssl->in_buf;
8358 }
8359 else
8360#endif /* MBEDTLS_SSL_PROTO_DTLS */
8361 {
8362 ssl->out_hdr = ssl->out_buf + 8;
8363 ssl->in_hdr = ssl->in_buf + 8;
8364 }
8365
8366 /* Derive other internal pointers. */
8367 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008368 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008369}
8370
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008371int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008372 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008373{
Paul Bakker48916f92012-09-16 19:57:18 +00008374 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008375
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008376 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008377
8378 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008379 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008380 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008381
8382 /* Set to NULL in case of an error condition */
8383 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008384
Angus Grattond8213d02016-05-25 20:56:48 +10008385 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8386 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008387 {
Angus Grattond8213d02016-05-25 20:56:48 +10008388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008389 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008390 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008391 }
8392
8393 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8394 if( ssl->out_buf == NULL )
8395 {
8396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008397 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008398 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008399 }
8400
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008401 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008402
Paul Bakker48916f92012-09-16 19:57:18 +00008403 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008404 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008405
8406 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008407
8408error:
8409 mbedtls_free( ssl->in_buf );
8410 mbedtls_free( ssl->out_buf );
8411
8412 ssl->conf = NULL;
8413
8414 ssl->in_buf = NULL;
8415 ssl->out_buf = NULL;
8416
8417 ssl->in_hdr = NULL;
8418 ssl->in_ctr = NULL;
8419 ssl->in_len = NULL;
8420 ssl->in_iv = NULL;
8421 ssl->in_msg = NULL;
8422
8423 ssl->out_hdr = NULL;
8424 ssl->out_ctr = NULL;
8425 ssl->out_len = NULL;
8426 ssl->out_iv = NULL;
8427 ssl->out_msg = NULL;
8428
k-stachowiak9f7798e2018-07-31 16:52:32 +02008429 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008430}
8431
8432/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008433 * Reset an initialized and used SSL context for re-use while retaining
8434 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008435 *
8436 * If partial is non-zero, keep data in the input buffer and client ID.
8437 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008438 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008439static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008440{
Paul Bakker48916f92012-09-16 19:57:18 +00008441 int ret;
8442
Hanno Becker7e772132018-08-10 12:38:21 +01008443#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8444 !defined(MBEDTLS_SSL_SRV_C)
8445 ((void) partial);
8446#endif
8447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008449
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008450 /* Cancel any possibly running timer */
8451 ssl_set_timer( ssl, 0 );
8452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453#if defined(MBEDTLS_SSL_RENEGOTIATION)
8454 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008455 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008456
8457 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8459 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008460#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008462
Paul Bakker7eb013f2011-10-06 12:37:39 +00008463 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008464 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008465
8466 ssl->in_msgtype = 0;
8467 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008469 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008470 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008471#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008472#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008473 ssl_dtls_replay_reset( ssl );
8474#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008475
8476 ssl->in_hslen = 0;
8477 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008478
8479 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008480
8481 ssl->out_msgtype = 0;
8482 ssl->out_msglen = 0;
8483 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008484#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8485 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008486 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008487#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008488
Hanno Becker19859472018-08-06 09:40:20 +01008489 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8490
Paul Bakker48916f92012-09-16 19:57:18 +00008491 ssl->transform_in = NULL;
8492 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008493
Hanno Becker78640902018-08-13 16:35:15 +01008494 ssl->session_in = NULL;
8495 ssl->session_out = NULL;
8496
Angus Grattond8213d02016-05-25 20:56:48 +10008497 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008498
8499#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008500 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008501#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8502 {
8503 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008504 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008505 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8508 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8511 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8514 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008515 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008516 }
8517#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008518
Paul Bakker48916f92012-09-16 19:57:18 +00008519 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008521 mbedtls_ssl_transform_free( ssl->transform );
8522 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008523 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008524 }
Paul Bakker48916f92012-09-16 19:57:18 +00008525
Paul Bakkerc0463502013-02-14 11:19:38 +01008526 if( ssl->session )
8527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008528 mbedtls_ssl_session_free( ssl->session );
8529 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008530 ssl->session = NULL;
8531 }
8532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008534 ssl->alpn_chosen = NULL;
8535#endif
8536
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008537#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008538#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008539 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008540#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008541 {
8542 mbedtls_free( ssl->cli_id );
8543 ssl->cli_id = NULL;
8544 ssl->cli_id_len = 0;
8545 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008546#endif
8547
Paul Bakker48916f92012-09-16 19:57:18 +00008548 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8549 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008550
8551 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008552}
8553
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008554/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008555 * Reset an initialized and used SSL context for re-use while retaining
8556 * all application-set variables, function pointers and data.
8557 */
8558int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8559{
8560 return( ssl_session_reset_int( ssl, 0 ) );
8561}
8562
8563/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008564 * SSL set accessors
8565 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008566void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008567{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008568 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008569}
8570
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008571void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008572{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008573 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008574}
8575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008576#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008577void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008578{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008579 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008580}
8581#endif
8582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008583#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008584void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008585{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008586 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008587}
8588#endif
8589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008591
Hanno Becker1841b0a2018-08-24 11:13:57 +01008592void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8593 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008594{
8595 ssl->disable_datagram_packing = !allow_packing;
8596}
8597
8598void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8599 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008600{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008601 conf->hs_timeout_min = min;
8602 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008603}
8604#endif
8605
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008606void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008607{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008608 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008609}
8610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008612void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008613 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008614 void *p_vrfy )
8615{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008616 conf->f_vrfy = f_vrfy;
8617 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008618}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008620
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008621void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008622 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008623 void *p_rng )
8624{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008625 conf->f_rng = f_rng;
8626 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008627}
8628
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008629void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008630 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008631 void *p_dbg )
8632{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008633 conf->f_dbg = f_dbg;
8634 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008635}
8636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008638 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008639 mbedtls_ssl_send_t *f_send,
8640 mbedtls_ssl_recv_t *f_recv,
8641 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008642{
8643 ssl->p_bio = p_bio;
8644 ssl->f_send = f_send;
8645 ssl->f_recv = f_recv;
8646 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008647}
8648
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008649#if defined(MBEDTLS_SSL_PROTO_DTLS)
8650void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8651{
8652 ssl->mtu = mtu;
8653}
8654#endif
8655
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008656void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008657{
8658 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008659}
8660
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008661void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8662 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008663 mbedtls_ssl_set_timer_t *f_set_timer,
8664 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008665{
8666 ssl->p_timer = p_timer;
8667 ssl->f_set_timer = f_set_timer;
8668 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008669
8670 /* Make sure we start with no timer running */
8671 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008672}
8673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008675void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008676 void *p_cache,
8677 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8678 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008679{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008680 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008681 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008682 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686#if defined(MBEDTLS_SSL_CLI_C)
8687int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008688{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008689 int ret;
8690
8691 if( ssl == NULL ||
8692 session == NULL ||
8693 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008694 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008697 }
8698
Hanno Becker52055ae2019-02-06 14:30:46 +00008699 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8700 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008701 return( ret );
8702
Paul Bakker0a597072012-09-25 21:55:46 +00008703 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008704
8705 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008706}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008708
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008709void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008710 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008711{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008712 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8713 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8714 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8715 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008716}
8717
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008718void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008719 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008720 int major, int minor )
8721{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008722 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008723 return;
8724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008725 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008726 return;
8727
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008728 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008729}
8730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008732void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008733 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008734{
8735 conf->cert_profile = profile;
8736}
8737
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008738/* Append a new keycert entry to a (possibly empty) list */
8739static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8740 mbedtls_x509_crt *cert,
8741 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008742{
niisato8ee24222018-06-25 19:05:48 +09008743 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008744
niisato8ee24222018-06-25 19:05:48 +09008745 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8746 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008747 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008748
niisato8ee24222018-06-25 19:05:48 +09008749 new_cert->cert = cert;
8750 new_cert->key = key;
8751 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008752
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008753 /* Update head is the list was null, else add to the end */
8754 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008755 {
niisato8ee24222018-06-25 19:05:48 +09008756 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008757 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008758 else
8759 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008760 mbedtls_ssl_key_cert *cur = *head;
8761 while( cur->next != NULL )
8762 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008763 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008764 }
8765
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008766 return( 0 );
8767}
8768
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008769int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008770 mbedtls_x509_crt *own_cert,
8771 mbedtls_pk_context *pk_key )
8772{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008773 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008774}
8775
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008776void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008777 mbedtls_x509_crt *ca_chain,
8778 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008779{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008780 conf->ca_chain = ca_chain;
8781 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008782
8783#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8784 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8785 * cannot be used together. */
8786 conf->f_ca_cb = NULL;
8787 conf->p_ca_cb = NULL;
8788#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008789}
Hanno Becker5adaad92019-03-27 16:54:37 +00008790
8791#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8792void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008793 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008794 void *p_ca_cb )
8795{
8796 conf->f_ca_cb = f_ca_cb;
8797 conf->p_ca_cb = p_ca_cb;
8798
8799 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8800 * cannot be used together. */
8801 conf->ca_chain = NULL;
8802 conf->ca_crl = NULL;
8803}
8804#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008806
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008807#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8808int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8809 mbedtls_x509_crt *own_cert,
8810 mbedtls_pk_context *pk_key )
8811{
8812 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8813 own_cert, pk_key ) );
8814}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008815
8816void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8817 mbedtls_x509_crt *ca_chain,
8818 mbedtls_x509_crl *ca_crl )
8819{
8820 ssl->handshake->sni_ca_chain = ca_chain;
8821 ssl->handshake->sni_ca_crl = ca_crl;
8822}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008823
8824void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8825 int authmode )
8826{
8827 ssl->handshake->sni_authmode = authmode;
8828}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008829#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8830
Hanno Becker8927c832019-04-03 12:52:50 +01008831#if defined(MBEDTLS_X509_CRT_PARSE_C)
8832void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8833 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8834 void *p_vrfy )
8835{
8836 ssl->f_vrfy = f_vrfy;
8837 ssl->p_vrfy = p_vrfy;
8838}
8839#endif
8840
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008841#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008842/*
8843 * Set EC J-PAKE password for current handshake
8844 */
8845int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8846 const unsigned char *pw,
8847 size_t pw_len )
8848{
8849 mbedtls_ecjpake_role role;
8850
Janos Follath8eb64132016-06-03 15:40:57 +01008851 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8853
8854 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8855 role = MBEDTLS_ECJPAKE_SERVER;
8856 else
8857 role = MBEDTLS_ECJPAKE_CLIENT;
8858
8859 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8860 role,
8861 MBEDTLS_MD_SHA256,
8862 MBEDTLS_ECP_DP_SECP256R1,
8863 pw, pw_len ) );
8864}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008865#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008868
8869static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8870{
8871 /* Remove reference to existing PSK, if any. */
8872#if defined(MBEDTLS_USE_PSA_CRYPTO)
8873 if( conf->psk_opaque != 0 )
8874 {
8875 /* The maintenance of the PSK key slot is the
8876 * user's responsibility. */
8877 conf->psk_opaque = 0;
8878 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008879 /* This and the following branch should never
8880 * be taken simultaenously as we maintain the
8881 * invariant that raw and opaque PSKs are never
8882 * configured simultaneously. As a safeguard,
8883 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008884#endif /* MBEDTLS_USE_PSA_CRYPTO */
8885 if( conf->psk != NULL )
8886 {
8887 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8888
8889 mbedtls_free( conf->psk );
8890 conf->psk = NULL;
8891 conf->psk_len = 0;
8892 }
8893
8894 /* Remove reference to PSK identity, if any. */
8895 if( conf->psk_identity != NULL )
8896 {
8897 mbedtls_free( conf->psk_identity );
8898 conf->psk_identity = NULL;
8899 conf->psk_identity_len = 0;
8900 }
8901}
8902
Hanno Becker7390c712018-11-15 13:33:04 +00008903/* This function assumes that PSK identity in the SSL config is unset.
8904 * It checks that the provided identity is well-formed and attempts
8905 * to make a copy of it in the SSL config.
8906 * On failure, the PSK identity in the config remains unset. */
8907static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8908 unsigned char const *psk_identity,
8909 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008910{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008911 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008912 if( psk_identity == NULL ||
8913 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008914 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008915 {
8916 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8917 }
8918
Hanno Becker7390c712018-11-15 13:33:04 +00008919 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8920 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008921 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008922
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008923 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008924 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008925
8926 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008927}
8928
Hanno Becker7390c712018-11-15 13:33:04 +00008929int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8930 const unsigned char *psk, size_t psk_len,
8931 const unsigned char *psk_identity, size_t psk_identity_len )
8932{
8933 int ret;
8934 /* Remove opaque/raw PSK + PSK Identity */
8935 ssl_conf_remove_psk( conf );
8936
8937 /* Check and set raw PSK */
8938 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8939 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8940 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8941 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8942 conf->psk_len = psk_len;
8943 memcpy( conf->psk, psk, conf->psk_len );
8944
8945 /* Check and set PSK Identity */
8946 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8947 if( ret != 0 )
8948 ssl_conf_remove_psk( conf );
8949
8950 return( ret );
8951}
8952
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008953static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8954{
8955#if defined(MBEDTLS_USE_PSA_CRYPTO)
8956 if( ssl->handshake->psk_opaque != 0 )
8957 {
8958 ssl->handshake->psk_opaque = 0;
8959 }
8960 else
8961#endif /* MBEDTLS_USE_PSA_CRYPTO */
8962 if( ssl->handshake->psk != NULL )
8963 {
8964 mbedtls_platform_zeroize( ssl->handshake->psk,
8965 ssl->handshake->psk_len );
8966 mbedtls_free( ssl->handshake->psk );
8967 ssl->handshake->psk_len = 0;
8968 }
8969}
8970
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008971int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8972 const unsigned char *psk, size_t psk_len )
8973{
8974 if( psk == NULL || ssl->handshake == NULL )
8975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8976
8977 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8978 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8979
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008980 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008981
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008982 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008983 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008984
8985 ssl->handshake->psk_len = psk_len;
8986 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8987
8988 return( 0 );
8989}
8990
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008991#if defined(MBEDTLS_USE_PSA_CRYPTO)
8992int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008993 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008994 const unsigned char *psk_identity,
8995 size_t psk_identity_len )
8996{
Hanno Becker7390c712018-11-15 13:33:04 +00008997 int ret;
8998 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008999 ssl_conf_remove_psk( conf );
9000
Hanno Becker7390c712018-11-15 13:33:04 +00009001 /* Check and set opaque PSK */
9002 if( psk_slot == 0 )
9003 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009004 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009005
9006 /* Check and set PSK Identity */
9007 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9008 psk_identity_len );
9009 if( ret != 0 )
9010 ssl_conf_remove_psk( conf );
9011
9012 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009013}
9014
9015int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009016 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009017{
9018 if( psk_slot == 0 || ssl->handshake == NULL )
9019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9020
9021 ssl_remove_psk( ssl );
9022 ssl->handshake->psk_opaque = psk_slot;
9023 return( 0 );
9024}
9025#endif /* MBEDTLS_USE_PSA_CRYPTO */
9026
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009027void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009029 size_t),
9030 void *p_psk )
9031{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009032 conf->f_psk = f_psk;
9033 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009035#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009036
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009037#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009038
9039#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009040int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009041{
9042 int ret;
9043
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009044 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9045 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9046 {
9047 mbedtls_mpi_free( &conf->dhm_P );
9048 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009049 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009050 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009051
9052 return( 0 );
9053}
Hanno Becker470a8c42017-10-04 15:28:46 +01009054#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009055
Hanno Beckera90658f2017-10-04 15:29:08 +01009056int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9057 const unsigned char *dhm_P, size_t P_len,
9058 const unsigned char *dhm_G, size_t G_len )
9059{
9060 int ret;
9061
9062 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9063 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9064 {
9065 mbedtls_mpi_free( &conf->dhm_P );
9066 mbedtls_mpi_free( &conf->dhm_G );
9067 return( ret );
9068 }
9069
9070 return( 0 );
9071}
Paul Bakker5121ce52009-01-03 21:22:43 +00009072
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009073int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009074{
9075 int ret;
9076
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009077 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9078 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9079 {
9080 mbedtls_mpi_free( &conf->dhm_P );
9081 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009082 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009083 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009084
9085 return( 0 );
9086}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009087#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009088
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009089#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9090/*
9091 * Set the minimum length for Diffie-Hellman parameters
9092 */
9093void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9094 unsigned int bitlen )
9095{
9096 conf->dhm_min_bitlen = bitlen;
9097}
9098#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9099
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009100#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009101/*
9102 * Set allowed/preferred hashes for handshake signatures
9103 */
9104void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9105 const int *hashes )
9106{
9107 conf->sig_hashes = hashes;
9108}
Hanno Becker947194e2017-04-07 13:25:49 +01009109#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009110
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009111#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009112/*
9113 * Set the allowed elliptic curves
9114 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009115void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009116 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009117{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009118 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009119}
Hanno Becker947194e2017-04-07 13:25:49 +01009120#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009121
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009122#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009123int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009124{
Hanno Becker947194e2017-04-07 13:25:49 +01009125 /* Initialize to suppress unnecessary compiler warning */
9126 size_t hostname_len = 0;
9127
9128 /* Check if new hostname is valid before
9129 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009130 if( hostname != NULL )
9131 {
9132 hostname_len = strlen( hostname );
9133
9134 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9136 }
9137
9138 /* Now it's clear that we will overwrite the old hostname,
9139 * so we can free it safely */
9140
9141 if( ssl->hostname != NULL )
9142 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009143 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009144 mbedtls_free( ssl->hostname );
9145 }
9146
9147 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009148
Paul Bakker5121ce52009-01-03 21:22:43 +00009149 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009150 {
9151 ssl->hostname = NULL;
9152 }
9153 else
9154 {
9155 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009156 if( ssl->hostname == NULL )
9157 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009158
Hanno Becker947194e2017-04-07 13:25:49 +01009159 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009160
Hanno Becker947194e2017-04-07 13:25:49 +01009161 ssl->hostname[hostname_len] = '\0';
9162 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009163
9164 return( 0 );
9165}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009166#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009167
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009168#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009169void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009171 const unsigned char *, size_t),
9172 void *p_sni )
9173{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009174 conf->f_sni = f_sni;
9175 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009177#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009180int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009181{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009182 size_t cur_len, tot_len;
9183 const char **p;
9184
9185 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009186 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9187 * MUST NOT be truncated."
9188 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009189 */
9190 tot_len = 0;
9191 for( p = protos; *p != NULL; p++ )
9192 {
9193 cur_len = strlen( *p );
9194 tot_len += cur_len;
9195
9196 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009198 }
9199
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009200 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009201
9202 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009203}
9204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009205const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009206{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009207 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009210
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009211void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009212{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009213 conf->max_major_ver = major;
9214 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009215}
9216
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009217void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009218{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009219 conf->min_major_ver = major;
9220 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009221}
9222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009224void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009225{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009226 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009227}
9228#endif
9229
Janos Follath088ce432017-04-10 12:42:31 +01009230#if defined(MBEDTLS_SSL_SRV_C)
9231void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9232 char cert_req_ca_list )
9233{
9234 conf->cert_req_ca_list = cert_req_ca_list;
9235}
9236#endif
9237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009238#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009239void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009240{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009241 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009242}
9243#endif
9244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009246void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009247{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009248 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009249}
9250#endif
9251
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009252#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009253void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009254{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009255 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009256}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009257#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009259#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009260int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009262 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009263 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009266 }
9267
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009268 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009269
9270 return( 0 );
9271}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009272#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009275void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009276{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009277 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009278}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009282void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009283{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009284 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009285}
9286#endif
9287
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009288void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009289{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009290 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009291}
9292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009294void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009295{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009296 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009297}
9298
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009299void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009300{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009301 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009302}
9303
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009304void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009305 const unsigned char period[8] )
9306{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009307 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009308}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009312#if defined(MBEDTLS_SSL_CLI_C)
9313void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009314{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009315 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009316}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009317#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009318
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009319#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009320void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9321 mbedtls_ssl_ticket_write_t *f_ticket_write,
9322 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9323 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009324{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009325 conf->f_ticket_write = f_ticket_write;
9326 conf->f_ticket_parse = f_ticket_parse;
9327 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009328}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009329#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009331
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009332#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9333void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9334 mbedtls_ssl_export_keys_t *f_export_keys,
9335 void *p_export_keys )
9336{
9337 conf->f_export_keys = f_export_keys;
9338 conf->p_export_keys = p_export_keys;
9339}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009340
9341void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9342 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9343 void *p_export_keys )
9344{
9345 conf->f_export_keys_ext = f_export_keys_ext;
9346 conf->p_export_keys = p_export_keys;
9347}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009348#endif
9349
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009350#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009351void mbedtls_ssl_conf_async_private_cb(
9352 mbedtls_ssl_config *conf,
9353 mbedtls_ssl_async_sign_t *f_async_sign,
9354 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9355 mbedtls_ssl_async_resume_t *f_async_resume,
9356 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009357 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009358{
9359 conf->f_async_sign_start = f_async_sign;
9360 conf->f_async_decrypt_start = f_async_decrypt;
9361 conf->f_async_resume = f_async_resume;
9362 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009363 conf->p_async_config_data = async_config_data;
9364}
9365
Gilles Peskine8f97af72018-04-26 11:46:10 +02009366void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9367{
9368 return( conf->p_async_config_data );
9369}
9370
Gilles Peskine1febfef2018-04-30 11:54:39 +02009371void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009372{
9373 if( ssl->handshake == NULL )
9374 return( NULL );
9375 else
9376 return( ssl->handshake->user_async_ctx );
9377}
9378
Gilles Peskine1febfef2018-04-30 11:54:39 +02009379void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009380 void *ctx )
9381{
9382 if( ssl->handshake != NULL )
9383 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009384}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009385#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009386
Paul Bakker5121ce52009-01-03 21:22:43 +00009387/*
9388 * SSL get accessors
9389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009391{
9392 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9393}
9394
Hanno Becker8b170a02017-10-10 11:51:19 +01009395int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9396{
9397 /*
9398 * Case A: We're currently holding back
9399 * a message for further processing.
9400 */
9401
9402 if( ssl->keep_current_message == 1 )
9403 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009405 return( 1 );
9406 }
9407
9408 /*
9409 * Case B: Further records are pending in the current datagram.
9410 */
9411
9412#if defined(MBEDTLS_SSL_PROTO_DTLS)
9413 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9414 ssl->in_left > ssl->next_record_offset )
9415 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009416 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009417 return( 1 );
9418 }
9419#endif /* MBEDTLS_SSL_PROTO_DTLS */
9420
9421 /*
9422 * Case C: A handshake message is being processed.
9423 */
9424
Hanno Becker8b170a02017-10-10 11:51:19 +01009425 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9426 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009427 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009428 return( 1 );
9429 }
9430
9431 /*
9432 * Case D: An application data message is being processed
9433 */
9434 if( ssl->in_offt != NULL )
9435 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009436 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009437 return( 1 );
9438 }
9439
9440 /*
9441 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009442 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009443 * we implement support for multiple alerts in single records.
9444 */
9445
9446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9447 return( 0 );
9448}
9449
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009450uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009451{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009452 if( ssl->session != NULL )
9453 return( ssl->session->verify_result );
9454
9455 if( ssl->session_negotiate != NULL )
9456 return( ssl->session_negotiate->verify_result );
9457
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009458 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009459}
9460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009461const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009462{
Paul Bakker926c8e42013-03-06 10:23:34 +01009463 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009464 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009467}
9468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009472 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009473 {
9474 switch( ssl->minor_ver )
9475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009476 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009477 return( "DTLSv1.0" );
9478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009480 return( "DTLSv1.2" );
9481
9482 default:
9483 return( "unknown (DTLS)" );
9484 }
9485 }
9486#endif
9487
Paul Bakker43ca69c2011-01-15 17:35:19 +00009488 switch( ssl->minor_ver )
9489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009491 return( "SSLv3.0" );
9492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009494 return( "TLSv1.0" );
9495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009497 return( "TLSv1.1" );
9498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009500 return( "TLSv1.2" );
9501
Paul Bakker43ca69c2011-01-15 17:35:19 +00009502 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009503 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009504 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009505}
9506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009508{
Hanno Becker3136ede2018-08-17 15:28:19 +01009509 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009511 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009512
Hanno Becker5903de42019-05-03 14:46:38 +01009513 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9514
Hanno Becker78640902018-08-13 16:35:15 +01009515 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009516 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518#if defined(MBEDTLS_ZLIB_SUPPORT)
9519 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9520 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009521#endif
9522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525 case MBEDTLS_MODE_GCM:
9526 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009527 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009529 transform_expansion = transform->minlen;
9530 break;
9531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009533
9534 block_size = mbedtls_cipher_get_block_size(
9535 &transform->cipher_ctx_enc );
9536
Hanno Becker3136ede2018-08-17 15:28:19 +01009537 /* Expansion due to the addition of the MAC. */
9538 transform_expansion += transform->maclen;
9539
9540 /* Expansion due to the addition of CBC padding;
9541 * Theoretically up to 256 bytes, but we never use
9542 * more than the block size of the underlying cipher. */
9543 transform_expansion += block_size;
9544
9545 /* For TLS 1.1 or higher, an explicit IV is added
9546 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009547#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9548 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009549 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009550#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009551
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009552 break;
9553
9554 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009557 }
9558
Hanno Beckera0e20d02019-05-15 14:03:01 +01009559#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009560 if( transform->out_cid_len != 0 )
9561 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009562#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009563
Hanno Becker5903de42019-05-03 14:46:38 +01009564 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009565}
9566
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009567#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9568size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9569{
9570 size_t max_len;
9571
9572 /*
9573 * Assume mfl_code is correct since it was checked when set
9574 */
Angus Grattond8213d02016-05-25 20:56:48 +10009575 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009576
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009577 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009578 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009579 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009580 {
Angus Grattond8213d02016-05-25 20:56:48 +10009581 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009582 }
9583
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009584 /* During a handshake, use the value being negotiated */
9585 if( ssl->session_negotiate != NULL &&
9586 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9587 {
9588 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9589 }
9590
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009591 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009592}
9593#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9594
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009595#if defined(MBEDTLS_SSL_PROTO_DTLS)
9596static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9597{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009598 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9599 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9600 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9601 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9602 return ( 0 );
9603
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009604 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9605 return( ssl->mtu );
9606
9607 if( ssl->mtu == 0 )
9608 return( ssl->handshake->mtu );
9609
9610 return( ssl->mtu < ssl->handshake->mtu ?
9611 ssl->mtu : ssl->handshake->mtu );
9612}
9613#endif /* MBEDTLS_SSL_PROTO_DTLS */
9614
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009615int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9616{
9617 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9618
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009619#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9620 !defined(MBEDTLS_SSL_PROTO_DTLS)
9621 (void) ssl;
9622#endif
9623
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009624#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9625 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9626
9627 if( max_len > mfl )
9628 max_len = mfl;
9629#endif
9630
9631#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009632 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009633 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009634 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009635 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9636 const size_t overhead = (size_t) ret;
9637
9638 if( ret < 0 )
9639 return( ret );
9640
9641 if( mtu <= overhead )
9642 {
9643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9644 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9645 }
9646
9647 if( max_len > mtu - overhead )
9648 max_len = mtu - overhead;
9649 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009650#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009651
Hanno Becker0defedb2018-08-10 12:35:02 +01009652#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9653 !defined(MBEDTLS_SSL_PROTO_DTLS)
9654 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009655#endif
9656
9657 return( (int) max_len );
9658}
9659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009660#if defined(MBEDTLS_X509_CRT_PARSE_C)
9661const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009662{
9663 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009664 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009665
Hanno Beckere6824572019-02-07 13:18:46 +00009666#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009667 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009668#else
9669 return( NULL );
9670#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009671}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009675int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9676 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009677{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009678 if( ssl == NULL ||
9679 dst == NULL ||
9680 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009681 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009684 }
9685
Hanno Becker52055ae2019-02-06 14:30:46 +00009686 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009689
Paul Bakker5121ce52009-01-03 21:22:43 +00009690/*
Paul Bakker1961b702013-01-25 14:49:24 +01009691 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009694{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009696
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009697 if( ssl == NULL || ssl->conf == NULL )
9698 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009701 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009702 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009703#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009705 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009707#endif
9708
Paul Bakker1961b702013-01-25 14:49:24 +01009709 return( ret );
9710}
9711
9712/*
9713 * Perform the SSL handshake
9714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009715int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009716{
9717 int ret = 0;
9718
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009719 if( ssl == NULL || ssl->conf == NULL )
9720 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009724 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009726 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009727
9728 if( ret != 0 )
9729 break;
9730 }
9731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009733
9734 return( ret );
9735}
9736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737#if defined(MBEDTLS_SSL_RENEGOTIATION)
9738#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009739/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009740 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009743{
9744 int ret;
9745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009747
9748 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9750 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009751
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009752 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009753 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009754 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009755 return( ret );
9756 }
9757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009759
9760 return( 0 );
9761}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009763
9764/*
9765 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766 * - any side: calling mbedtls_ssl_renegotiate(),
9767 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9768 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009769 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009770 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009774{
9775 int ret;
9776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009778
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009779 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9780 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009781
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009782 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9783 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009785 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009787 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009788 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009789 ssl->handshake->out_msg_seq = 1;
9790 else
9791 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009792 }
9793#endif
9794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9796 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009801 return( ret );
9802 }
9803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009805
9806 return( 0 );
9807}
9808
9809/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009810 * Renegotiate current connection on client,
9811 * or request renegotiation on server
9812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009814{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009816
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009817 if( ssl == NULL || ssl->conf == NULL )
9818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009821 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009822 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009828
9829 /* Did we already try/start sending HelloRequest? */
9830 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009832
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009833 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009834 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009838 /*
9839 * On client, either start the renegotiation process or,
9840 * if already in progress, continue the handshake
9841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009846
9847 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009850 return( ret );
9851 }
9852 }
9853 else
9854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009858 return( ret );
9859 }
9860 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009862
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009863 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009864}
9865
9866/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009867 * Check record counters and renegotiate if they're above the limit.
9868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009870{
Andres AG2196c7f2016-12-15 17:01:16 +00009871 size_t ep_len = ssl_ep_len( ssl );
9872 int in_ctr_cmp;
9873 int out_ctr_cmp;
9874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009875 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9876 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009877 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009878 {
9879 return( 0 );
9880 }
9881
Andres AG2196c7f2016-12-15 17:01:16 +00009882 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9883 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009884 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009885 ssl->conf->renego_period + ep_len, 8 - ep_len );
9886
9887 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009888 {
9889 return( 0 );
9890 }
9891
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009894}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009895#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009896
9897/*
9898 * Receive application data decrypted from the SSL layer
9899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009901{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009902 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009903 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009904
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009905 if( ssl == NULL || ssl->conf == NULL )
9906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009910#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009911 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009914 return( ret );
9915
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009916 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009918 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009919 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009920 return( ret );
9921 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009922 }
9923#endif
9924
Hanno Becker4a810fb2017-05-24 16:27:30 +01009925 /*
9926 * Check if renegotiation is necessary and/or handshake is
9927 * in process. If yes, perform/continue, and fall through
9928 * if an unexpected packet is received while the client
9929 * is waiting for the ServerHello.
9930 *
9931 * (There is no equivalent to the last condition on
9932 * the server-side as it is not treated as within
9933 * a handshake while waiting for the ClientHello
9934 * after a renegotiation request.)
9935 */
9936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009938 ret = ssl_check_ctr_renegotiate( ssl );
9939 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9940 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009942 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009943 return( ret );
9944 }
9945#endif
9946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009950 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9951 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009954 return( ret );
9955 }
9956 }
9957
Hanno Beckere41158b2017-10-23 13:30:32 +01009958 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009959 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009960 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009961 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009962 if( ssl->f_get_timer != NULL &&
9963 ssl->f_get_timer( ssl->p_timer ) == -1 )
9964 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009965 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009966 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009967
Hanno Becker327c93b2018-08-15 13:56:18 +01009968 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009969 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009970 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9971 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009972
Hanno Becker4a810fb2017-05-24 16:27:30 +01009973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9974 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009975 }
9976
9977 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009979 {
9980 /*
9981 * OpenSSL sends empty messages to randomize the IV
9982 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009983 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009985 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009986 return( 0 );
9987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009989 return( ret );
9990 }
9991 }
9992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009993 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009996
Hanno Becker4a810fb2017-05-24 16:27:30 +01009997 /*
9998 * - For client-side, expect SERVER_HELLO_REQUEST.
9999 * - For server-side, expect CLIENT_HELLO.
10000 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10001 */
10002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010004 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010006 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010009
10010 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010011#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010012 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010013 {
10014 continue;
10015 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010016#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010017 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010018 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010019#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010020
Hanno Becker4a810fb2017-05-24 16:27:30 +010010021#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010022 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010026
10027 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010029 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010030 {
10031 continue;
10032 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010033#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010035 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010036#endif /* MBEDTLS_SSL_SRV_C */
10037
Hanno Becker21df7f92017-10-17 11:03:26 +010010038#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010039 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010040 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10041 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10042 ssl->conf->allow_legacy_renegotiation ==
10043 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10044 {
10045 /*
10046 * Accept renegotiation request
10047 */
Paul Bakker48916f92012-09-16 19:57:18 +000010048
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010049 /* DTLS clients need to know renego is server-initiated */
10050#if defined(MBEDTLS_SSL_PROTO_DTLS)
10051 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10052 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10053 {
10054 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10055 }
10056#endif
10057 ret = ssl_start_renegotiation( ssl );
10058 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10059 ret != 0 )
10060 {
10061 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10062 return( ret );
10063 }
10064 }
10065 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010066#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010067 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010068 /*
10069 * Refuse renegotiation
10070 */
10071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010074#if defined(MBEDTLS_SSL_PROTO_SSL3)
10075 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010076 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010077 /* SSLv3 does not have a "no_renegotiation" warning, so
10078 we send a fatal alert and abort the connection. */
10079 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10080 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10081 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010082 }
10083 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10085#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10086 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10087 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10090 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10091 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010092 {
10093 return( ret );
10094 }
Paul Bakker48916f92012-09-16 19:57:18 +000010095 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010096 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010097#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10098 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10101 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010102 }
Paul Bakker48916f92012-09-16 19:57:18 +000010103 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010104
Hanno Becker90333da2017-10-10 11:27:13 +010010105 /* At this point, we don't know whether the renegotiation has been
10106 * completed or not. The cases to consider are the following:
10107 * 1) The renegotiation is complete. In this case, no new record
10108 * has been read yet.
10109 * 2) The renegotiation is incomplete because the client received
10110 * an application data record while awaiting the ServerHello.
10111 * 3) The renegotiation is incomplete because the client received
10112 * a non-handshake, non-application data message while awaiting
10113 * the ServerHello.
10114 * In each of these case, looping will be the proper action:
10115 * - For 1), the next iteration will read a new record and check
10116 * if it's application data.
10117 * - For 2), the loop condition isn't satisfied as application data
10118 * is present, hence continue is the same as break
10119 * - For 3), the loop condition is satisfied and read_record
10120 * will re-deliver the message that was held back by the client
10121 * when expecting the ServerHello.
10122 */
10123 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010124 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010125#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010127 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010128 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010129 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010130 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010133 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010134 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010135 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010136 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010137 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010138#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010140 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10141 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010144 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010145 }
10146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010147 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10150 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010151 }
10152
10153 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010154
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010155 /* We're going to return something now, cancel timer,
10156 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010157 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010158 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010159
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010160#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010161 /* If we requested renego but received AppData, resend HelloRequest.
10162 * Do it now, after setting in_offt, to avoid taking this branch
10163 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010164#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010165 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010166 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010167 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010168 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010170 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010171 return( ret );
10172 }
10173 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010174#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010175#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010176 }
10177
10178 n = ( len < ssl->in_msglen )
10179 ? len : ssl->in_msglen;
10180
10181 memcpy( buf, ssl->in_offt, n );
10182 ssl->in_msglen -= n;
10183
10184 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010185 {
10186 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010187 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010188 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010189 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010190 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010191 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010192 /* more data available */
10193 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010194 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010197
Paul Bakker23986e52011-04-24 08:57:21 +000010198 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010199}
10200
10201/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010202 * Send application data to be encrypted by the SSL layer, taking care of max
10203 * fragment length and buffer size.
10204 *
10205 * According to RFC 5246 Section 6.2.1:
10206 *
10207 * Zero-length fragments of Application data MAY be sent as they are
10208 * potentially useful as a traffic analysis countermeasure.
10209 *
10210 * Therefore, it is possible that the input message length is 0 and the
10211 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010212 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010213static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010214 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010215{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010216 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10217 const size_t max_len = (size_t) ret;
10218
10219 if( ret < 0 )
10220 {
10221 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10222 return( ret );
10223 }
10224
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010225 if( len > max_len )
10226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010227#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010228 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010231 "maximum fragment length: %d > %d",
10232 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010234 }
10235 else
10236#endif
10237 len = max_len;
10238 }
Paul Bakker887bd502011-06-08 13:10:54 +000010239
Paul Bakker5121ce52009-01-03 21:22:43 +000010240 if( ssl->out_left != 0 )
10241 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010242 /*
10243 * The user has previously tried to send the data and
10244 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10245 * written. In this case, we expect the high-level write function
10246 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010248 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010251 return( ret );
10252 }
10253 }
Paul Bakker887bd502011-06-08 13:10:54 +000010254 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010255 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010256 /*
10257 * The user is trying to send a message the first time, so we need to
10258 * copy the data into the internal buffers and setup the data structure
10259 * to keep track of partial writes
10260 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010261 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010262 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010263 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010264
Hanno Becker67bc7c32018-08-06 11:33:50 +010010265 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010268 return( ret );
10269 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010270 }
10271
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010272 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010273}
10274
10275/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010276 * Write application data, doing 1/n-1 splitting if necessary.
10277 *
10278 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010279 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010280 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010282#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010283static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010284 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010285{
10286 int ret;
10287
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010288 if( ssl->conf->cbc_record_splitting ==
10289 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010290 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010291 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10292 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10293 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010294 {
10295 return( ssl_write_real( ssl, buf, len ) );
10296 }
10297
10298 if( ssl->split_done == 0 )
10299 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010300 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010301 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010302 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010303 }
10304
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010305 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10306 return( ret );
10307 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010308
10309 return( ret + 1 );
10310}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010311#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010312
10313/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010314 * Write application data (public-facing wrapper)
10315 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010316int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010317{
10318 int ret;
10319
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010321
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010322 if( ssl == NULL || ssl->conf == NULL )
10323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10324
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010325#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010326 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10327 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010328 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010329 return( ret );
10330 }
10331#endif
10332
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010333 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010334 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010335 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010336 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010338 return( ret );
10339 }
10340 }
10341
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010342#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010343 ret = ssl_write_split( ssl, buf, len );
10344#else
10345 ret = ssl_write_real( ssl, buf, len );
10346#endif
10347
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010349
10350 return( ret );
10351}
10352
10353/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010354 * Notify the peer that the connection is being closed
10355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010357{
10358 int ret;
10359
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010360 if( ssl == NULL || ssl->conf == NULL )
10361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010364
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010365 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010366 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010370 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10371 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10372 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010374 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010375 return( ret );
10376 }
10377 }
10378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010380
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010381 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010382}
10383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010384void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010385{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010386 if( transform == NULL )
10387 return;
10388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010389#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010390 deflateEnd( &transform->ctx_deflate );
10391 inflateEnd( &transform->ctx_inflate );
10392#endif
10393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010394 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10395 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010396
Hanno Beckerd56ed242018-01-03 15:32:51 +000010397#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398 mbedtls_md_free( &transform->md_ctx_enc );
10399 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010400#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010401
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010402 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010403}
10404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010405#if defined(MBEDTLS_X509_CRT_PARSE_C)
10406static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010408 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010409
10410 while( cur != NULL )
10411 {
10412 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010413 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010414 cur = next;
10415 }
10416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010417#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010418
Hanno Becker0271f962018-08-16 13:23:47 +010010419#if defined(MBEDTLS_SSL_PROTO_DTLS)
10420
10421static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10422{
10423 unsigned offset;
10424 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10425
10426 if( hs == NULL )
10427 return;
10428
Hanno Becker283f5ef2018-08-24 09:34:47 +010010429 ssl_free_buffered_record( ssl );
10430
Hanno Becker0271f962018-08-16 13:23:47 +010010431 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010432 ssl_buffering_free_slot( ssl, offset );
10433}
10434
10435static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10436 uint8_t slot )
10437{
10438 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10439 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010440
10441 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10442 return;
10443
Hanno Beckere605b192018-08-21 15:59:07 +010010444 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010445 {
Hanno Beckere605b192018-08-21 15:59:07 +010010446 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010447 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010448 mbedtls_free( hs_buf->data );
10449 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010450 }
10451}
10452
10453#endif /* MBEDTLS_SSL_PROTO_DTLS */
10454
Gilles Peskine9b562d52018-04-25 20:32:43 +020010455void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010456{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010457 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10458
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010459 if( handshake == NULL )
10460 return;
10461
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010462#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10463 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10464 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010465 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010466 handshake->async_in_progress = 0;
10467 }
10468#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10469
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010470#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10471 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10472 mbedtls_md5_free( &handshake->fin_md5 );
10473 mbedtls_sha1_free( &handshake->fin_sha1 );
10474#endif
10475#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10476#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010477#if defined(MBEDTLS_USE_PSA_CRYPTO)
10478 psa_hash_abort( &handshake->fin_sha256_psa );
10479#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010480 mbedtls_sha256_free( &handshake->fin_sha256 );
10481#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010482#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010483#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010484#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010485 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010486#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010487 mbedtls_sha512_free( &handshake->fin_sha512 );
10488#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010489#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010490#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492#if defined(MBEDTLS_DHM_C)
10493 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010494#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495#if defined(MBEDTLS_ECDH_C)
10496 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010497#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010498#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010499 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010500#if defined(MBEDTLS_SSL_CLI_C)
10501 mbedtls_free( handshake->ecjpake_cache );
10502 handshake->ecjpake_cache = NULL;
10503 handshake->ecjpake_cache_len = 0;
10504#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010505#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010506
Janos Follath4ae5c292016-02-10 11:27:43 +000010507#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10508 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010509 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010510 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010511#endif
10512
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010513#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10514 if( handshake->psk != NULL )
10515 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010516 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010517 mbedtls_free( handshake->psk );
10518 }
10519#endif
10520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010521#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10522 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010523 /*
10524 * Free only the linked list wrapper, not the keys themselves
10525 * since the belong to the SNI callback
10526 */
10527 if( handshake->sni_key_cert != NULL )
10528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010529 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010530
10531 while( cur != NULL )
10532 {
10533 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010534 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010535 cur = next;
10536 }
10537 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010538#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010539
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010540#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010541 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010542 if( handshake->ecrs_peer_cert != NULL )
10543 {
10544 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10545 mbedtls_free( handshake->ecrs_peer_cert );
10546 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010547#endif
10548
Hanno Becker75173122019-02-06 16:18:31 +000010549#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10550 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10551 mbedtls_pk_free( &handshake->peer_pubkey );
10552#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010554#if defined(MBEDTLS_SSL_PROTO_DTLS)
10555 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010556 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010557 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010558#endif
10559
Hanno Becker4a63ed42019-01-08 11:39:35 +000010560#if defined(MBEDTLS_ECDH_C) && \
10561 defined(MBEDTLS_USE_PSA_CRYPTO)
10562 psa_destroy_key( handshake->ecdh_psa_privkey );
10563#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10564
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010565 mbedtls_platform_zeroize( handshake,
10566 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010567}
10568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010569void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010570{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010571 if( session == NULL )
10572 return;
10573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010574#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010575 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010576#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010577
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010578#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010579 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010580#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010581
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010582 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010583}
10584
Paul Bakker5121ce52009-01-03 21:22:43 +000010585/*
10586 * Free an SSL context
10587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010588void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010589{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010590 if( ssl == NULL )
10591 return;
10592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010594
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010595 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010596 {
Angus Grattond8213d02016-05-25 20:56:48 +100010597 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010599 }
10600
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010601 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010602 {
Angus Grattond8213d02016-05-25 20:56:48 +100010603 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010604 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010605 }
10606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010607#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010608 if( ssl->compress_buf != NULL )
10609 {
Angus Grattond8213d02016-05-25 20:56:48 +100010610 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010611 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010612 }
10613#endif
10614
Paul Bakker48916f92012-09-16 19:57:18 +000010615 if( ssl->transform )
10616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617 mbedtls_ssl_transform_free( ssl->transform );
10618 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010619 }
10620
10621 if( ssl->handshake )
10622 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010623 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10625 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627 mbedtls_free( ssl->handshake );
10628 mbedtls_free( ssl->transform_negotiate );
10629 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010630 }
10631
Paul Bakkerc0463502013-02-14 11:19:38 +010010632 if( ssl->session )
10633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010634 mbedtls_ssl_session_free( ssl->session );
10635 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010636 }
10637
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010638#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010639 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010640 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010641 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010642 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010643 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010644#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010646#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10647 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10650 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010651 }
10652#endif
10653
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010654#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010656#endif
10657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010659
Paul Bakker86f04f42013-02-14 11:20:09 +010010660 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010661 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010662}
10663
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010664/*
10665 * Initialze mbedtls_ssl_config
10666 */
10667void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10668{
10669 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10670}
10671
Simon Butcherc97b6972015-12-27 23:48:17 +000010672#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010673static int ssl_preset_default_hashes[] = {
10674#if defined(MBEDTLS_SHA512_C)
10675 MBEDTLS_MD_SHA512,
10676 MBEDTLS_MD_SHA384,
10677#endif
10678#if defined(MBEDTLS_SHA256_C)
10679 MBEDTLS_MD_SHA256,
10680 MBEDTLS_MD_SHA224,
10681#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010682#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010683 MBEDTLS_MD_SHA1,
10684#endif
10685 MBEDTLS_MD_NONE
10686};
Simon Butcherc97b6972015-12-27 23:48:17 +000010687#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010688
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010689static int ssl_preset_suiteb_ciphersuites[] = {
10690 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10691 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10692 0
10693};
10694
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010695#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010696static int ssl_preset_suiteb_hashes[] = {
10697 MBEDTLS_MD_SHA256,
10698 MBEDTLS_MD_SHA384,
10699 MBEDTLS_MD_NONE
10700};
10701#endif
10702
10703#if defined(MBEDTLS_ECP_C)
10704static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010705#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010706 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010707#endif
10708#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010709 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010710#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010711 MBEDTLS_ECP_DP_NONE
10712};
10713#endif
10714
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010715/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010716 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010717 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010718int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010719 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010720{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010721#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010722 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010723#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010724
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010725 /* Use the functions here so that they are covered in tests,
10726 * but otherwise access member directly for efficiency */
10727 mbedtls_ssl_conf_endpoint( conf, endpoint );
10728 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010729
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010730 /*
10731 * Things that are common to all presets
10732 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010733#if defined(MBEDTLS_SSL_CLI_C)
10734 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10735 {
10736 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10737#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10738 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10739#endif
10740 }
10741#endif
10742
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010743#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010744 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010745#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010746
10747#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10748 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10749#endif
10750
10751#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10752 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10753#endif
10754
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010755#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10756 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10757#endif
10758
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010759#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010760 conf->f_cookie_write = ssl_cookie_write_dummy;
10761 conf->f_cookie_check = ssl_cookie_check_dummy;
10762#endif
10763
10764#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10765 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10766#endif
10767
Janos Follath088ce432017-04-10 12:42:31 +010010768#if defined(MBEDTLS_SSL_SRV_C)
10769 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10770#endif
10771
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010772#if defined(MBEDTLS_SSL_PROTO_DTLS)
10773 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10774 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10775#endif
10776
10777#if defined(MBEDTLS_SSL_RENEGOTIATION)
10778 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010779 memset( conf->renego_period, 0x00, 2 );
10780 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010781#endif
10782
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010783#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10784 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10785 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010786 const unsigned char dhm_p[] =
10787 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10788 const unsigned char dhm_g[] =
10789 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10790
Hanno Beckera90658f2017-10-04 15:29:08 +010010791 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10792 dhm_p, sizeof( dhm_p ),
10793 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010794 {
10795 return( ret );
10796 }
10797 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010798#endif
10799
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010800 /*
10801 * Preset-specific defaults
10802 */
10803 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010804 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010805 /*
10806 * NSA Suite B
10807 */
10808 case MBEDTLS_SSL_PRESET_SUITEB:
10809 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10810 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10811 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10812 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10813
10814 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10815 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10816 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10817 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10818 ssl_preset_suiteb_ciphersuites;
10819
10820#if defined(MBEDTLS_X509_CRT_PARSE_C)
10821 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010822#endif
10823
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010824#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010825 conf->sig_hashes = ssl_preset_suiteb_hashes;
10826#endif
10827
10828#if defined(MBEDTLS_ECP_C)
10829 conf->curve_list = ssl_preset_suiteb_curves;
10830#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010831 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010832
10833 /*
10834 * Default
10835 */
10836 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010837 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10838 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10839 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10840 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10841 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10842 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10843 MBEDTLS_SSL_MIN_MINOR_VERSION :
10844 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010845 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10846 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10847
10848#if defined(MBEDTLS_SSL_PROTO_DTLS)
10849 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10850 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10851#endif
10852
10853 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10854 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10855 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10856 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10857 mbedtls_ssl_list_ciphersuites();
10858
10859#if defined(MBEDTLS_X509_CRT_PARSE_C)
10860 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10861#endif
10862
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010863#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010864 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010865#endif
10866
10867#if defined(MBEDTLS_ECP_C)
10868 conf->curve_list = mbedtls_ecp_grp_id_list();
10869#endif
10870
10871#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10872 conf->dhm_min_bitlen = 1024;
10873#endif
10874 }
10875
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010876 return( 0 );
10877}
10878
10879/*
10880 * Free mbedtls_ssl_config
10881 */
10882void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10883{
10884#if defined(MBEDTLS_DHM_C)
10885 mbedtls_mpi_free( &conf->dhm_P );
10886 mbedtls_mpi_free( &conf->dhm_G );
10887#endif
10888
10889#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10890 if( conf->psk != NULL )
10891 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010892 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010893 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010894 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010895 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010896 }
10897
10898 if( conf->psk_identity != NULL )
10899 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010900 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010901 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010902 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010903 conf->psk_identity_len = 0;
10904 }
10905#endif
10906
10907#if defined(MBEDTLS_X509_CRT_PARSE_C)
10908 ssl_key_cert_free( conf->key_cert );
10909#endif
10910
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010911 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010912}
10913
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010914#if defined(MBEDTLS_PK_C) && \
10915 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010916/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010919unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010921#if defined(MBEDTLS_RSA_C)
10922 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10923 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010924#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010925#if defined(MBEDTLS_ECDSA_C)
10926 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10927 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010928#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010929 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010930}
10931
Hanno Becker7e5437a2017-04-28 17:15:26 +010010932unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10933{
10934 switch( type ) {
10935 case MBEDTLS_PK_RSA:
10936 return( MBEDTLS_SSL_SIG_RSA );
10937 case MBEDTLS_PK_ECDSA:
10938 case MBEDTLS_PK_ECKEY:
10939 return( MBEDTLS_SSL_SIG_ECDSA );
10940 default:
10941 return( MBEDTLS_SSL_SIG_ANON );
10942 }
10943}
10944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010945mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010946{
10947 switch( sig )
10948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010949#if defined(MBEDTLS_RSA_C)
10950 case MBEDTLS_SSL_SIG_RSA:
10951 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010952#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953#if defined(MBEDTLS_ECDSA_C)
10954 case MBEDTLS_SSL_SIG_ECDSA:
10955 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010956#endif
10957 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010958 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010959 }
10960}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010961#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010962
Hanno Becker7e5437a2017-04-28 17:15:26 +010010963#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10964 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10965
10966/* Find an entry in a signature-hash set matching a given hash algorithm. */
10967mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10968 mbedtls_pk_type_t sig_alg )
10969{
10970 switch( sig_alg )
10971 {
10972 case MBEDTLS_PK_RSA:
10973 return( set->rsa );
10974 case MBEDTLS_PK_ECDSA:
10975 return( set->ecdsa );
10976 default:
10977 return( MBEDTLS_MD_NONE );
10978 }
10979}
10980
10981/* Add a signature-hash-pair to a signature-hash set */
10982void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10983 mbedtls_pk_type_t sig_alg,
10984 mbedtls_md_type_t md_alg )
10985{
10986 switch( sig_alg )
10987 {
10988 case MBEDTLS_PK_RSA:
10989 if( set->rsa == MBEDTLS_MD_NONE )
10990 set->rsa = md_alg;
10991 break;
10992
10993 case MBEDTLS_PK_ECDSA:
10994 if( set->ecdsa == MBEDTLS_MD_NONE )
10995 set->ecdsa = md_alg;
10996 break;
10997
10998 default:
10999 break;
11000 }
11001}
11002
11003/* Allow exactly one hash algorithm for each signature. */
11004void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11005 mbedtls_md_type_t md_alg )
11006{
11007 set->rsa = md_alg;
11008 set->ecdsa = md_alg;
11009}
11010
11011#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11012 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11013
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011014/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011015 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011018{
11019 switch( hash )
11020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011021#if defined(MBEDTLS_MD5_C)
11022 case MBEDTLS_SSL_HASH_MD5:
11023 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011024#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025#if defined(MBEDTLS_SHA1_C)
11026 case MBEDTLS_SSL_HASH_SHA1:
11027 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011028#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011029#if defined(MBEDTLS_SHA256_C)
11030 case MBEDTLS_SSL_HASH_SHA224:
11031 return( MBEDTLS_MD_SHA224 );
11032 case MBEDTLS_SSL_HASH_SHA256:
11033 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011035#if defined(MBEDTLS_SHA512_C)
11036 case MBEDTLS_SSL_HASH_SHA384:
11037 return( MBEDTLS_MD_SHA384 );
11038 case MBEDTLS_SSL_HASH_SHA512:
11039 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011040#endif
11041 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011042 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011043 }
11044}
11045
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011046/*
11047 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11048 */
11049unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11050{
11051 switch( md )
11052 {
11053#if defined(MBEDTLS_MD5_C)
11054 case MBEDTLS_MD_MD5:
11055 return( MBEDTLS_SSL_HASH_MD5 );
11056#endif
11057#if defined(MBEDTLS_SHA1_C)
11058 case MBEDTLS_MD_SHA1:
11059 return( MBEDTLS_SSL_HASH_SHA1 );
11060#endif
11061#if defined(MBEDTLS_SHA256_C)
11062 case MBEDTLS_MD_SHA224:
11063 return( MBEDTLS_SSL_HASH_SHA224 );
11064 case MBEDTLS_MD_SHA256:
11065 return( MBEDTLS_SSL_HASH_SHA256 );
11066#endif
11067#if defined(MBEDTLS_SHA512_C)
11068 case MBEDTLS_MD_SHA384:
11069 return( MBEDTLS_SSL_HASH_SHA384 );
11070 case MBEDTLS_MD_SHA512:
11071 return( MBEDTLS_SSL_HASH_SHA512 );
11072#endif
11073 default:
11074 return( MBEDTLS_SSL_HASH_NONE );
11075 }
11076}
11077
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011078#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011079/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011080 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011081 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011082 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011083int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011085 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011086
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011087 if( ssl->conf->curve_list == NULL )
11088 return( -1 );
11089
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011090 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011091 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011092 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011093
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011094 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011095}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011096#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011097
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011098#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011099/*
11100 * Check if a hash proposed by the peer is in our list.
11101 * Return 0 if we're willing to use it, -1 otherwise.
11102 */
11103int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11104 mbedtls_md_type_t md )
11105{
11106 const int *cur;
11107
11108 if( ssl->conf->sig_hashes == NULL )
11109 return( -1 );
11110
11111 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11112 if( *cur == (int) md )
11113 return( 0 );
11114
11115 return( -1 );
11116}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011117#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011119#if defined(MBEDTLS_X509_CRT_PARSE_C)
11120int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11121 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011122 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011123 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011124{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011125 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011126#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011127 int usage = 0;
11128#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011129#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011130 const char *ext_oid;
11131 size_t ext_len;
11132#endif
11133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011134#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11135 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011136 ((void) cert);
11137 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011138 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011139#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011141#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11142 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011143 {
11144 /* Server part of the key exchange */
11145 switch( ciphersuite->key_exchange )
11146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011147 case MBEDTLS_KEY_EXCHANGE_RSA:
11148 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011149 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011150 break;
11151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011152 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11153 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11154 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11155 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011156 break;
11157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011158 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11159 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011160 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011161 break;
11162
11163 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011164 case MBEDTLS_KEY_EXCHANGE_NONE:
11165 case MBEDTLS_KEY_EXCHANGE_PSK:
11166 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11167 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011168 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011169 usage = 0;
11170 }
11171 }
11172 else
11173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011174 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11175 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011176 }
11177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011178 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011179 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011180 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011181 ret = -1;
11182 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011183#else
11184 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011185#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011187#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11188 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011190 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11191 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011192 }
11193 else
11194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011195 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11196 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011197 }
11198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011199 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011200 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011201 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011202 ret = -1;
11203 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011204#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011205
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011206 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011207}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011208#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011209
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011210/*
11211 * Convert version numbers to/from wire format
11212 * and, for DTLS, to/from TLS equivalent.
11213 *
11214 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011215 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011216 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11217 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011219void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011220 unsigned char ver[2] )
11221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011222#if defined(MBEDTLS_SSL_PROTO_DTLS)
11223 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011225 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011226 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11227
11228 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11229 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11230 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011231 else
11232#else
11233 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011234#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011235 {
11236 ver[0] = (unsigned char) major;
11237 ver[1] = (unsigned char) minor;
11238 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011239}
11240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011241void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011242 const unsigned char ver[2] )
11243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011244#if defined(MBEDTLS_SSL_PROTO_DTLS)
11245 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011246 {
11247 *major = 255 - ver[0] + 2;
11248 *minor = 255 - ver[1] + 1;
11249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011250 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011251 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11252 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011253 else
11254#else
11255 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011256#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011257 {
11258 *major = ver[0];
11259 *minor = ver[1];
11260 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011261}
11262
Simon Butcher99000142016-10-13 17:21:01 +010011263int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11264{
11265#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11266 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11267 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11268
11269 switch( md )
11270 {
11271#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11272#if defined(MBEDTLS_MD5_C)
11273 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011274 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011275#endif
11276#if defined(MBEDTLS_SHA1_C)
11277 case MBEDTLS_SSL_HASH_SHA1:
11278 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11279 break;
11280#endif
11281#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11282#if defined(MBEDTLS_SHA512_C)
11283 case MBEDTLS_SSL_HASH_SHA384:
11284 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11285 break;
11286#endif
11287#if defined(MBEDTLS_SHA256_C)
11288 case MBEDTLS_SSL_HASH_SHA256:
11289 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11290 break;
11291#endif
11292 default:
11293 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11294 }
11295
11296 return 0;
11297#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11298 (void) ssl;
11299 (void) md;
11300
11301 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11302#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11303}
11304
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011305#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11306 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11307int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11308 unsigned char *output,
11309 unsigned char *data, size_t data_len )
11310{
11311 int ret = 0;
11312 mbedtls_md5_context mbedtls_md5;
11313 mbedtls_sha1_context mbedtls_sha1;
11314
11315 mbedtls_md5_init( &mbedtls_md5 );
11316 mbedtls_sha1_init( &mbedtls_sha1 );
11317
11318 /*
11319 * digitally-signed struct {
11320 * opaque md5_hash[16];
11321 * opaque sha_hash[20];
11322 * };
11323 *
11324 * md5_hash
11325 * MD5(ClientHello.random + ServerHello.random
11326 * + ServerParams);
11327 * sha_hash
11328 * SHA(ClientHello.random + ServerHello.random
11329 * + ServerParams);
11330 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011331 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011332 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011334 goto exit;
11335 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011336 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011337 ssl->handshake->randbytes, 64 ) ) != 0 )
11338 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011340 goto exit;
11341 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011342 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011343 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011345 goto exit;
11346 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011347 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011348 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011350 goto exit;
11351 }
11352
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011353 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011354 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011356 goto exit;
11357 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011358 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011359 ssl->handshake->randbytes, 64 ) ) != 0 )
11360 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011362 goto exit;
11363 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011364 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011365 data_len ) ) != 0 )
11366 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011368 goto exit;
11369 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011370 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011371 output + 16 ) ) != 0 )
11372 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011374 goto exit;
11375 }
11376
11377exit:
11378 mbedtls_md5_free( &mbedtls_md5 );
11379 mbedtls_sha1_free( &mbedtls_sha1 );
11380
11381 if( ret != 0 )
11382 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11383 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11384
11385 return( ret );
11386
11387}
11388#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11389 MBEDTLS_SSL_PROTO_TLS1_1 */
11390
11391#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11392 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011393
11394#if defined(MBEDTLS_USE_PSA_CRYPTO)
11395int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11396 unsigned char *hash, size_t *hashlen,
11397 unsigned char *data, size_t data_len,
11398 mbedtls_md_type_t md_alg )
11399{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011400 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011401 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011402 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11403
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011405
11406 if( ( status = psa_hash_setup( &hash_operation,
11407 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011408 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011409 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011410 goto exit;
11411 }
11412
Andrzej Kurek814feff2019-01-14 04:35:19 -050011413 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11414 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011415 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011416 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011417 goto exit;
11418 }
11419
Andrzej Kurek814feff2019-01-14 04:35:19 -050011420 if( ( status = psa_hash_update( &hash_operation,
11421 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011422 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011423 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011424 goto exit;
11425 }
11426
Andrzej Kurek814feff2019-01-14 04:35:19 -050011427 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11428 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011429 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011430 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011431 goto exit;
11432 }
11433
11434exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011435 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011436 {
11437 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11438 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011439 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011440 {
11441 case PSA_ERROR_NOT_SUPPORTED:
11442 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011443 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011444 case PSA_ERROR_BUFFER_TOO_SMALL:
11445 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11446 case PSA_ERROR_INSUFFICIENT_MEMORY:
11447 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11448 default:
11449 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11450 }
11451 }
11452 return( 0 );
11453}
11454
11455#else
11456
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011457int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011458 unsigned char *hash, size_t *hashlen,
11459 unsigned char *data, size_t data_len,
11460 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011461{
11462 int ret = 0;
11463 mbedtls_md_context_t ctx;
11464 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011465 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011466
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011467 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011468
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011469 mbedtls_md_init( &ctx );
11470
11471 /*
11472 * digitally-signed struct {
11473 * opaque client_random[32];
11474 * opaque server_random[32];
11475 * ServerDHParams params;
11476 * };
11477 */
11478 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11479 {
11480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11481 goto exit;
11482 }
11483 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11484 {
11485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11486 goto exit;
11487 }
11488 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11489 {
11490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11491 goto exit;
11492 }
11493 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11494 {
11495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11496 goto exit;
11497 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011498 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011499 {
11500 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11501 goto exit;
11502 }
11503
11504exit:
11505 mbedtls_md_free( &ctx );
11506
11507 if( ret != 0 )
11508 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11509 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11510
11511 return( ret );
11512}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011513#endif /* MBEDTLS_USE_PSA_CRYPTO */
11514
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011515#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11516 MBEDTLS_SSL_PROTO_TLS1_2 */
11517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011518#endif /* MBEDTLS_SSL_TLS_C */