blob: d1b4e7c98ff99c396638e1ea127e2d24bad83b3b [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Hanno Becker2a43f6f2018-08-10 11:12:52 +010062static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010063static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010064
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010070 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010071#else
72 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010073#endif
74 return( 0 );
75}
76
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077/*
78 * Start a timer.
79 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020083 if( ssl->f_set_timer == NULL )
84 return;
85
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
87 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088}
89
90/*
91 * Return -1 is timer is expired, 0 if it isn't.
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020094{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020095 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020096 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020097
98 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020099 {
100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200102 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103
104 return( 0 );
105}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200106
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100107static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
108 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100109static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100110
Hanno Beckercfe45792019-07-03 16:13:00 +0100111#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +0100112static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
113 unsigned char *buf,
114 size_t len,
115 mbedtls_record *rec );
116
Hanno Beckercfe45792019-07-03 16:13:00 +0100117int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
118 unsigned char *buf,
119 size_t buflen )
120{
Hanno Becker54229812019-07-12 14:40:00 +0100121 int ret = 0;
122 mbedtls_record rec;
123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
124 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
125
126 /* We don't support record checking in TLS because
127 * (a) there doesn't seem to be a usecase for it, and
128 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
129 * and we'd need to backup the transform here.
130 */
131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
132 {
133 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
134 goto exit;
135 }
136#if defined(MBEDTLS_SSL_PROTO_DTLS)
137 else
138 {
139 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
140 if( ret != 0 )
141 {
142 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
143 goto exit;
144 }
145
146 if( ssl->transform_in != NULL )
147 {
148 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
149 if( ret != 0 )
150 {
151 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
152 goto exit;
153 }
154 }
155 }
156#endif /* MBEDTLS_SSL_PROTO_DTLS */
157
158exit:
159 /* On success, we have decrypted the buffer in-place, so make
160 * sure we don't leak any plaintext data. */
161 mbedtls_platform_zeroize( buf, buflen );
162
163 /* For the purpose of this API, treat messages with unexpected CID
164 * as well as such from future epochs as unexpected. */
165 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
166 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
167 {
168 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
169 }
170
171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
172 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100173}
174#endif /* MBEDTLS_SSL_RECORD_CHECKING */
175
Hanno Becker67bc7c32018-08-06 11:33:50 +0100176#define SSL_DONT_FORCE_FLUSH 0
177#define SSL_FORCE_FLUSH 1
178
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200179#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100180
Hanno Beckera0e20d02019-05-15 14:03:01 +0100181#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100182/* Top-level Connection ID API */
183
Hanno Becker8367ccc2019-05-14 11:30:10 +0100184int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
185 size_t len,
186 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100187{
188 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
190
Hanno Becker611ac772019-05-14 11:45:26 +0100191 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
192 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
193 {
194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
195 }
196
197 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100198 conf->cid_len = len;
199 return( 0 );
200}
201
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100202int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
203 int enable,
204 unsigned char const *own_cid,
205 size_t own_cid_len )
206{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100207 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
209
Hanno Beckerca092242019-04-25 16:01:49 +0100210 ssl->negotiate_cid = enable;
211 if( enable == MBEDTLS_SSL_CID_DISABLED )
212 {
213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
214 return( 0 );
215 }
216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100217 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100218
Hanno Beckerad4a1372019-05-03 13:06:44 +0100219 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100220 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
222 (unsigned) own_cid_len,
223 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
225 }
226
227 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100228 /* Truncation is not an issue here because
229 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
230 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100231
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100232 return( 0 );
233}
234
235int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
236 int *enabled,
237 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
238 size_t *peer_cid_len )
239{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100240 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100241
Hanno Becker76a79ab2019-05-03 14:38:32 +0100242 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
243 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
244 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100246 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100247
Hanno Beckerc5f24222019-05-03 12:54:52 +0100248 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
249 * were used, but client and server requested the empty CID.
250 * This is indistinguishable from not using the CID extension
251 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100252 if( ssl->transform_in->in_cid_len == 0 &&
253 ssl->transform_in->out_cid_len == 0 )
254 {
255 return( 0 );
256 }
257
Hanno Becker615ef172019-05-22 16:50:35 +0100258 if( peer_cid_len != NULL )
259 {
260 *peer_cid_len = ssl->transform_in->out_cid_len;
261 if( peer_cid != NULL )
262 {
263 memcpy( peer_cid, ssl->transform_in->out_cid,
264 ssl->transform_in->out_cid_len );
265 }
266 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100267
268 *enabled = MBEDTLS_SSL_CID_ENABLED;
269
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100270 return( 0 );
271}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100272#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100273
Hanno Beckerd5847772018-08-28 10:09:23 +0100274/* Forward declarations for functions related to message buffering. */
275static void ssl_buffering_free( mbedtls_ssl_context *ssl );
276static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
277 uint8_t slot );
278static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
279static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
280static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
281static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100282static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
283 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100284static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100285
Hanno Beckera67dee22018-08-22 10:05:20 +0100286static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100287static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100288{
Hanno Becker11682cc2018-08-22 14:41:02 +0100289 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100290
291 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100292 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100293
294 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
295}
296
Hanno Becker67bc7c32018-08-06 11:33:50 +0100297static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
298{
Hanno Becker11682cc2018-08-22 14:41:02 +0100299 size_t const bytes_written = ssl->out_left;
300 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100301
302 /* Double-check that the write-index hasn't gone
303 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100304 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100305 {
306 /* Should never happen... */
307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
308 }
309
310 return( (int) ( mtu - bytes_written ) );
311}
312
313static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
314{
315 int ret;
316 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400317 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100318
319#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
320 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
321
322 if( max_len > mfl )
323 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100324
325 /* By the standard (RFC 6066 Sect. 4), the MFL extension
326 * only limits the maximum record payload size, so in theory
327 * we would be allowed to pack multiple records of payload size
328 * MFL into a single datagram. However, this would mean that there's
329 * no way to explicitly communicate MTU restrictions to the peer.
330 *
331 * The following reduction of max_len makes sure that we never
332 * write datagrams larger than MFL + Record Expansion Overhead.
333 */
334 if( max_len <= ssl->out_left )
335 return( 0 );
336
337 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100338#endif
339
340 ret = ssl_get_remaining_space_in_datagram( ssl );
341 if( ret < 0 )
342 return( ret );
343 remaining = (size_t) ret;
344
345 ret = mbedtls_ssl_get_record_expansion( ssl );
346 if( ret < 0 )
347 return( ret );
348 expansion = (size_t) ret;
349
350 if( remaining <= expansion )
351 return( 0 );
352
353 remaining -= expansion;
354 if( remaining >= max_len )
355 remaining = max_len;
356
357 return( (int) remaining );
358}
359
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200360/*
361 * Double the retransmit timeout value, within the allowed range,
362 * returning -1 if the maximum value has already been reached.
363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200365{
366 uint32_t new_timeout;
367
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200368 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200369 return( -1 );
370
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200371 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
372 * in the following way: after the initial transmission and a first
373 * retransmission, back off to a temporary estimated MTU of 508 bytes.
374 * This value is guaranteed to be deliverable (if not guaranteed to be
375 * delivered) of any compliant IPv4 (and IPv6) network, and should work
376 * on most non-IP stacks too. */
377 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400378 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200379 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
381 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200382
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200383 new_timeout = 2 * ssl->handshake->retransmit_timeout;
384
385 /* Avoid arithmetic overflow and range overflow */
386 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200387 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200388 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200389 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200390 }
391
392 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200394 ssl->handshake->retransmit_timeout ) );
395
396 return( 0 );
397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200400{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200401 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200403 ssl->handshake->retransmit_timeout ) );
404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200408/*
409 * Convert max_fragment_length codes to length.
410 * RFC 6066 says:
411 * enum{
412 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
413 * } MaxFragmentLength;
414 * and we add 0 -> extension unused
415 */
Angus Grattond8213d02016-05-25 20:56:48 +1000416static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200417{
Angus Grattond8213d02016-05-25 20:56:48 +1000418 switch( mfl )
419 {
420 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
421 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
422 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
423 return 512;
424 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
425 return 1024;
426 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
427 return 2048;
428 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
429 return 4096;
430 default:
431 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
432 }
433}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200435
Hanno Becker52055ae2019-02-06 14:30:46 +0000436int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
437 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200438{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ssl_session_free( dst );
440 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000443
444#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200445 if( src->peer_cert != NULL )
446 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200447 int ret;
448
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200449 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200450 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200451 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200456 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200459 dst->peer_cert = NULL;
460 return( ret );
461 }
462 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000463#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000464 if( src->peer_cert_digest != NULL )
465 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000466 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000467 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000468 if( dst->peer_cert_digest == NULL )
469 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
470
471 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
472 src->peer_cert_digest_len );
473 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000474 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000475 }
476#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200479
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200480#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200481 if( src->ticket != NULL )
482 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200483 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200484 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200485 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200486
487 memcpy( dst->ticket, src->ticket, src->ticket_len );
488 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200489#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200490
491 return( 0 );
492}
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
495int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200496 const unsigned char *key_enc, const unsigned char *key_dec,
497 size_t keylen,
498 const unsigned char *iv_enc, const unsigned char *iv_dec,
499 size_t ivlen,
500 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200501 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
503int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
504int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
505int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
506int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
507#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000508
Paul Bakker5121ce52009-01-03 21:22:43 +0000509/*
510 * Key material generation
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200513static int ssl3_prf( const unsigned char *secret, size_t slen,
514 const char *label,
515 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000516 unsigned char *dstbuf, size_t dlen )
517{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100518 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000519 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200520 mbedtls_md5_context md5;
521 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000522 unsigned char padding[16];
523 unsigned char sha1sum[20];
524 ((void)label);
525
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200526 mbedtls_md5_init( &md5 );
527 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200528
Paul Bakker5f70b252012-09-13 14:23:06 +0000529 /*
530 * SSLv3:
531 * block =
532 * MD5( secret + SHA1( 'A' + secret + random ) ) +
533 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
534 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
535 * ...
536 */
537 for( i = 0; i < dlen / 16; i++ )
538 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200539 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000540
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100541 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100542 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100543 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100544 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100545 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100546 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100547 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100548 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100549 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100550 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000551
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100552 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100553 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100554 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100555 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100556 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100557 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100558 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100559 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000560 }
561
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100562exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200563 mbedtls_md5_free( &md5 );
564 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000565
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500566 mbedtls_platform_zeroize( padding, sizeof( padding ) );
567 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000568
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100569 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200574static int tls1_prf( const unsigned char *secret, size_t slen,
575 const char *label,
576 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000577 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578{
Paul Bakker23986e52011-04-24 08:57:21 +0000579 size_t nb, hs;
580 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200581 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300582 unsigned char *tmp;
583 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 const mbedtls_md_info_t *md_info;
586 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100587 int ret;
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Ron Eldor3b350852019-05-07 18:31:49 +0300591 tmp_len = 20 + strlen( label ) + rlen;
592 tmp = mbedtls_calloc( 1, tmp_len );
593 if( tmp == NULL )
594 {
595 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
596 goto exit;
597 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 hs = ( slen + 1 ) / 2;
600 S1 = secret;
601 S2 = secret + slen - hs;
602
603 nb = strlen( label );
604 memcpy( tmp + 20, label, nb );
605 memcpy( tmp + 20 + nb, random, rlen );
606 nb += rlen;
607
608 /*
609 * First compute P_md5(secret,label+random)[0..dlen]
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300612 {
613 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
614 goto exit;
615 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300618 {
619 goto exit;
620 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
623 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
624 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626 for( i = 0; i < dlen; i += 16 )
627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_md_hmac_reset ( &md_ctx );
629 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
630 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
634 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
637
638 for( j = 0; j < k; j++ )
639 dstbuf[i + j] = h_i[j];
640 }
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100643
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 /*
645 * XOR out with P_sha1(secret,label+random)[0..dlen]
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300648 {
649 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
650 goto exit;
651 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300654 {
655 goto exit;
656 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
659 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
660 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
662 for( i = 0; i < dlen; i += 20 )
663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_md_hmac_reset ( &md_ctx );
665 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
666 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_hmac_reset ( &md_ctx );
669 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
670 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
673
674 for( j = 0; j < k; j++ )
675 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
676 }
677
Ron Eldor3b350852019-05-07 18:31:49 +0300678exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100680
Ron Eldor3b350852019-05-07 18:31:49 +0300681 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500682 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Ron Eldor3b350852019-05-07 18:31:49 +0300684 mbedtls_free( tmp );
685 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500690#if defined(MBEDTLS_USE_PSA_CRYPTO)
691static int tls_prf_generic( mbedtls_md_type_t md_type,
692 const unsigned char *secret, size_t slen,
693 const char *label,
694 const unsigned char *random, size_t rlen,
695 unsigned char *dstbuf, size_t dlen )
696{
697 psa_status_t status;
698 psa_algorithm_t alg;
Janos Follath53b8ec22019-08-08 10:28:27 +0100699 psa_key_attributes_t key_attributes;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500700 psa_key_handle_t master_slot;
Janos Follathda6ac012019-08-16 13:47:29 +0100701 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100702 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500703
Andrzej Kurekc929a822019-01-14 03:51:11 -0500704 if( md_type == MBEDTLS_MD_SHA384 )
705 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
706 else
707 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
708
Janos Follath53b8ec22019-08-08 10:28:27 +0100709 key_attributes = psa_key_attributes_init();
710 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
711 psa_set_key_algorithm( &key_attributes, alg );
712 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500713
Janos Follath53b8ec22019-08-08 10:28:27 +0100714 status = psa_import_key( &key_attributes, secret, slen, &master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500715 if( status != PSA_SUCCESS )
716 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
717
Janos Follathda6ac012019-08-16 13:47:29 +0100718 status = psa_key_derivation( &derivation,
Andrzej Kurekc929a822019-01-14 03:51:11 -0500719 master_slot, alg,
720 random, rlen,
721 (unsigned char const *) label,
722 (size_t) strlen( label ),
723 dlen );
724 if( status != PSA_SUCCESS )
725 {
Janos Follathda6ac012019-08-16 13:47:29 +0100726 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500727 psa_destroy_key( master_slot );
728 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
729 }
730
Janos Follathda6ac012019-08-16 13:47:29 +0100731 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500732 if( status != PSA_SUCCESS )
733 {
Janos Follathda6ac012019-08-16 13:47:29 +0100734 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500735 psa_destroy_key( master_slot );
736 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
737 }
738
Janos Follathda6ac012019-08-16 13:47:29 +0100739 status = psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500740 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500741 {
742 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500743 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500744 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500745
746 status = psa_destroy_key( master_slot );
747 if( status != PSA_SUCCESS )
748 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
749
Andrzej Kurek33171262019-01-15 03:25:18 -0500750 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500751}
752
753#else /* MBEDTLS_USE_PSA_CRYPTO */
754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100756 const unsigned char *secret, size_t slen,
757 const char *label,
758 const unsigned char *random, size_t rlen,
759 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000760{
761 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100762 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300763 unsigned char *tmp;
764 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
Ron Eldor3b350852019-05-07 18:31:49 +0300777 tmp_len = md_len + strlen( label ) + rlen;
778 tmp = mbedtls_calloc( 1, tmp_len );
779 if( tmp == NULL )
780 {
781 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
782 goto exit;
783 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000784
785 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100786 memcpy( tmp + md_len, label, nb );
787 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000788 nb += rlen;
789
790 /*
791 * Compute P_<hash>(secret, label + random)[0..dlen]
792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300794 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
797 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
798 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100799
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100800 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_md_hmac_reset ( &md_ctx );
803 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
804 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_hmac_reset ( &md_ctx );
807 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
808 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000809
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100810 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000811
812 for( j = 0; j < k; j++ )
813 dstbuf[i + j] = h_i[j];
814 }
815
Ron Eldor3b350852019-05-07 18:31:49 +0300816exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100818
Ron Eldor3b350852019-05-07 18:31:49 +0300819 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500820 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000821
Ron Eldor3b350852019-05-07 18:31:49 +0300822 mbedtls_free( tmp );
823
824 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000825}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500826#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100828static int tls_prf_sha256( const unsigned char *secret, size_t slen,
829 const char *label,
830 const unsigned char *random, size_t rlen,
831 unsigned char *dstbuf, size_t dlen )
832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100834 label, random, rlen, dstbuf, dlen ) );
835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200839static int tls_prf_sha384( const unsigned char *secret, size_t slen,
840 const char *label,
841 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000842 unsigned char *dstbuf, size_t dlen )
843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100845 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_SHA512_C */
848#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
853 defined(MBEDTLS_SSL_PROTO_TLS1_1)
854static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#endif
Paul Bakker380da532012-04-18 16:10:25 +0000856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_SSL_PROTO_SSL3)
858static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
859static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200860#endif
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
863static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
864static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200865#endif
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
868#if defined(MBEDTLS_SHA256_C)
869static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
870static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
871static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_SHA512_C)
875static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
876static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
877static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000880
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100881#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100882 defined(MBEDTLS_USE_PSA_CRYPTO)
883static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
884{
885 if( ssl->conf->f_psk != NULL )
886 {
887 /* If we've used a callback to select the PSK,
888 * the static configuration is irrelevant. */
889 if( ssl->handshake->psk_opaque != 0 )
890 return( 1 );
891
892 return( 0 );
893 }
894
895 if( ssl->conf->psk_opaque != 0 )
896 return( 1 );
897
898 return( 0 );
899}
900#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100901 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100902
Ron Eldorcf280092019-05-14 20:19:13 +0300903#if defined(MBEDTLS_SSL_EXPORT_KEYS)
904static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
905{
906#if defined(MBEDTLS_SSL_PROTO_SSL3)
907 if( tls_prf == ssl3_prf )
908 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300909 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300910 }
911 else
912#endif
913#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
914 if( tls_prf == tls1_prf )
915 {
916 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
917 }
918 else
919#endif
920#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
921#if defined(MBEDTLS_SHA512_C)
922 if( tls_prf == tls_prf_sha384 )
923 {
924 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
925 }
926 else
927#endif
928#if defined(MBEDTLS_SHA256_C)
929 if( tls_prf == tls_prf_sha256 )
930 {
931 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
932 }
933 else
934#endif
935#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
936 return( MBEDTLS_SSL_TLS_PRF_NONE );
937}
938#endif /* MBEDTLS_SSL_EXPORT_KEYS */
939
Ron Eldor51d3ab52019-05-12 14:54:30 +0300940int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
941 const unsigned char *secret, size_t slen,
942 const char *label,
943 const unsigned char *random, size_t rlen,
944 unsigned char *dstbuf, size_t dlen )
945{
946 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
947
948 switch( prf )
949 {
950#if defined(MBEDTLS_SSL_PROTO_SSL3)
951 case MBEDTLS_SSL_TLS_PRF_SSL3:
952 tls_prf = ssl3_prf;
953 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300954#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300955#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
956 case MBEDTLS_SSL_TLS_PRF_TLS1:
957 tls_prf = tls1_prf;
958 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300959#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
960
961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300962#if defined(MBEDTLS_SHA512_C)
963 case MBEDTLS_SSL_TLS_PRF_SHA384:
964 tls_prf = tls_prf_sha384;
965 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300966#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300967#if defined(MBEDTLS_SHA256_C)
968 case MBEDTLS_SSL_TLS_PRF_SHA256:
969 tls_prf = tls_prf_sha256;
970 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300971#endif /* MBEDTLS_SHA256_C */
972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300973 default:
974 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
975 }
976
977 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
978}
979
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200980/*
981 * This function will ultimetaly only be responsible for populating a
982 * transform structure from data passed as explicit parameters.
983 *
984 * For now however it's doing rather more in a rather less explicit way.
985 */
986static int ssl_populate_transform( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000987{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200988 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000989#if defined(MBEDTLS_USE_PSA_CRYPTO)
990 int psa_fallthrough;
991#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 unsigned char keyblk[256];
994 unsigned char *key1;
995 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100996 unsigned char *mac_enc;
997 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000998 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200999 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001000 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001001 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 const mbedtls_cipher_info_t *cipher_info;
1003 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001004
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001005 /* cf. RFC 5246, Section 8.1:
1006 * "The master secret is always exactly 48 bytes in length." */
1007 size_t const master_secret_len = 48;
1008
Hanno Becker35b23c72018-10-23 12:10:41 +01001009#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1010 unsigned char session_hash[48];
1011#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_ssl_session *session = ssl->session_negotiate;
1014 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
1015 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Jaeden Amero2de07f12019-06-05 13:32:08 +01001019#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1020 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001021 transform->encrypt_then_mac = session->encrypt_then_mac;
1022#endif
1023 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001024
1025 ciphersuite_info = handshake->ciphersuite_info;
1026 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001027 if( cipher_info == NULL )
1028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001030 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001032 }
1033
Hanno Beckere694c3e2017-12-27 21:34:08 +00001034 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001035 if( md_info == NULL )
1036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001038 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001040 }
1041
Hanno Beckera0e20d02019-05-15 14:03:01 +01001042#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001043 /* Copy own and peer's CID if the use of the CID
1044 * extension has been negotiated. */
1045 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1046 {
1047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001048
Hanno Becker05154c32019-05-03 15:23:51 +01001049 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001050 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001051 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001052 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001053
1054 transform->out_cid_len = ssl->handshake->peer_cid_len;
1055 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1056 ssl->handshake->peer_cid_len );
1057 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1058 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001059 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001060#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001061
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 /*
1063 * SSLv3:
1064 * master =
1065 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1066 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1067 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001068 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001069 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 * master = PRF( premaster, "master secret", randbytes )[0..47]
1071 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001072 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1075 }
1076 else
1077 {
1078 /* The label for the KDF used for key expansion.
1079 * This is either "master secret" or "extended master secret"
1080 * depending on whether the Extended Master Secret extension
1081 * is used. */
1082 char const *lbl = "master secret";
1083
1084 /* The salt for the KDF used for key expansion.
1085 * - If the Extended Master Secret extension is not used,
1086 * this is ClientHello.Random + ServerHello.Random
1087 * (see Sect. 8.1 in RFC 5246).
1088 * - If the Extended Master Secret extension is used,
1089 * this is the transcript of the handshake so far.
1090 * (see Sect. 4 in RFC 7627). */
1091 unsigned char const *salt = handshake->randbytes;
1092 size_t salt_len = 64;
1093
1094#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001098
Hanno Becker35b23c72018-10-23 12:10:41 +01001099 lbl = "extended master secret";
1100 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001101 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1103 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001106 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1107 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001108 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001109 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001110 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001111#endif /* MBEDTLS_SHA512_C */
1112 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001113 }
1114 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001116 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001117
Hanno Becker35b23c72018-10-23 12:10:41 +01001118 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001119 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001120#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1121
Hanno Becker7d0a5692018-10-23 15:26:22 +01001122#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1123 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1124 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1125 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1126 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001127 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001128 /* Perform PSK-to-MS expansion in a single step. */
1129 psa_status_t status;
1130 psa_algorithm_t alg;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001131 psa_key_handle_t psk;
Janos Follathda6ac012019-08-16 13:47:29 +01001132 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +01001133 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001134
1135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1136
1137 psk = ssl->conf->psk_opaque;
1138 if( ssl->handshake->psk_opaque != 0 )
1139 psk = ssl->handshake->psk_opaque;
1140
Hanno Becker22bf1452019-04-05 11:21:08 +01001141 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001142 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1143 else
1144 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1145
Janos Follathda6ac012019-08-16 13:47:29 +01001146 status = psa_key_derivation( &derivation, psk, alg,
Hanno Becker7d0a5692018-10-23 15:26:22 +01001147 salt, salt_len,
1148 (unsigned char const *) lbl,
1149 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001150 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001151 if( status != PSA_SUCCESS )
1152 {
Janos Follathda6ac012019-08-16 13:47:29 +01001153 psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001154 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1155 }
1156
Janos Follathda6ac012019-08-16 13:47:29 +01001157 status = psa_key_derivation_output_bytes( &derivation,
Janos Follath6de99db2019-07-30 12:55:06 +01001158 session->master,
1159 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001160 if( status != PSA_SUCCESS )
1161 {
Janos Follathda6ac012019-08-16 13:47:29 +01001162 psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001163 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1164 }
1165
Janos Follathda6ac012019-08-16 13:47:29 +01001166 status = psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001167 if( status != PSA_SUCCESS )
1168 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001169 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001170 else
1171#endif
1172 {
1173 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1174 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001175 session->master,
1176 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001177 if( ret != 0 )
1178 {
1179 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1180 return( ret );
1181 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001182
Hanno Becker7d0a5692018-10-23 15:26:22 +01001183 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1184 handshake->premaster,
1185 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001186
Hanno Becker7d0a5692018-10-23 15:26:22 +01001187 mbedtls_platform_zeroize( handshake->premaster,
1188 sizeof(handshake->premaster) );
1189 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001191
1192 /*
1193 * Swap the client and server random values.
1194 */
Paul Bakker48916f92012-09-16 19:57:18 +00001195 memcpy( tmp, handshake->randbytes, 64 );
1196 memcpy( handshake->randbytes, tmp + 32, 32 );
1197 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001198 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
1200 /*
1201 * SSLv3:
1202 * key block =
1203 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1204 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1205 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1206 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1207 * ...
1208 *
1209 * TLSv1:
1210 * key block = PRF( master, "key expansion", randbytes )
1211 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001212 ret = handshake->tls_prf( session->master, 48, "key expansion",
1213 handshake->randbytes, 64, keyblk, 256 );
1214 if( ret != 0 )
1215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001217 return( ret );
1218 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1221 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1222 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1223 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1224 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 /*
1227 * Determine the appropriate key, IV and MAC length.
1228 */
Paul Bakker68884e32013-01-07 18:20:04 +01001229
Hanno Becker88aaf652017-12-27 08:17:40 +00001230 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001231
Hanno Becker8031d062018-01-03 15:32:31 +00001232#if defined(MBEDTLS_GCM_C) || \
1233 defined(MBEDTLS_CCM_C) || \
1234 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001236 cipher_info->mode == MBEDTLS_MODE_CCM ||
1237 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001239 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001240
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001241 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001242 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001243 transform->taglen =
1244 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001245
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001246 /* All modes haves 96-bit IVs;
1247 * GCM and CCM has 4 implicit and 8 explicit bytes
1248 * ChachaPoly has all 12 bytes implicit
1249 */
Paul Bakker68884e32013-01-07 18:20:04 +01001250 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001251 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1252 transform->fixed_ivlen = 12;
1253 else
1254 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001255
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001256 /* Minimum length of encrypted record */
1257 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001258 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001259 }
1260 else
Hanno Becker8031d062018-01-03 15:32:31 +00001261#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1262#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1263 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1264 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001265 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001266 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1268 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001271 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001272 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001274 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001275 mac_key_len = mbedtls_md_get_size( md_info );
1276 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001279 /*
1280 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1281 * (rfc 6066 page 13 or rfc 2104 section 4),
1282 * so we only need to adjust the length here.
1283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001287
1288#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1289 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001290 * HMAC implementation which also truncates the key
1291 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001292 mac_key_len = transform->maclen;
1293#endif
1294 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001296
1297 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001298 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001300 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001302 transform->minlen = transform->maclen;
1303 else
Paul Bakker68884e32013-01-07 18:20:04 +01001304 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001305 /*
1306 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001307 * 1. if EtM is in use: one block plus MAC
1308 * otherwise: * first multiple of blocklen greater than maclen
1309 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1312 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001313 {
1314 transform->minlen = transform->maclen
1315 + cipher_info->block_size;
1316 }
1317 else
1318#endif
1319 {
1320 transform->minlen = transform->maclen
1321 + cipher_info->block_size
1322 - transform->maclen % cipher_info->block_size;
1323 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1326 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1327 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001328 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001329 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001330#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1332 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1333 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001334 {
1335 transform->minlen += transform->ivlen;
1336 }
1337 else
1338#endif
1339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001341 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1342 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001343 }
Paul Bakker68884e32013-01-07 18:20:04 +01001344 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 }
Hanno Becker8031d062018-01-03 15:32:31 +00001346 else
1347#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1348 {
1349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1350 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1351 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
Hanno Becker88aaf652017-12-27 08:17:40 +00001353 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1354 (unsigned) keylen,
1355 (unsigned) transform->minlen,
1356 (unsigned) transform->ivlen,
1357 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
1359 /*
1360 * Finally setup the cipher contexts, IVs and MAC secrets.
1361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001363 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001365 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001366 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
Paul Bakker68884e32013-01-07 18:20:04 +01001368 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001369 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001371 /*
1372 * This is not used in TLS v1.1.
1373 */
Paul Bakker48916f92012-09-16 19:57:18 +00001374 iv_copy_len = ( transform->fixed_ivlen ) ?
1375 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001376 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1377 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001378 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 }
1380 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#endif /* MBEDTLS_SSL_CLI_C */
1382#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001383 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001384 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001385 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001386 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Hanno Becker81c7b182017-11-09 18:39:33 +00001388 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001389 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001391 /*
1392 * This is not used in TLS v1.1.
1393 */
Paul Bakker48916f92012-09-16 19:57:18 +00001394 iv_copy_len = ( transform->fixed_ivlen ) ?
1395 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001396 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1397 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001398 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001400 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001404 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1405 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Hanno Beckerd56ed242018-01-03 15:32:51 +00001408#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_SSL_PROTO_SSL3)
1410 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001411 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001412 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001415 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1416 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001417 }
1418
Hanno Becker81c7b182017-11-09 18:39:33 +00001419 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1420 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001421 }
1422 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1424#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1425 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1426 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001427 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001428 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1429 For AEAD-based ciphersuites, there is nothing to do here. */
1430 if( mac_key_len != 0 )
1431 {
1432 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1433 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1434 }
Paul Bakker68884e32013-01-07 18:20:04 +01001435 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001436 else
1437#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001440 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1441 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001442 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001443#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1446 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001447 {
1448 int ret = 0;
1449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001451
Hanno Becker88aaf652017-12-27 08:17:40 +00001452 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001453 transform->iv_enc, transform->iv_dec,
1454 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001455 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001456 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001459 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1460 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001461 }
1462 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001463#else
1464 ((void) mac_dec);
1465 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001467
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001468#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1469 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001470 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001471 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1472 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001473 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001474 iv_copy_len );
1475 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001476
1477 if( ssl->conf->f_export_keys_ext != NULL )
1478 {
1479 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1480 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001481 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001482 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001483 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001484 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001485 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001486 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001487#endif
1488
Hanno Beckerf704bef2018-11-16 15:21:18 +00001489#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001490
1491 /* Only use PSA-based ciphers for TLS-1.2.
1492 * That's relevant at least for TLS-1.0, where
1493 * we assume that mbedtls_cipher_crypt() updates
1494 * the structure field for the IV, which the PSA-based
1495 * implementation currently doesn't. */
1496#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1497 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001498 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001499 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001500 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001501 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1502 {
1503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001504 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001505 }
1506
1507 if( ret == 0 )
1508 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001510 psa_fallthrough = 0;
1511 }
1512 else
1513 {
1514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1515 psa_fallthrough = 1;
1516 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001517 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001518 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001519 psa_fallthrough = 1;
1520#else
1521 psa_fallthrough = 1;
1522#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001523
Hanno Beckercb1cc802018-11-17 22:27:38 +00001524 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001525#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001526 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001527 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001529 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001530 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001531 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001532
Hanno Beckerf704bef2018-11-16 15:21:18 +00001533#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001534 /* Only use PSA-based ciphers for TLS-1.2.
1535 * That's relevant at least for TLS-1.0, where
1536 * we assume that mbedtls_cipher_crypt() updates
1537 * the structure field for the IV, which the PSA-based
1538 * implementation currently doesn't. */
1539#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1540 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001541 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001542 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001543 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001544 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1545 {
1546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001547 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001548 }
1549
1550 if( ret == 0 )
1551 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001553 psa_fallthrough = 0;
1554 }
1555 else
1556 {
1557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1558 psa_fallthrough = 1;
1559 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001560 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001561 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001562 psa_fallthrough = 1;
1563#else
1564 psa_fallthrough = 1;
1565#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001566
Hanno Beckercb1cc802018-11-17 22:27:38 +00001567 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001568#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001569 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001570 cipher_info ) ) != 0 )
1571 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001572 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001573 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001577 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001581 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001582 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001585 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001589 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001590 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#if defined(MBEDTLS_CIPHER_MODE_CBC)
1593 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1596 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001599 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001600 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1603 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001606 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001613 // Initialize compression
1614 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001616 {
Paul Bakker16770332013-10-11 09:59:44 +02001617 if( ssl->compress_buf == NULL )
1618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001620 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001621 if( ssl->compress_buf == NULL )
1622 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001624 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001625 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1626 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001627 }
1628 }
1629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001631
Paul Bakker48916f92012-09-16 19:57:18 +00001632 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1633 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001634
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001635 if( deflateInit( &transform->ctx_deflate,
1636 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001637 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001640 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1641 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001642 }
1643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001647end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001648 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1649 mbedtls_platform_zeroize( handshake->randbytes,
1650 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001651 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001652}
1653
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001654/*
1655 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
1656 *
1657 * Inputs:
1658 * - SSL/TLS minor version
1659 * - hash associated with the ciphersuite (only used by TLS 1.2)
1660 *
1661 * Ouputs:
1662 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1663 */
1664static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1665 int minor_ver,
1666 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001667{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001668#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1669 (void) hash;
1670#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001671
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001672#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001673 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001674 {
1675 handshake->tls_prf = ssl3_prf;
1676 handshake->calc_verify = ssl_calc_verify_ssl;
1677 handshake->calc_finished = ssl_calc_finished_ssl;
1678 }
1679 else
1680#endif
1681#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001682 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001683 {
1684 handshake->tls_prf = tls1_prf;
1685 handshake->calc_verify = ssl_calc_verify_tls;
1686 handshake->calc_finished = ssl_calc_finished_tls;
1687 }
1688 else
1689#endif
1690#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1691#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001692 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1693 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001694 {
1695 handshake->tls_prf = tls_prf_sha384;
1696 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1697 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1698 }
1699 else
1700#endif
1701#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001702 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001703 {
1704 handshake->tls_prf = tls_prf_sha256;
1705 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1706 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1707 }
1708 else
1709#endif
1710#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1711 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1713 }
1714
1715 return( 0 );
1716}
1717
1718
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001719int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1720{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001721 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001722 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1723 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001724
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001725 ret = ssl_set_handshake_prfs( ssl->handshake,
1726 ssl->minor_ver,
1727 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001728 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001729 {
1730 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001731 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001732 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001733
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001734 return( ssl_populate_transform( ssl ) );
1735}
1736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_SSL_PROTO_SSL3)
1738void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001739{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001740 mbedtls_md5_context md5;
1741 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001742 unsigned char pad_1[48];
1743 unsigned char pad_2[48];
1744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001746
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001747 mbedtls_md5_init( &md5 );
1748 mbedtls_sha1_init( &sha1 );
1749
1750 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1751 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001752
Paul Bakker380da532012-04-18 16:10:25 +00001753 memset( pad_1, 0x36, 48 );
1754 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001756 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1757 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1758 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001759
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001760 mbedtls_md5_starts_ret( &md5 );
1761 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1762 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1763 mbedtls_md5_update_ret( &md5, hash, 16 );
1764 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001766 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1767 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1768 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001769
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001770 mbedtls_sha1_starts_ret( &sha1 );
1771 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1772 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1773 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1774 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001778
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001779 mbedtls_md5_free( &md5 );
1780 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001781
Paul Bakker380da532012-04-18 16:10:25 +00001782 return;
1783}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1787void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001788{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001789 mbedtls_md5_context md5;
1790 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001793
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001794 mbedtls_md5_init( &md5 );
1795 mbedtls_sha1_init( &sha1 );
1796
1797 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1798 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001799
Andrzej Kurekeb342242019-01-29 09:14:33 -05001800 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001801 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001805
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001806 mbedtls_md5_free( &md5 );
1807 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001808
Paul Bakker380da532012-04-18 16:10:25 +00001809 return;
1810}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1814#if defined(MBEDTLS_SHA256_C)
1815void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001816{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001817#if defined(MBEDTLS_USE_PSA_CRYPTO)
1818 size_t hash_size;
1819 psa_status_t status;
1820 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1821
1822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1823 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1824 if( status != PSA_SUCCESS )
1825 {
1826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1827 return;
1828 }
1829
1830 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1831 if( status != PSA_SUCCESS )
1832 {
1833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1834 return;
1835 }
1836 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1838#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001839 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001840
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001841 mbedtls_sha256_init( &sha256 );
1842
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001844
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001845 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001846 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001850
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001851 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001852#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001853 return;
1854}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857#if defined(MBEDTLS_SHA512_C)
1858void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001859{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001860#if defined(MBEDTLS_USE_PSA_CRYPTO)
1861 size_t hash_size;
1862 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001863 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001864
1865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001866 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001867 if( status != PSA_SUCCESS )
1868 {
1869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1870 return;
1871 }
1872
Andrzej Kurek972fba52019-01-30 03:29:12 -05001873 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001874 if( status != PSA_SUCCESS )
1875 {
1876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1877 return;
1878 }
1879 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1881#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001882 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001883
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001884 mbedtls_sha512_init( &sha512 );
1885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001887
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001888 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001889 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001893
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001894 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001895#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 return;
1897}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#endif /* MBEDTLS_SHA512_C */
1899#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1902int 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 +02001903{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001904 unsigned char *p = ssl->handshake->premaster;
1905 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001906 const unsigned char *psk = ssl->conf->psk;
1907 size_t psk_len = ssl->conf->psk_len;
1908
1909 /* If the psk callback was called, use its result */
1910 if( ssl->handshake->psk != NULL )
1911 {
1912 psk = ssl->handshake->psk;
1913 psk_len = ssl->handshake->psk_len;
1914 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001915
1916 /*
1917 * PMS = struct {
1918 * opaque other_secret<0..2^16-1>;
1919 * opaque psk<0..2^16-1>;
1920 * };
1921 * with "other_secret" depending on the particular key exchange
1922 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1924 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001925 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001926 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001928
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001929 *(p++) = (unsigned char)( psk_len >> 8 );
1930 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001931
1932 if( end < p || (size_t)( end - p ) < psk_len )
1933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1934
1935 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001936 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001937 }
1938 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1940#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1941 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001942 {
1943 /*
1944 * other_secret already set by the ClientKeyExchange message,
1945 * and is 48 bytes long
1946 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001947 if( end - p < 2 )
1948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1949
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001950 *p++ = 0;
1951 *p++ = 48;
1952 p += 48;
1953 }
1954 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1956#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1957 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001958 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001959 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001960 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001961
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001962 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001964 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001965 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001968 return( ret );
1969 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001970 *(p++) = (unsigned char)( len >> 8 );
1971 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001972 p += len;
1973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001975 }
1976 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1978#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1979 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001980 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001981 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001982 size_t zlen;
1983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001985 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001986 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001989 return( ret );
1990 }
1991
1992 *(p++) = (unsigned char)( zlen >> 8 );
1993 *(p++) = (unsigned char)( zlen );
1994 p += zlen;
1995
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001996 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1997 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001998 }
1999 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2003 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002004 }
2005
2006 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002007 if( end - p < 2 )
2008 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002009
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002010 *(p++) = (unsigned char)( psk_len >> 8 );
2011 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002012
2013 if( end < p || (size_t)( end - p ) < psk_len )
2014 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2015
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002016 memcpy( p, psk, psk_len );
2017 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002018
2019 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2020
2021 return( 0 );
2022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002026/*
2027 * SSLv3.0 MAC functions
2028 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002029#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002030static void ssl_mac( mbedtls_md_context_t *md_ctx,
2031 const unsigned char *secret,
2032 const unsigned char *buf, size_t len,
2033 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002034 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002035{
2036 unsigned char header[11];
2037 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002038 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2040 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002041
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002042 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002044 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002045 else
Paul Bakker68884e32013-01-07 18:20:04 +01002046 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002047
2048 memcpy( header, ctr, 8 );
2049 header[ 8] = (unsigned char) type;
2050 header[ 9] = (unsigned char)( len >> 8 );
2051 header[10] = (unsigned char)( len );
2052
Paul Bakker68884e32013-01-07 18:20:04 +01002053 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 mbedtls_md_starts( md_ctx );
2055 mbedtls_md_update( md_ctx, secret, md_size );
2056 mbedtls_md_update( md_ctx, padding, padlen );
2057 mbedtls_md_update( md_ctx, header, 11 );
2058 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002059 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002060
Paul Bakker68884e32013-01-07 18:20:04 +01002061 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 mbedtls_md_starts( md_ctx );
2063 mbedtls_md_update( md_ctx, secret, md_size );
2064 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002065 mbedtls_md_update( md_ctx, out, md_size );
2066 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002069
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002070/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002071 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002072#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002073 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2074 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2075 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2076/* This function makes sure every byte in the memory region is accessed
2077 * (in ascending addresses order) */
2078static void ssl_read_memory( unsigned char *p, size_t len )
2079{
2080 unsigned char acc = 0;
2081 volatile unsigned char force;
2082
2083 for( ; len != 0; p++, len-- )
2084 acc ^= *p;
2085
2086 force = acc;
2087 (void) force;
2088}
2089#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2090
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002091/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002093 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002094
Hanno Beckera0e20d02019-05-15 14:03:01 +01002095#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002096/* This functions transforms a DTLS plaintext fragment and a record content
2097 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002098 *
2099 * struct {
2100 * opaque content[DTLSPlaintext.length];
2101 * ContentType real_type;
2102 * uint8 zeros[length_of_padding];
2103 * } DTLSInnerPlaintext;
2104 *
2105 * Input:
2106 * - `content`: The beginning of the buffer holding the
2107 * plaintext to be wrapped.
2108 * - `*content_size`: The length of the plaintext in Bytes.
2109 * - `max_len`: The number of Bytes available starting from
2110 * `content`. This must be `>= *content_size`.
2111 * - `rec_type`: The desired record content type.
2112 *
2113 * Output:
2114 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2115 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2116 *
2117 * Returns:
2118 * - `0` on success.
2119 * - A negative error code if `max_len` didn't offer enough space
2120 * for the expansion.
2121 */
2122static int ssl_cid_build_inner_plaintext( unsigned char *content,
2123 size_t *content_size,
2124 size_t remaining,
2125 uint8_t rec_type )
2126{
2127 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002128 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2129 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2130 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002131
2132 /* Write real content type */
2133 if( remaining == 0 )
2134 return( -1 );
2135 content[ len ] = rec_type;
2136 len++;
2137 remaining--;
2138
2139 if( remaining < pad )
2140 return( -1 );
2141 memset( content + len, 0, pad );
2142 len += pad;
2143 remaining -= pad;
2144
2145 *content_size = len;
2146 return( 0 );
2147}
2148
Hanno Becker07dc97d2019-05-20 15:08:01 +01002149/* This function parses a DTLSInnerPlaintext structure.
2150 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002151static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2152 size_t *content_size,
2153 uint8_t *rec_type )
2154{
2155 size_t remaining = *content_size;
2156
2157 /* Determine length of padding by skipping zeroes from the back. */
2158 do
2159 {
2160 if( remaining == 0 )
2161 return( -1 );
2162 remaining--;
2163 } while( content[ remaining ] == 0 );
2164
2165 *content_size = remaining;
2166 *rec_type = content[ remaining ];
2167
2168 return( 0 );
2169}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002170#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002171
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002172/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002173 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002174static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002175 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002176 mbedtls_record *rec )
2177{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002178 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002179 *
2180 * additional_data = seq_num + TLSCompressed.type +
2181 * TLSCompressed.version + TLSCompressed.length;
2182 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002183 * For the CID extension, this is extended as follows
2184 * (quoting draft-ietf-tls-dtls-connection-id-05,
2185 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002186 *
2187 * additional_data = seq_num + DTLSPlaintext.type +
2188 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002189 * cid +
2190 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002191 * length_of_DTLSInnerPlaintext;
2192 */
2193
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002194 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2195 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002196 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002197
Hanno Beckera0e20d02019-05-15 14:03:01 +01002198#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002199 if( rec->cid_len != 0 )
2200 {
2201 memcpy( add_data + 11, rec->cid, rec->cid_len );
2202 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2203 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2204 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2205 *add_data_len = 13 + 1 + rec->cid_len;
2206 }
2207 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002209 {
2210 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2211 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2212 *add_data_len = 13;
2213 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002214}
2215
Hanno Beckera18d1322018-01-03 14:27:32 +00002216int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2217 mbedtls_ssl_transform *transform,
2218 mbedtls_record *rec,
2219 int (*f_rng)(void *, unsigned char *, size_t),
2220 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002223 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002224 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002225 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002226 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002227 size_t post_avail;
2228
2229 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002230#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002231 ((void) ssl);
2232#endif
2233
2234 /* The PRNG is used for dynamic IV generation that's used
2235 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2236#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2237 ( defined(MBEDTLS_AES_C) || \
2238 defined(MBEDTLS_ARIA_C) || \
2239 defined(MBEDTLS_CAMELLIA_C) ) && \
2240 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2241 ((void) f_rng);
2242 ((void) p_rng);
2243#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002246
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002247 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002248 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2251 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002252 if( rec == NULL
2253 || rec->buf == NULL
2254 || rec->buf_len < rec->data_offset
2255 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002256#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002257 || rec->cid_len != 0
2258#endif
2259 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002260 {
2261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002263 }
2264
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002265 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002266 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002268 data, rec->data_len );
2269
2270 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2271
2272 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2273 {
2274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2275 (unsigned) rec->data_len,
2276 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2278 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002279
Hanno Beckera0e20d02019-05-15 14:03:01 +01002280#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002281 /*
2282 * Add CID information
2283 */
2284 rec->cid_len = transform->out_cid_len;
2285 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2286 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002287
2288 if( rec->cid_len != 0 )
2289 {
2290 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002291 * Wrap plaintext into DTLSInnerPlaintext structure.
2292 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002293 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002294 * Note that this changes `rec->data_len`, and hence
2295 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002296 */
2297 if( ssl_cid_build_inner_plaintext( data,
2298 &rec->data_len,
2299 post_avail,
2300 rec->type ) != 0 )
2301 {
2302 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2303 }
2304
2305 rec->type = MBEDTLS_SSL_MSG_CID;
2306 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002307#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002308
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002309 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2310
Paul Bakker5121ce52009-01-03 21:22:43 +00002311 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002312 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 */
Hanno Becker52344c22018-01-03 15:24:20 +00002314#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 if( mode == MBEDTLS_MODE_STREAM ||
2316 ( mode == MBEDTLS_MODE_CBC
2317#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002318 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002319#endif
2320 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002322 if( post_avail < transform->maclen )
2323 {
2324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2325 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2326 }
2327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002329 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002330 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002331 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002332 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2333 data, rec->data_len, rec->ctr, rec->type, mac );
2334 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002335 }
2336 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002337#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2339 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002340 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002341 {
Hanno Becker992b6872017-11-09 18:57:39 +00002342 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2343
Hanno Beckercab87e62019-04-29 13:52:53 +01002344 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002345
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002346 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002347 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002348 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2349 data, rec->data_len );
2350 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2351 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2352
2353 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002354 }
2355 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002356#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2359 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002360 }
2361
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002362 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2363 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002364
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002365 rec->data_len += transform->maclen;
2366 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002367 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002368 }
Hanno Becker52344c22018-01-03 15:24:20 +00002369#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002370
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002371 /*
2372 * Encrypt
2373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2375 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002377 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002378 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002380 "including %d bytes of padding",
2381 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002383 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2384 transform->iv_enc, transform->ivlen,
2385 data, rec->data_len,
2386 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002389 return( ret );
2390 }
2391
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002392 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2395 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002396 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 }
Paul Bakker68884e32013-01-07 18:20:04 +01002398 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002400
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002401#if defined(MBEDTLS_GCM_C) || \
2402 defined(MBEDTLS_CCM_C) || \
2403 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002405 mode == MBEDTLS_MODE_CCM ||
2406 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002407 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002408 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002409 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002410 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002411
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002412 /* Check that there's space for both the authentication tag
2413 * and the explicit IV before and after the record content. */
2414 if( post_avail < transform->taglen ||
2415 rec->data_offset < explicit_iv_len )
2416 {
2417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2418 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2419 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002420
Paul Bakker68884e32013-01-07 18:20:04 +01002421 /*
2422 * Generate IV
2423 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002424 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2425 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002426 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002427 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002428 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2429 explicit_iv_len );
2430 /* Prefix record content with explicit IV. */
2431 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002432 }
2433 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2434 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002435 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002436 unsigned char i;
2437
2438 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2439
2440 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002441 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002442 }
2443 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002444 {
2445 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2447 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002448 }
2449
Hanno Beckercab87e62019-04-29 13:52:53 +01002450 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002451
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002452 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2453 iv, transform->ivlen );
2454 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002455 data - explicit_iv_len, explicit_iv_len );
2456 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002457 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002459 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002460 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002461
Paul Bakker68884e32013-01-07 18:20:04 +01002462 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002463 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002464 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002465
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002466 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002467 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002468 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002469 data, rec->data_len, /* source */
2470 data, &rec->data_len, /* destination */
2471 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002474 return( ret );
2475 }
2476
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002477 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2478 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002479
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002480 rec->data_len += transform->taglen + explicit_iv_len;
2481 rec->data_offset -= explicit_iv_len;
2482 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002483 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002484 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2487#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002488 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002491 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002492 size_t padlen, i;
2493 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002494
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002495 /* Currently we're always using minimal padding
2496 * (up to 255 bytes would be allowed). */
2497 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2498 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 padlen = 0;
2500
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002501 /* Check there's enough space in the buffer for the padding. */
2502 if( post_avail < padlen + 1 )
2503 {
2504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2505 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2506 }
2507
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002509 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002511 rec->data_len += padlen + 1;
2512 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002515 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002516 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2517 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002518 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002519 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002520 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002521 if( f_rng == NULL )
2522 {
2523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2524 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2525 }
2526
2527 if( rec->data_offset < transform->ivlen )
2528 {
2529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2530 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2531 }
2532
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002533 /*
2534 * Generate IV
2535 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002536 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002537 if( ret != 0 )
2538 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002539
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002540 memcpy( data - transform->ivlen, transform->iv_enc,
2541 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002542
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002543 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002547 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002548 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002549 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002551 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2552 transform->iv_enc,
2553 transform->ivlen,
2554 data, rec->data_len,
2555 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002558 return( ret );
2559 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002560
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002561 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002568 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002569 {
2570 /*
2571 * Save IV in SSL3 and TLS1
2572 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002573 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2574 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002576 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002577#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002578 {
2579 data -= transform->ivlen;
2580 rec->data_offset -= transform->ivlen;
2581 rec->data_len += transform->ivlen;
2582 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002585 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002586 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002587 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2588
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002589 /*
2590 * MAC(MAC_write_key, seq_num +
2591 * TLSCipherText.type +
2592 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002593 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002594 * IV + // except for TLS 1.0
2595 * ENC(content + padding + padding_length));
2596 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002597
2598 if( post_avail < transform->maclen)
2599 {
2600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2601 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2602 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002603
Hanno Beckercab87e62019-04-29 13:52:53 +01002604 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002607 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002608 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002609
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002610 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002611 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002612 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2613 data, rec->data_len );
2614 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2615 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002616
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002617 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002618
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002619 rec->data_len += transform->maclen;
2620 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002621 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002622 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002625 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002627 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2630 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002631 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002633 /* Make extra sure authentication was performed, exactly once */
2634 if( auth_done != 1 )
2635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2637 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002638 }
2639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
2642 return( 0 );
2643}
2644
Hanno Becker605949f2019-07-12 08:23:59 +01002645int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002646 mbedtls_ssl_transform *transform,
2647 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002648{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002649 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002651 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002652#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002653 size_t padlen = 0, correct = 1;
2654#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002655 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002656 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002657 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002658
Hanno Beckera18d1322018-01-03 14:27:32 +00002659#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002660 ((void) ssl);
2661#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002664 if( rec == NULL ||
2665 rec->buf == NULL ||
2666 rec->buf_len < rec->data_offset ||
2667 rec->buf_len - rec->data_offset < rec->data_len )
2668 {
2669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002671 }
2672
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002673 data = rec->buf + rec->data_offset;
2674 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Hanno Beckera0e20d02019-05-15 14:03:01 +01002676#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002677 /*
2678 * Match record's CID with incoming CID.
2679 */
Hanno Becker938489a2019-05-08 13:02:22 +01002680 if( rec->cid_len != transform->in_cid_len ||
2681 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2682 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002683 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002684 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002685#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2688 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002689 {
2690 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002691 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2692 transform->iv_dec,
2693 transform->ivlen,
2694 data, rec->data_len,
2695 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002698 return( ret );
2699 }
2700
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002701 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2704 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002705 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 }
Paul Bakker68884e32013-01-07 18:20:04 +01002707 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002709#if defined(MBEDTLS_GCM_C) || \
2710 defined(MBEDTLS_CCM_C) || \
2711 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002713 mode == MBEDTLS_MODE_CCM ||
2714 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002715 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002716 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002717 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002718
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002719 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002720 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002721 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002722
2723 /* Check that there's enough space for the explicit IV
2724 * (at the beginning of the record) and the MAC (at the
2725 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002726 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002729 "+ taglen (%d)", rec->data_len,
2730 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002732 }
Paul Bakker68884e32013-01-07 18:20:04 +01002733
Hanno Beckerd96a6522019-07-10 13:55:25 +01002734#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002735 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2736 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002737 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002738
Hanno Beckerd96a6522019-07-10 13:55:25 +01002739 /* Fixed */
2740 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2741 /* Explicit */
2742 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002743 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002744 else
2745#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2746#if defined(MBEDTLS_CHACHAPOLY_C)
2747 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002748 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002749 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002750 unsigned char i;
2751
2752 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2753
2754 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002755 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002756 }
2757 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002758#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002759 {
2760 /* Reminder if we ever add an AEAD mode with a different size */
2761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2762 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2763 }
2764
Hanno Beckerd96a6522019-07-10 13:55:25 +01002765 /* Group changes to data, data_len, and add_data, because
2766 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002767 data += explicit_iv_len;
2768 rec->data_offset += explicit_iv_len;
2769 rec->data_len -= explicit_iv_len + transform->taglen;
2770
Hanno Beckercab87e62019-04-29 13:52:53 +01002771 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002772 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002773 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002774
Hanno Beckerd96a6522019-07-10 13:55:25 +01002775 /* Because of the check above, we know that there are
2776 * explicit_iv_len Bytes preceeding data, and taglen
2777 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002778 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002779 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002780
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002781 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002782 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002783 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002784
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002785 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002786 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002787 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002788 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2789 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002790 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002791 data, rec->data_len,
2792 data, &olen,
2793 data + rec->data_len,
2794 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2799 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002800
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002801 return( ret );
2802 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002803 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002804
Hanno Beckerd96a6522019-07-10 13:55:25 +01002805 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002806 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2809 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002810 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002811 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002812 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2814#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002815 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002818 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002819
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 /*
Paul Bakker45829992013-01-03 14:52:21 +01002821 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002824 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2825 {
2826 /* The ciphertext is prefixed with the CBC IV. */
2827 minlen += transform->ivlen;
2828 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002829#endif
Paul Bakker45829992013-01-03 14:52:21 +01002830
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002831 /* Size considerations:
2832 *
2833 * - The CBC cipher text must not be empty and hence
2834 * at least of size transform->ivlen.
2835 *
2836 * Together with the potential IV-prefix, this explains
2837 * the first of the two checks below.
2838 *
2839 * - The record must contain a MAC, either in plain or
2840 * encrypted, depending on whether Encrypt-then-MAC
2841 * is used or not.
2842 * - If it is, the message contains the IV-prefix,
2843 * the CBC ciphertext, and the MAC.
2844 * - If it is not, the padded plaintext, and hence
2845 * the CBC ciphertext, has at least length maclen + 1
2846 * because there is at least the padding length byte.
2847 *
2848 * As the CBC ciphertext is not empty, both cases give the
2849 * lower bound minlen + maclen + 1 on the record size, which
2850 * we test for in the second check below.
2851 */
2852 if( rec->data_len < minlen + transform->ivlen ||
2853 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002856 "+ 1 ) ( + expl IV )", rec->data_len,
2857 transform->ivlen,
2858 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002860 }
2861
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002862 /*
2863 * Authenticate before decrypt if enabled
2864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002866 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002867 {
Hanno Becker992b6872017-11-09 18:57:39 +00002868 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002871
Hanno Beckerd96a6522019-07-10 13:55:25 +01002872 /* Update data_len in tandem with add_data.
2873 *
2874 * The subtraction is safe because of the previous check
2875 * data_len >= minlen + maclen + 1.
2876 *
2877 * Afterwards, we know that data + data_len is followed by at
2878 * least maclen Bytes, which justifies the call to
2879 * mbedtls_ssl_safer_memcmp() below.
2880 *
2881 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002882 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002883 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002884
Hanno Beckerd96a6522019-07-10 13:55:25 +01002885 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002886 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2887 add_data_len );
2888 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2889 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2891 data, rec->data_len );
2892 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2893 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002894
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002895 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2896 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002897 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002898 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002899
Hanno Beckerd96a6522019-07-10 13:55:25 +01002900 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002901 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2902 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002906 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002907 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002908 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002910
2911 /*
2912 * Check length sanity
2913 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002914
2915 /* We know from above that data_len > minlen >= 0,
2916 * so the following check in particular implies that
2917 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002918 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002921 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002923 }
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002926 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002927 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002928 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002929 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002930 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002931 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002932 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002933
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002934 data += transform->ivlen;
2935 rec->data_offset += transform->ivlen;
2936 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002937 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002939
Hanno Beckerd96a6522019-07-10 13:55:25 +01002940 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2941
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002942 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2943 transform->iv_dec, transform->ivlen,
2944 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002947 return( ret );
2948 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002949
Hanno Beckerd96a6522019-07-10 13:55:25 +01002950 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002951 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002955 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002958 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002959 {
2960 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002961 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
2962 * records is equivalent to CBC decryption of the concatenation
2963 * of the records; in other words, IVs are maintained across
2964 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02002965 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002966 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2967 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002968 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002969#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002970
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002971 /* Safe since data_len >= minlen + maclen + 1, so after having
2972 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01002973 * data_len > 0 (because of data_len % ivlen == 0, it's actually
2974 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002975 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002976
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002977 if( auth_done == 1 )
2978 {
2979 correct *= ( rec->data_len >= padlen + 1 );
2980 padlen *= ( rec->data_len >= padlen + 1 );
2981 }
2982 else
Paul Bakker45829992013-01-03 14:52:21 +01002983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002985 if( rec->data_len < transform->maclen + padlen + 1 )
2986 {
2987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2988 rec->data_len,
2989 transform->maclen,
2990 padlen + 1 ) );
2991 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002992#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002993
2994 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2995 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002996 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002997
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002998 padlen++;
2999
3000 /* Regardless of the validity of the padding,
3001 * we have data_len >= padlen here. */
3002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003004 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003005 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003006 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008#if defined(MBEDTLS_SSL_DEBUG_ALL)
3009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003010 "should be no more than %d",
3011 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003012#endif
Paul Bakker45829992013-01-03 14:52:21 +01003013 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003014 }
3015 }
3016 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3018#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3019 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003020 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003022 /* The padding check involves a series of up to 256
3023 * consecutive memory reads at the end of the record
3024 * plaintext buffer. In order to hide the length and
3025 * validity of the padding, always perform exactly
3026 * `min(256,plaintext_len)` reads (but take into account
3027 * only the last `padlen` bytes for the padding check). */
3028 size_t pad_count = 0;
3029 size_t real_count = 0;
3030 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003031
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003032 /* Index of first padding byte; it has been ensured above
3033 * that the subtraction is safe. */
3034 size_t const padding_idx = rec->data_len - padlen;
3035 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3036 size_t const start_idx = rec->data_len - num_checks;
3037 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003038
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003039 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003040 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003041 real_count |= ( idx >= padding_idx );
3042 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003043 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003044 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003047 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003049#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003050 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003052 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3054 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003058 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003059
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003060 /* If the padding was found to be invalid, padlen == 0
3061 * and the subtraction is safe. If the padding was found valid,
3062 * padlen hasn't been changed and the previous assertion
3063 * data_len >= padlen still holds. */
3064 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003065 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003066 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003068 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003072 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003073
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003074#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003076 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003077#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003078
3079 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003080 * Authenticate if not done yet.
3081 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003082 */
Hanno Becker52344c22018-01-03 15:24:20 +00003083#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003084 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003085 {
Hanno Becker992b6872017-11-09 18:57:39 +00003086 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003087
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003088 /* If the initial value of padlen was such that
3089 * data_len < maclen + padlen + 1, then padlen
3090 * got reset to 1, and the initial check
3091 * data_len >= minlen + maclen + 1
3092 * guarantees that at this point we still
3093 * have at least data_len >= maclen.
3094 *
3095 * If the initial value of padlen was such that
3096 * data_len >= maclen + padlen + 1, then we have
3097 * subtracted either padlen + 1 (if the padding was correct)
3098 * or 0 (if the padding was incorrect) since then,
3099 * hence data_len >= maclen in any case.
3100 */
3101 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003102 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003105 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003106 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003107 ssl_mac( &transform->md_ctx_dec,
3108 transform->mac_dec,
3109 data, rec->data_len,
3110 rec->ctr, rec->type,
3111 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003112 }
3113 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3115#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3116 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003117 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003118 {
3119 /*
3120 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003121 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003122 *
3123 * Known timing attacks:
3124 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3125 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003126 * To compensate for different timings for the MAC calculation
3127 * depending on how much padding was removed (which is determined
3128 * by padlen), process extra_run more blocks through the hash
3129 * function.
3130 *
3131 * The formula in the paper is
3132 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3133 * where L1 is the size of the header plus the decrypted message
3134 * plus CBC padding and L2 is the size of the header plus the
3135 * decrypted message. This is for an underlying hash function
3136 * with 64-byte blocks.
3137 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3138 * correctly. We round down instead of up, so -56 is the correct
3139 * value for our calculations instead of -55.
3140 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003141 * Repeat the formula rather than defining a block_size variable.
3142 * This avoids requiring division by a variable at runtime
3143 * (which would be marginally less efficient and would require
3144 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003145 */
3146 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003147 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003148
3149 /*
3150 * The next two sizes are the minimum and maximum values of
3151 * in_msglen over all padlen values.
3152 *
3153 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003154 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003155 *
3156 * Note that max_len + maclen is never more than the buffer
3157 * length, as we previously did in_msglen -= maclen too.
3158 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003159 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003160 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3161
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003162 memset( tmp, 0, sizeof( tmp ) );
3163
3164 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003165 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003166#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3167 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003168 case MBEDTLS_MD_MD5:
3169 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003170 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003171 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003172 extra_run =
3173 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3174 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003175 break;
3176#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003177#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003178 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003179 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003180 extra_run =
3181 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3182 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003183 break;
3184#endif
3185 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003187 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3188 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003189
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003190 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003191
Hanno Beckercab87e62019-04-29 13:52:53 +01003192 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3193 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003194 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3195 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003196 /* Make sure we access everything even when padlen > 0. This
3197 * makes the synchronisation requirements for just-in-time
3198 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003199 ssl_read_memory( data + rec->data_len, padlen );
3200 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003201
3202 /* Call mbedtls_md_process at least once due to cache attacks
3203 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003204 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003205 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003206
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003207 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003208
3209 /* Make sure we access all the memory that could contain the MAC,
3210 * before we check it in the next code block. This makes the
3211 * synchronisation requirements for just-in-time Prime+Probe
3212 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003213 ssl_read_memory( data + min_len,
3214 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003215 }
3216 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3218 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3221 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003222 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003223
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003224#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003225 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3226 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003227#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003228
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003229 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3230 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232#if defined(MBEDTLS_SSL_DEBUG_ALL)
3233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003234#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003235 correct = 0;
3236 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003237 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003238 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003239
3240 /*
3241 * Finally check the correct flag
3242 */
3243 if( correct == 0 )
3244 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003245#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003246
3247 /* Make extra sure authentication was performed, exactly once */
3248 if( auth_done != 1 )
3249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3251 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003252 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
Hanno Beckera0e20d02019-05-15 14:03:01 +01003254#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003255 if( rec->cid_len != 0 )
3256 {
3257 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3258 &rec->type );
3259 if( ret != 0 )
3260 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3261 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003262#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003265
3266 return( 0 );
3267}
3268
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003269#undef MAC_NONE
3270#undef MAC_PLAINTEXT
3271#undef MAC_CIPHERTEXT
3272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003274/*
3275 * Compression/decompression functions
3276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003278{
3279 int ret;
3280 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003281 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003282 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003283 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003286
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003287 if( len_pre == 0 )
3288 return( 0 );
3289
Paul Bakker2770fbd2012-07-03 13:30:23 +00003290 memcpy( msg_pre, ssl->out_msg, len_pre );
3291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003293 ssl->out_msglen ) );
3294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003296 ssl->out_msg, ssl->out_msglen );
3297
Paul Bakker48916f92012-09-16 19:57:18 +00003298 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3299 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3300 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003301 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003302
Paul Bakker48916f92012-09-16 19:57:18 +00003303 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003304 if( ret != Z_OK )
3305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3307 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003308 }
3309
Angus Grattond8213d02016-05-25 20:56:48 +10003310 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003311 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003314 ssl->out_msglen ) );
3315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003317 ssl->out_msg, ssl->out_msglen );
3318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003320
3321 return( 0 );
3322}
3323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003325{
3326 int ret;
3327 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003328 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003329 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003330 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003331
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
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003334 if( len_pre == 0 )
3335 return( 0 );
3336
Paul Bakker2770fbd2012-07-03 13:30:23 +00003337 memcpy( msg_pre, ssl->in_msg, len_pre );
3338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003340 ssl->in_msglen ) );
3341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003343 ssl->in_msg, ssl->in_msglen );
3344
Paul Bakker48916f92012-09-16 19:57:18 +00003345 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3346 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3347 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003348 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003349 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003350
Paul Bakker48916f92012-09-16 19:57:18 +00003351 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003352 if( ret != Z_OK )
3353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3355 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003356 }
3357
Angus Grattond8213d02016-05-25 20:56:48 +10003358 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003359 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003362 ssl->in_msglen ) );
3363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003365 ssl->in_msg, ssl->in_msglen );
3366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003368
3369 return( 0 );
3370}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3374static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376#if defined(MBEDTLS_SSL_PROTO_DTLS)
3377static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003378{
3379 /* If renegotiation is not enforced, retransmit until we would reach max
3380 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003381 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003382 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003383 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003384 unsigned char doublings = 1;
3385
3386 while( ratio != 0 )
3387 {
3388 ++doublings;
3389 ratio >>= 1;
3390 }
3391
3392 if( ++ssl->renego_records_seen > doublings )
3393 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003395 return( 0 );
3396 }
3397 }
3398
3399 return( ssl_write_hello_request( ssl ) );
3400}
3401#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003403
Paul Bakker5121ce52009-01-03 21:22:43 +00003404/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003405 * Fill the input message buffer by appending data to it.
3406 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003407 *
3408 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3409 * available (from this read and/or a previous one). Otherwise, an error code
3410 * is returned (possibly EOF or WANT_READ).
3411 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003412 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3413 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3414 * since we always read a whole datagram at once.
3415 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003416 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003417 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003420{
Paul Bakker23986e52011-04-24 08:57:21 +00003421 int ret;
3422 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003425
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003426 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003429 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003431 }
3432
Angus Grattond8213d02016-05-25 20:56:48 +10003433 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003437 }
3438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003440 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003442 uint32_t timeout;
3443
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003444 /* Just to be sure */
3445 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3446 {
3447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3448 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3450 }
3451
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003452 /*
3453 * The point is, we need to always read a full datagram at once, so we
3454 * sometimes read more then requested, and handle the additional data.
3455 * It could be the rest of the current record (while fetching the
3456 * header) and/or some other records in the same datagram.
3457 */
3458
3459 /*
3460 * Move to the next record in the already read datagram if applicable
3461 */
3462 if( ssl->next_record_offset != 0 )
3463 {
3464 if( ssl->in_left < ssl->next_record_offset )
3465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003468 }
3469
3470 ssl->in_left -= ssl->next_record_offset;
3471
3472 if( ssl->in_left != 0 )
3473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003475 ssl->next_record_offset ) );
3476 memmove( ssl->in_hdr,
3477 ssl->in_hdr + ssl->next_record_offset,
3478 ssl->in_left );
3479 }
3480
3481 ssl->next_record_offset = 0;
3482 }
3483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003485 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003486
3487 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003488 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003489 */
3490 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003493 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003494 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003495
3496 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003497 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003498 * are not at the beginning of a new record, the caller did something
3499 * wrong.
3500 */
3501 if( ssl->in_left != 0 )
3502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3504 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003505 }
3506
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003507 /*
3508 * Don't even try to read if time's out already.
3509 * This avoids by-passing the timer when repeatedly receiving messages
3510 * that will end up being dropped.
3511 */
3512 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003513 {
3514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003515 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003516 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003517 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003518 {
Angus Grattond8213d02016-05-25 20:56:48 +10003519 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003522 timeout = ssl->handshake->retransmit_timeout;
3523 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003524 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003527
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003528 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003529 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3530 timeout );
3531 else
3532 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003535
3536 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003538 }
3539
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003540 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003543 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003546 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003547 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003550 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003551 }
3552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003556 return( ret );
3557 }
3558
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003559 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003560 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003562 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003564 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003565 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003568 return( ret );
3569 }
3570
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003571 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003572 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003574 }
3575
Paul Bakker5121ce52009-01-03 21:22:43 +00003576 if( ret < 0 )
3577 return( ret );
3578
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003579 ssl->in_left = ret;
3580 }
3581 else
3582#endif
3583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003585 ssl->in_left, nb_want ) );
3586
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003587 while( ssl->in_left < nb_want )
3588 {
3589 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003590
3591 if( ssl_check_timer( ssl ) != 0 )
3592 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3593 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003594 {
3595 if( ssl->f_recv_timeout != NULL )
3596 {
3597 ret = ssl->f_recv_timeout( ssl->p_bio,
3598 ssl->in_hdr + ssl->in_left, len,
3599 ssl->conf->read_timeout );
3600 }
3601 else
3602 {
3603 ret = ssl->f_recv( ssl->p_bio,
3604 ssl->in_hdr + ssl->in_left, len );
3605 }
3606 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003609 ssl->in_left, nb_want ) );
3610 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003611
3612 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003614
3615 if( ret < 0 )
3616 return( ret );
3617
mohammad160352aecb92018-03-28 23:41:40 -07003618 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003619 {
Darryl Green11999bb2018-03-13 15:22:58 +00003620 MBEDTLS_SSL_DEBUG_MSG( 1,
3621 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003622 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3624 }
3625
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003626 ssl->in_left += ret;
3627 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 }
3629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003631
3632 return( 0 );
3633}
3634
3635/*
3636 * Flush any data not yet written
3637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003639{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003640 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003641 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003644
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003645 if( ssl->f_send == NULL )
3646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003648 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003650 }
3651
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003652 /* Avoid incrementing counter if data is flushed */
3653 if( ssl->out_left == 0 )
3654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003656 return( 0 );
3657 }
3658
Paul Bakker5121ce52009-01-03 21:22:43 +00003659 while( ssl->out_left > 0 )
3660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003662 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003663
Hanno Becker2b1e3542018-08-06 11:19:13 +01003664 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003665 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003668
3669 if( ret <= 0 )
3670 return( ret );
3671
mohammad160352aecb92018-03-28 23:41:40 -07003672 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003673 {
Darryl Green11999bb2018-03-13 15:22:58 +00003674 MBEDTLS_SSL_DEBUG_MSG( 1,
3675 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003676 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3678 }
3679
Paul Bakker5121ce52009-01-03 21:22:43 +00003680 ssl->out_left -= ret;
3681 }
3682
Hanno Becker2b1e3542018-08-06 11:19:13 +01003683#if defined(MBEDTLS_SSL_PROTO_DTLS)
3684 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003685 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003686 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003687 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003688 else
3689#endif
3690 {
3691 ssl->out_hdr = ssl->out_buf + 8;
3692 }
3693 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003696
3697 return( 0 );
3698}
3699
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003700/*
3701 * Functions to handle the DTLS retransmission state machine
3702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003704/*
3705 * Append current handshake message to current outgoing flight
3706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003708{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3711 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3712 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003713
3714 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003715 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003716 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003719 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003720 }
3721
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003722 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003723 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003726 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003727 }
3728
3729 /* Copy current handshake message with headers */
3730 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3731 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003732 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003733 msg->next = NULL;
3734
3735 /* Append to the current flight */
3736 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003737 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003738 else
3739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003741 while( cur->next != NULL )
3742 cur = cur->next;
3743 cur->next = msg;
3744 }
3745
Hanno Becker3b235902018-08-06 09:54:53 +01003746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003747 return( 0 );
3748}
3749
3750/*
3751 * Free the current flight of handshake messages
3752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003754{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755 mbedtls_ssl_flight_item *cur = flight;
3756 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003757
3758 while( cur != NULL )
3759 {
3760 next = cur->next;
3761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762 mbedtls_free( cur->p );
3763 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003764
3765 cur = next;
3766 }
3767}
3768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3770static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003771#endif
3772
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003773/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003774 * Swap transform_out and out_ctr with the alternative ones
3775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003777{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003778 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003779 unsigned char tmp_out_ctr[8];
3780
3781 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003784 return;
3785 }
3786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003788
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003789 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003790 tmp_transform = ssl->transform_out;
3791 ssl->transform_out = ssl->handshake->alt_transform_out;
3792 ssl->handshake->alt_transform_out = tmp_transform;
3793
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003794 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003795 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3796 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003797 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003798
3799 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003800 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3803 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3808 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003809 }
3810 }
3811#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003812}
3813
3814/*
3815 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003816 */
3817int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3818{
3819 int ret = 0;
3820
3821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3822
3823 ret = mbedtls_ssl_flight_transmit( ssl );
3824
3825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3826
3827 return( ret );
3828}
3829
3830/*
3831 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003832 *
3833 * Need to remember the current message in case flush_output returns
3834 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003835 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003836 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003837int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003838{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003843 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003845
3846 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003847 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003848 ssl_swap_epochs( ssl );
3849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003851 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003852
3853 while( ssl->handshake->cur_msg != NULL )
3854 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003855 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003856 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003857
Hanno Beckere1dcb032018-08-17 16:47:58 +01003858 int const is_finished =
3859 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3860 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3861
Hanno Becker04da1892018-08-14 13:22:10 +01003862 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3863 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3864
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003865 /* Swap epochs before sending Finished: we can't do it after
3866 * sending ChangeCipherSpec, in case write returns WANT_READ.
3867 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003868 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003869 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003871 ssl_swap_epochs( ssl );
3872 }
3873
Hanno Becker67bc7c32018-08-06 11:33:50 +01003874 ret = ssl_get_remaining_payload_in_datagram( ssl );
3875 if( ret < 0 )
3876 return( ret );
3877 max_frag_len = (size_t) ret;
3878
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003879 /* CCS is copied as is, while HS messages may need fragmentation */
3880 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3881 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003882 if( max_frag_len == 0 )
3883 {
3884 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3885 return( ret );
3886
3887 continue;
3888 }
3889
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003890 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003891 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003892 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003893
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003894 /* Update position inside current message */
3895 ssl->handshake->cur_msg_p += cur->len;
3896 }
3897 else
3898 {
3899 const unsigned char * const p = ssl->handshake->cur_msg_p;
3900 const size_t hs_len = cur->len - 12;
3901 const size_t frag_off = p - ( cur->p + 12 );
3902 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003903 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003904
Hanno Beckere1dcb032018-08-17 16:47:58 +01003905 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003906 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003907 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003908 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003909
Hanno Becker67bc7c32018-08-06 11:33:50 +01003910 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3911 return( ret );
3912
3913 continue;
3914 }
3915 max_hs_frag_len = max_frag_len - 12;
3916
3917 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3918 max_hs_frag_len : rem_len;
3919
3920 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003921 {
3922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003923 (unsigned) cur_hs_frag_len,
3924 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003925 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003926
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003927 /* Messages are stored with handshake headers as if not fragmented,
3928 * copy beginning of headers then fill fragmentation fields.
3929 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3930 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003931
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003932 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3933 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3934 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3935
Hanno Becker67bc7c32018-08-06 11:33:50 +01003936 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3937 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3938 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003939
3940 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3941
Hanno Becker3f7b9732018-08-28 09:53:25 +01003942 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003943 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3944 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003945 ssl->out_msgtype = cur->type;
3946
3947 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003948 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003949 }
3950
3951 /* If done with the current message move to the next one if any */
3952 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3953 {
3954 if( cur->next != NULL )
3955 {
3956 ssl->handshake->cur_msg = cur->next;
3957 ssl->handshake->cur_msg_p = cur->next->p + 12;
3958 }
3959 else
3960 {
3961 ssl->handshake->cur_msg = NULL;
3962 ssl->handshake->cur_msg_p = NULL;
3963 }
3964 }
3965
3966 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003967 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003970 return( ret );
3971 }
3972 }
3973
Hanno Becker67bc7c32018-08-06 11:33:50 +01003974 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3975 return( ret );
3976
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003977 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3979 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003980 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003983 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3984 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003985
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003987
3988 return( 0 );
3989}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003990
3991/*
3992 * To be called when the last message of an incoming flight is received.
3993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003995{
3996 /* We won't need to resend that one any more */
3997 ssl_flight_free( ssl->handshake->flight );
3998 ssl->handshake->flight = NULL;
3999 ssl->handshake->cur_msg = NULL;
4000
4001 /* The next incoming flight will start with this msg_seq */
4002 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4003
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004004 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004005 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004006
Hanno Becker0271f962018-08-16 13:23:47 +01004007 /* Clear future message buffering structure. */
4008 ssl_buffering_free( ssl );
4009
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004010 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004011 ssl_set_timer( ssl, 0 );
4012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4014 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004017 }
4018 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004020}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004021
4022/*
4023 * To be called when the last message of an outgoing flight is send.
4024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004026{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004027 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004028 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4031 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004034 }
4035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004037}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004039
Paul Bakker5121ce52009-01-03 21:22:43 +00004040/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004041 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004042 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004043
4044/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004045 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004046 *
4047 * - fill in handshake headers
4048 * - update handshake checksum
4049 * - DTLS: save message for resending
4050 * - then pass to the record layer
4051 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004052 * DTLS: except for HelloRequest, messages are only queued, and will only be
4053 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004054 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004055 * Inputs:
4056 * - ssl->out_msglen: 4 + actual handshake message len
4057 * (4 is the size of handshake headers for TLS)
4058 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4059 * - ssl->out_msg + 4: the handshake message body
4060 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004061 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004062 * - ssl->out_msglen: the length of the record contents
4063 * (including handshake headers but excluding record headers)
4064 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004065 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004066int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004067{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004068 int ret;
4069 const size_t hs_len = ssl->out_msglen - 4;
4070 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004071
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4073
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004074 /*
4075 * Sanity checks
4076 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004077 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004078 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4079 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004080 /* In SSLv3, the client might send a NoCertificate alert. */
4081#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4082 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4083 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4084 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4085#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4086 {
4087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4088 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4089 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004090 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004091
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004092 /* Whenever we send anything different from a
4093 * HelloRequest we should be in a handshake - double check. */
4094 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4095 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004096 ssl->handshake == NULL )
4097 {
4098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4099 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004103 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004104 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004106 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4108 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004109 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004110#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004111
Hanno Beckerb50a2532018-08-06 11:52:54 +01004112 /* Double-check that we did not exceed the bounds
4113 * of the outgoing record buffer.
4114 * This should never fail as the various message
4115 * writing functions must obey the bounds of the
4116 * outgoing record buffer, but better be safe.
4117 *
4118 * Note: We deliberately do not check for the MTU or MFL here.
4119 */
4120 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4121 {
4122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4123 "size %u, maximum %u",
4124 (unsigned) ssl->out_msglen,
4125 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4126 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4127 }
4128
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004129 /*
4130 * Fill handshake headers
4131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004133 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004134 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4135 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4136 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004137
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004138 /*
4139 * DTLS has additional fields in the Handshake layer,
4140 * between the length field and the actual payload:
4141 * uint16 message_seq;
4142 * uint24 fragment_offset;
4143 * uint24 fragment_length;
4144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004146 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004147 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004148 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004149 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004150 {
4151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4152 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004153 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004154 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4156 }
4157
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004158 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004159 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004160
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004161 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004162 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004163 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004164 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4165 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4166 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004167 }
4168 else
4169 {
4170 ssl->out_msg[4] = 0;
4171 ssl->out_msg[5] = 0;
4172 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004173
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004174 /* Handshake hashes are computed without fragmentation,
4175 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004176 memset( ssl->out_msg + 6, 0x00, 3 );
4177 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004178 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004180
Hanno Becker0207e532018-08-28 10:28:28 +01004181 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004182 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4183 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004184 }
4185
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004186 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004188 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004189 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4190 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004191 {
4192 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004195 return( ret );
4196 }
4197 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004198 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004199#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004200 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004201 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004202 {
4203 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4204 return( ret );
4205 }
4206 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004207
4208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4209
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004210 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004211}
4212
4213/*
4214 * Record layer functions
4215 */
4216
4217/*
4218 * Write current record.
4219 *
4220 * Uses:
4221 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4222 * - ssl->out_msglen: length of the record content (excl headers)
4223 * - ssl->out_msg: record content
4224 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004225int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004226{
4227 int ret, done = 0;
4228 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004229 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004230
4231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004234 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004236 {
4237 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004240 return( ret );
4241 }
4242
4243 len = ssl->out_msglen;
4244 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4248 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 ret = mbedtls_ssl_hw_record_write( ssl );
4253 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4256 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004257 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004258
4259 if( ret == 0 )
4260 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004261 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004262#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004263 if( !done )
4264 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004265 unsigned i;
4266 size_t protected_record_size;
4267
Hanno Becker6430faf2019-05-08 11:57:13 +01004268 /* Skip writing the record content type to after the encryption,
4269 * as it may change when using the CID extension. */
4270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004272 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004273
Hanno Becker19859472018-08-06 09:40:20 +01004274 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004275 ssl->out_len[0] = (unsigned char)( len >> 8 );
4276 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004277
Paul Bakker48916f92012-09-16 19:57:18 +00004278 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004279 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004280 mbedtls_record rec;
4281
4282 rec.buf = ssl->out_iv;
4283 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4284 ( ssl->out_iv - ssl->out_buf );
4285 rec.data_len = ssl->out_msglen;
4286 rec.data_offset = ssl->out_msg - rec.buf;
4287
4288 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4289 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4290 ssl->conf->transport, rec.ver );
4291 rec.type = ssl->out_msgtype;
4292
Hanno Beckera0e20d02019-05-15 14:03:01 +01004293#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004294 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004295 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004296#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004297
Hanno Beckera18d1322018-01-03 14:27:32 +00004298 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004299 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004302 return( ret );
4303 }
4304
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004305 if( rec.data_offset != 0 )
4306 {
4307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4309 }
4310
Hanno Becker6430faf2019-05-08 11:57:13 +01004311 /* Update the record content type and CID. */
4312 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004313#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004314 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004315#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004316 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004317 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4318 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004319 }
4320
Hanno Becker5903de42019-05-03 14:46:38 +01004321 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004322
4323#if defined(MBEDTLS_SSL_PROTO_DTLS)
4324 /* In case of DTLS, double-check that we don't exceed
4325 * the remaining space in the datagram. */
4326 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4327 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004328 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004329 if( ret < 0 )
4330 return( ret );
4331
4332 if( protected_record_size > (size_t) ret )
4333 {
4334 /* Should never happen */
4335 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4336 }
4337 }
4338#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004339
Hanno Becker6430faf2019-05-08 11:57:13 +01004340 /* Now write the potentially updated record content type. */
4341 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004344 "version = [%d:%d], msglen = %d",
4345 ssl->out_hdr[0], ssl->out_hdr[1],
4346 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004349 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004350
4351 ssl->out_left += protected_record_size;
4352 ssl->out_hdr += protected_record_size;
4353 ssl_update_out_pointers( ssl, ssl->transform_out );
4354
Hanno Becker04484622018-08-06 09:49:38 +01004355 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4356 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4357 break;
4358
4359 /* The loop goes to its end iff the counter is wrapping */
4360 if( i == ssl_ep_len( ssl ) )
4361 {
4362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4363 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004365 }
4366
Hanno Becker67bc7c32018-08-06 11:33:50 +01004367#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004368 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4369 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004370 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004371 size_t remaining;
4372 ret = ssl_get_remaining_payload_in_datagram( ssl );
4373 if( ret < 0 )
4374 {
4375 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4376 ret );
4377 return( ret );
4378 }
4379
4380 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004381 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004382 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004383 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004384 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004385 else
4386 {
Hanno Becker513815a2018-08-20 11:56:09 +01004387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004388 }
4389 }
4390#endif /* MBEDTLS_SSL_PROTO_DTLS */
4391
4392 if( ( flush == SSL_FORCE_FLUSH ) &&
4393 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004396 return( ret );
4397 }
4398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004400
4401 return( 0 );
4402}
4403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004405
4406static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4407{
4408 if( ssl->in_msglen < ssl->in_hslen ||
4409 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4410 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4411 {
4412 return( 1 );
4413 }
4414 return( 0 );
4415}
Hanno Becker44650b72018-08-16 12:51:11 +01004416
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004417static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004418{
4419 return( ( ssl->in_msg[9] << 16 ) |
4420 ( ssl->in_msg[10] << 8 ) |
4421 ssl->in_msg[11] );
4422}
4423
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004424static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004425{
4426 return( ( ssl->in_msg[6] << 16 ) |
4427 ( ssl->in_msg[7] << 8 ) |
4428 ssl->in_msg[8] );
4429}
4430
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004431static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004432{
4433 uint32_t msg_len, frag_off, frag_len;
4434
4435 msg_len = ssl_get_hs_total_len( ssl );
4436 frag_off = ssl_get_hs_frag_off( ssl );
4437 frag_len = ssl_get_hs_frag_len( ssl );
4438
4439 if( frag_off > msg_len )
4440 return( -1 );
4441
4442 if( frag_len > msg_len - frag_off )
4443 return( -1 );
4444
4445 if( frag_len + 12 > ssl->in_msglen )
4446 return( -1 );
4447
4448 return( 0 );
4449}
4450
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004451/*
4452 * Mark bits in bitmask (used for DTLS HS reassembly)
4453 */
4454static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4455{
4456 unsigned int start_bits, end_bits;
4457
4458 start_bits = 8 - ( offset % 8 );
4459 if( start_bits != 8 )
4460 {
4461 size_t first_byte_idx = offset / 8;
4462
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004463 /* Special case */
4464 if( len <= start_bits )
4465 {
4466 for( ; len != 0; len-- )
4467 mask[first_byte_idx] |= 1 << ( start_bits - len );
4468
4469 /* Avoid potential issues with offset or len becoming invalid */
4470 return;
4471 }
4472
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004473 offset += start_bits; /* Now offset % 8 == 0 */
4474 len -= start_bits;
4475
4476 for( ; start_bits != 0; start_bits-- )
4477 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4478 }
4479
4480 end_bits = len % 8;
4481 if( end_bits != 0 )
4482 {
4483 size_t last_byte_idx = ( offset + len ) / 8;
4484
4485 len -= end_bits; /* Now len % 8 == 0 */
4486
4487 for( ; end_bits != 0; end_bits-- )
4488 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4489 }
4490
4491 memset( mask + offset / 8, 0xFF, len / 8 );
4492}
4493
4494/*
4495 * Check that bitmask is full
4496 */
4497static int ssl_bitmask_check( unsigned char *mask, size_t len )
4498{
4499 size_t i;
4500
4501 for( i = 0; i < len / 8; i++ )
4502 if( mask[i] != 0xFF )
4503 return( -1 );
4504
4505 for( i = 0; i < len % 8; i++ )
4506 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4507 return( -1 );
4508
4509 return( 0 );
4510}
4511
Hanno Becker56e205e2018-08-16 09:06:12 +01004512/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004513static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004514 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004515{
Hanno Becker56e205e2018-08-16 09:06:12 +01004516 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004517
Hanno Becker56e205e2018-08-16 09:06:12 +01004518 alloc_len = 12; /* Handshake header */
4519 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004520
Hanno Beckerd07df862018-08-16 09:14:58 +01004521 if( add_bitmap )
4522 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004523
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004524 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004525}
Hanno Becker56e205e2018-08-16 09:06:12 +01004526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004528
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004529static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004530{
4531 return( ( ssl->in_msg[1] << 16 ) |
4532 ( ssl->in_msg[2] << 8 ) |
4533 ssl->in_msg[3] );
4534}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004535
Simon Butcher99000142016-10-13 17:21:01 +01004536int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004537{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004538 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004541 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004543 }
4544
Hanno Becker12555c62018-08-16 12:47:53 +01004545 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004548 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004549 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004552 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004553 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004554 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004555 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004556
Hanno Becker44650b72018-08-16 12:51:11 +01004557 if( ssl_check_hs_header( ssl ) != 0 )
4558 {
4559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4560 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4561 }
4562
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004563 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004564 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4565 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4566 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4567 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004568 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004569 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4570 {
4571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4572 recv_msg_seq,
4573 ssl->handshake->in_msg_seq ) );
4574 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4575 }
4576
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004577 /* Retransmit only on last message from previous flight, to avoid
4578 * too many retransmissions.
4579 * Besides, No sane server ever retransmits HelloVerifyRequest */
4580 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004584 "message_seq = %d, start_of_flight = %d",
4585 recv_msg_seq,
4586 ssl->handshake->in_flight_start_seq ) );
4587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004591 return( ret );
4592 }
4593 }
4594 else
4595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004597 "message_seq = %d, expected = %d",
4598 recv_msg_seq,
4599 ssl->handshake->in_msg_seq ) );
4600 }
4601
Hanno Becker90333da2017-10-10 11:27:13 +01004602 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004603 }
4604 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004605
Hanno Becker6d97ef52018-08-16 13:09:04 +01004606 /* Message reassembly is handled alongside buffering of future
4607 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004608 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004609 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004610 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004613 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004614 }
4615 }
4616 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004617#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004618 /* With TLS we don't handle fragmentation (for now) */
4619 if( ssl->in_msglen < ssl->in_hslen )
4620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4622 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004623 }
4624
Simon Butcher99000142016-10-13 17:21:01 +01004625 return( 0 );
4626}
4627
4628void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4629{
Hanno Becker0271f962018-08-16 13:23:47 +01004630 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004631
Hanno Becker0271f962018-08-16 13:23:47 +01004632 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004633 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004634 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004635 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004636
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004637 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004639 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004640 ssl->handshake != NULL )
4641 {
Hanno Becker0271f962018-08-16 13:23:47 +01004642 unsigned offset;
4643 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004644
Hanno Becker0271f962018-08-16 13:23:47 +01004645 /* Increment handshake sequence number */
4646 hs->in_msg_seq++;
4647
4648 /*
4649 * Clear up handshake buffering and reassembly structure.
4650 */
4651
4652 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004653 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004654
4655 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004656 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4657 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004658 offset++, hs_buf++ )
4659 {
4660 *hs_buf = *(hs_buf + 1);
4661 }
4662
4663 /* Create a fresh last entry */
4664 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004665 }
4666#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004667}
4668
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004669/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004670 * DTLS anti-replay: RFC 6347 4.1.2.6
4671 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004672 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4673 * Bit n is set iff record number in_window_top - n has been seen.
4674 *
4675 * Usually, in_window_top is the last record number seen and the lsb of
4676 * in_window is set. The only exception is the initial state (record number 0
4677 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004679#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4680static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004681{
4682 ssl->in_window_top = 0;
4683 ssl->in_window = 0;
4684}
4685
4686static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4687{
4688 return( ( (uint64_t) buf[0] << 40 ) |
4689 ( (uint64_t) buf[1] << 32 ) |
4690 ( (uint64_t) buf[2] << 24 ) |
4691 ( (uint64_t) buf[3] << 16 ) |
4692 ( (uint64_t) buf[4] << 8 ) |
4693 ( (uint64_t) buf[5] ) );
4694}
4695
4696/*
4697 * Return 0 if sequence number is acceptable, -1 otherwise
4698 */
Hanno Becker0183d692019-07-12 08:50:37 +01004699int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004700{
4701 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4702 uint64_t bit;
4703
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004704 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004705 return( 0 );
4706
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004707 if( rec_seqnum > ssl->in_window_top )
4708 return( 0 );
4709
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004710 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004711
4712 if( bit >= 64 )
4713 return( -1 );
4714
4715 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4716 return( -1 );
4717
4718 return( 0 );
4719}
4720
4721/*
4722 * Update replay window on new validated record
4723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004725{
4726 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4727
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004728 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004729 return;
4730
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004731 if( rec_seqnum > ssl->in_window_top )
4732 {
4733 /* Update window_top and the contents of the window */
4734 uint64_t shift = rec_seqnum - ssl->in_window_top;
4735
4736 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004737 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004738 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004739 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004740 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004741 ssl->in_window |= 1;
4742 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004743
4744 ssl->in_window_top = rec_seqnum;
4745 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004746 else
4747 {
4748 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004749 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004750
4751 if( bit < 64 ) /* Always true, but be extra sure */
4752 ssl->in_window |= (uint64_t) 1 << bit;
4753 }
4754}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004756
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004757#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004758/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004759static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4760
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004761/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004762 * Without any SSL context, check if a datagram looks like a ClientHello with
4763 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004764 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004765 *
4766 * - if cookie is valid, return 0
4767 * - if ClientHello looks superficially valid but cookie is not,
4768 * fill obuf and set olen, then
4769 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4770 * - otherwise return a specific error code
4771 */
4772static int ssl_check_dtls_clihlo_cookie(
4773 mbedtls_ssl_cookie_write_t *f_cookie_write,
4774 mbedtls_ssl_cookie_check_t *f_cookie_check,
4775 void *p_cookie,
4776 const unsigned char *cli_id, size_t cli_id_len,
4777 const unsigned char *in, size_t in_len,
4778 unsigned char *obuf, size_t buf_len, size_t *olen )
4779{
4780 size_t sid_len, cookie_len;
4781 unsigned char *p;
4782
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004783 /*
4784 * Structure of ClientHello with record and handshake headers,
4785 * and expected values. We don't need to check a lot, more checks will be
4786 * done when actually parsing the ClientHello - skipping those checks
4787 * avoids code duplication and does not make cookie forging any easier.
4788 *
4789 * 0-0 ContentType type; copied, must be handshake
4790 * 1-2 ProtocolVersion version; copied
4791 * 3-4 uint16 epoch; copied, must be 0
4792 * 5-10 uint48 sequence_number; copied
4793 * 11-12 uint16 length; (ignored)
4794 *
4795 * 13-13 HandshakeType msg_type; (ignored)
4796 * 14-16 uint24 length; (ignored)
4797 * 17-18 uint16 message_seq; copied
4798 * 19-21 uint24 fragment_offset; copied, must be 0
4799 * 22-24 uint24 fragment_length; (ignored)
4800 *
4801 * 25-26 ProtocolVersion client_version; (ignored)
4802 * 27-58 Random random; (ignored)
4803 * 59-xx SessionID session_id; 1 byte len + sid_len content
4804 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4805 * ...
4806 *
4807 * Minimum length is 61 bytes.
4808 */
4809 if( in_len < 61 ||
4810 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4811 in[3] != 0 || in[4] != 0 ||
4812 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4813 {
4814 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4815 }
4816
4817 sid_len = in[59];
4818 if( sid_len > in_len - 61 )
4819 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4820
4821 cookie_len = in[60 + sid_len];
4822 if( cookie_len > in_len - 60 )
4823 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4824
4825 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4826 cli_id, cli_id_len ) == 0 )
4827 {
4828 /* Valid cookie */
4829 return( 0 );
4830 }
4831
4832 /*
4833 * If we get here, we've got an invalid cookie, let's prepare HVR.
4834 *
4835 * 0-0 ContentType type; copied
4836 * 1-2 ProtocolVersion version; copied
4837 * 3-4 uint16 epoch; copied
4838 * 5-10 uint48 sequence_number; copied
4839 * 11-12 uint16 length; olen - 13
4840 *
4841 * 13-13 HandshakeType msg_type; hello_verify_request
4842 * 14-16 uint24 length; olen - 25
4843 * 17-18 uint16 message_seq; copied
4844 * 19-21 uint24 fragment_offset; copied
4845 * 22-24 uint24 fragment_length; olen - 25
4846 *
4847 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4848 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4849 *
4850 * Minimum length is 28.
4851 */
4852 if( buf_len < 28 )
4853 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4854
4855 /* Copy most fields and adapt others */
4856 memcpy( obuf, in, 25 );
4857 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4858 obuf[25] = 0xfe;
4859 obuf[26] = 0xff;
4860
4861 /* Generate and write actual cookie */
4862 p = obuf + 28;
4863 if( f_cookie_write( p_cookie,
4864 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4865 {
4866 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4867 }
4868
4869 *olen = p - obuf;
4870
4871 /* Go back and fill length fields */
4872 obuf[27] = (unsigned char)( *olen - 28 );
4873
4874 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4875 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4876 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4877
4878 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4879 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4880
4881 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4882}
4883
4884/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004885 * Handle possible client reconnect with the same UDP quadruplet
4886 * (RFC 6347 Section 4.2.8).
4887 *
4888 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4889 * that looks like a ClientHello.
4890 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004891 * - if the input looks like a ClientHello without cookies,
4892 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004893 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004894 * - if the input looks like a ClientHello with a valid cookie,
4895 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004896 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004897 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004898 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004899 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004900 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4901 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004902 */
4903static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4904{
4905 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004906 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004907
Hanno Becker2fddd372019-07-10 14:37:41 +01004908 if( ssl->conf->f_cookie_write == NULL ||
4909 ssl->conf->f_cookie_check == NULL )
4910 {
4911 /* If we can't use cookies to verify reachability of the peer,
4912 * drop the record. */
4913 return( 0 );
4914 }
4915
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004916 ret = ssl_check_dtls_clihlo_cookie(
4917 ssl->conf->f_cookie_write,
4918 ssl->conf->f_cookie_check,
4919 ssl->conf->p_cookie,
4920 ssl->cli_id, ssl->cli_id_len,
4921 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004922 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004923
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004924 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4925
4926 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004927 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004928 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004929 * If the error is permanent we'll catch it later,
4930 * if it's not, then hopefully it'll work next time. */
4931 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004932 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004933 }
4934
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004935 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004936 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004937 /* Got a valid cookie, partially reset context */
4938 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4939 {
4940 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4941 return( ret );
4942 }
4943
4944 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004945 }
4946
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004947 return( ret );
4948}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004949#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004950
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004951static int ssl_check_record_type( uint8_t record_type )
4952{
4953 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4954 record_type != MBEDTLS_SSL_MSG_ALERT &&
4955 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4956 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4957 {
4958 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4959 }
4960
4961 return( 0 );
4962}
4963
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004964/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004965 * ContentType type;
4966 * ProtocolVersion version;
4967 * uint16 epoch; // DTLS only
4968 * uint48 sequence_number; // DTLS only
4969 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004970 *
4971 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004972 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004973 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4974 *
4975 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004976 * 1. proceed with the record if this function returns 0
4977 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4978 * 3. return CLIENT_RECONNECT if this function return that value
4979 * 4. drop the whole datagram if this function returns anything else.
4980 * Point 2 is needed when the peer is resending, and we have already received
4981 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004982 */
Hanno Becker331de3d2019-07-12 11:10:16 +01004983static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01004984 unsigned char *buf,
4985 size_t len,
4986 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00004987{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004988 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004989
Hanno Beckere5e7e782019-07-11 12:29:35 +01004990 size_t const rec_hdr_type_offset = 0;
4991 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004992
Hanno Beckere5e7e782019-07-11 12:29:35 +01004993 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
4994 rec_hdr_type_len;
4995 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00004996
Hanno Beckere5e7e782019-07-11 12:29:35 +01004997 size_t const rec_hdr_ctr_len = 8;
4998#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01004999 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005000 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5001 rec_hdr_version_len;
5002
Hanno Beckera0e20d02019-05-15 14:03:01 +01005003#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005004 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5005 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005006 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005007#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5008#endif /* MBEDTLS_SSL_PROTO_DTLS */
5009
5010 size_t rec_hdr_len_offset; /* To be determined */
5011 size_t const rec_hdr_len_len = 2;
5012
5013 /*
5014 * Check minimum lengths for record header.
5015 */
5016
5017#if defined(MBEDTLS_SSL_PROTO_DTLS)
5018 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5019 {
5020 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5021 }
5022 else
5023#endif /* MBEDTLS_SSL_PROTO_DTLS */
5024 {
5025 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5026 }
5027
5028 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5029 {
5030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5031 (unsigned) len,
5032 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5033 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5034 }
5035
5036 /*
5037 * Parse and validate record content type
5038 */
5039
5040 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005041
5042 /* Check record content type */
5043#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5044 rec->cid_len = 0;
5045
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005046 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005047 ssl->conf->cid_len != 0 &&
5048 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005049 {
5050 /* Shift pointers to account for record header including CID
5051 * struct {
5052 * ContentType special_type = tls12_cid;
5053 * ProtocolVersion version;
5054 * uint16 epoch;
5055 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005056 * opaque cid[cid_length]; // Additional field compared to
5057 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005058 * uint16 length;
5059 * opaque enc_content[DTLSCiphertext.length];
5060 * } DTLSCiphertext;
5061 */
5062
5063 /* So far, we only support static CID lengths
5064 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005065 rec_hdr_cid_len = ssl->conf->cid_len;
5066 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005067
Hanno Beckere5e7e782019-07-11 12:29:35 +01005068 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005069 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5071 (unsigned) len,
5072 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005073 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005074 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005075
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005076 /* configured CID len is guaranteed at most 255, see
5077 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5078 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005079 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005080 }
5081 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005082#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005083 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005084 if( ssl_check_record_type( rec->type ) )
5085 {
Hanno Becker54229812019-07-12 14:40:00 +01005086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5087 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005088 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5089 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005090 }
5091
Hanno Beckere5e7e782019-07-11 12:29:35 +01005092 /*
5093 * Parse and validate record version
5094 */
5095
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005096 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5097 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005098 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5099 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005100 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005101
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005102 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5105 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005106 }
5107
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005108 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5111 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005112 }
5113
Hanno Beckere5e7e782019-07-11 12:29:35 +01005114 /*
5115 * Parse/Copy record sequence number.
5116 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005117
Hanno Beckere5e7e782019-07-11 12:29:35 +01005118#if defined(MBEDTLS_SSL_PROTO_DTLS)
5119 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005120 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005121 /* Copy explicit record sequence number from input buffer. */
5122 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5123 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005124 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005125 else
5126#endif /* MBEDTLS_SSL_PROTO_DTLS */
5127 {
5128 /* Copy implicit record sequence number from SSL context structure. */
5129 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5130 }
Paul Bakker40e46942009-01-03 21:51:57 +00005131
Hanno Beckere5e7e782019-07-11 12:29:35 +01005132 /*
5133 * Parse record length.
5134 */
5135
Hanno Beckere5e7e782019-07-11 12:29:35 +01005136 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005137 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5138 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005139 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005140
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005142 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005143 rec->type,
5144 major_ver, minor_ver, rec->data_len ) );
5145
5146 rec->buf = buf;
5147 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005148
Hanno Beckerd417cc92019-07-26 08:20:27 +01005149 if( rec->data_len == 0 )
5150 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005151
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005152 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005153 * DTLS-related tests.
5154 * Check epoch before checking length constraint because
5155 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5156 * message gets duplicated before the corresponding Finished message,
5157 * the second ChangeCipherSpec should be discarded because it belongs
5158 * to an old epoch, but not because its length is shorter than
5159 * the minimum record length for packets using the new record transform.
5160 * Note that these two kinds of failures are handled differently,
5161 * as an unexpected record is silently skipped but an invalid
5162 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005163 */
5164#if defined(MBEDTLS_SSL_PROTO_DTLS)
5165 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5166 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005167 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005168
Hanno Becker955a5c92019-07-10 17:12:07 +01005169 /* Check that the datagram is large enough to contain a record
5170 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005171 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005172 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5174 (unsigned) len,
5175 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005176 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5177 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005178
Hanno Becker37cfe732019-07-10 17:20:01 +01005179 /* Records from other, non-matching epochs are silently discarded.
5180 * (The case of same-port Client reconnects must be considered in
5181 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005182 if( rec_epoch != ssl->in_epoch )
5183 {
5184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5185 "expected %d, received %d",
5186 ssl->in_epoch, rec_epoch ) );
5187
Hanno Becker552f7472019-07-19 10:59:12 +01005188 /* Records from the next epoch are considered for buffering
5189 * (concretely: early Finished messages). */
5190 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005191 {
Hanno Becker552f7472019-07-19 10:59:12 +01005192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5193 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005194 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005195
Hanno Becker2fddd372019-07-10 14:37:41 +01005196 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005197 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005198#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005199 /* For records from the correct epoch, check whether their
5200 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005201 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005202 {
5203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5204 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5205 }
5206#endif
5207 }
5208#endif /* MBEDTLS_SSL_PROTO_DTLS */
5209
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005210 return( 0 );
5211}
Paul Bakker5121ce52009-01-03 21:22:43 +00005212
Paul Bakker5121ce52009-01-03 21:22:43 +00005213
Hanno Becker2fddd372019-07-10 14:37:41 +01005214#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5215static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5216{
5217 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5218
5219 /*
5220 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5221 * access the first byte of record content (handshake type), as we
5222 * have an active transform (possibly iv_len != 0), so use the
5223 * fact that the record header len is 13 instead.
5224 */
5225 if( rec_epoch == 0 &&
5226 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5227 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5228 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5229 ssl->in_left > 13 &&
5230 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5231 {
5232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5233 "from the same port" ) );
5234 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005235 }
5236
5237 return( 0 );
5238}
Hanno Becker2fddd372019-07-10 14:37:41 +01005239#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005240
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005241/*
5242 * If applicable, decrypt (and decompress) record content
5243 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005244static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5245 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005246{
5247 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005250 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5253 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005257 ret = mbedtls_ssl_hw_record_read( ssl );
5258 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5261 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005262 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005263
5264 if( ret == 0 )
5265 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005266 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005268 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005269 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005270 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005271
Hanno Beckera18d1322018-01-03 14:27:32 +00005272 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005273 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005276
Hanno Beckera0e20d02019-05-15 14:03:01 +01005277#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005278 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5279 ssl->conf->ignore_unexpected_cid
5280 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5281 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005283 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005284 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005285#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005286
Paul Bakker5121ce52009-01-03 21:22:43 +00005287 return( ret );
5288 }
5289
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005290 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005291 {
5292 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005293 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005294 }
5295
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005296 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005297 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005298
Hanno Beckera0e20d02019-05-15 14:03:01 +01005299#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005300 /* We have already checked the record content type
5301 * in ssl_parse_record_header(), failing or silently
5302 * dropping the record in the case of an unknown type.
5303 *
5304 * Since with the use of CIDs, the record content type
5305 * might change during decryption, re-check the record
5306 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005307 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005308 {
5309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5310 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5311 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005312#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005313
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005314 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005315 {
5316#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5317 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005318 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005319 {
5320 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5322 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5323 }
5324#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5325
5326 ssl->nb_zero++;
5327
5328 /*
5329 * Three or more empty messages may be a DoS attack
5330 * (excessive CPU consumption).
5331 */
5332 if( ssl->nb_zero > 3 )
5333 {
5334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005335 "messages, possible DoS attack" ) );
5336 /* Treat the records as if they were not properly authenticated,
5337 * thereby failing the connection if we see more than allowed
5338 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005339 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5340 }
5341 }
5342 else
5343 ssl->nb_zero = 0;
5344
5345#if defined(MBEDTLS_SSL_PROTO_DTLS)
5346 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5347 {
5348 ; /* in_ctr read from peer, not maintained internally */
5349 }
5350 else
5351#endif
5352 {
5353 unsigned i;
5354 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5355 if( ++ssl->in_ctr[i - 1] != 0 )
5356 break;
5357
5358 /* The loop goes to its end iff the counter is wrapping */
5359 if( i == ssl_ep_len( ssl ) )
5360 {
5361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5362 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5363 }
5364 }
5365
Paul Bakker5121ce52009-01-03 21:22:43 +00005366 }
5367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005369 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005371 {
5372 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005375 return( ret );
5376 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005377 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005381 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005384 }
5385#endif
5386
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005387 /* Check actual (decrypted) record content length against
5388 * configured maximum. */
5389 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5390 {
5391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5392 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5393 }
5394
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005395 return( 0 );
5396}
5397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005399
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005400/*
5401 * Read a record.
5402 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005403 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5404 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5405 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005406 */
Hanno Becker1097b342018-08-15 14:09:41 +01005407
5408/* Helper functions for mbedtls_ssl_read_record(). */
5409static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005410static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5411static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005412
Hanno Becker327c93b2018-08-15 13:56:18 +01005413int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005414 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005415{
5416 int ret;
5417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005419
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005420 if( ssl->keep_current_message == 0 )
5421 {
5422 do {
Simon Butcher99000142016-10-13 17:21:01 +01005423
Hanno Becker26994592018-08-15 14:14:59 +01005424 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005425 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005426 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005427
Hanno Beckere74d5562018-08-15 14:26:08 +01005428 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005429 {
Hanno Becker40f50842018-08-15 14:48:01 +01005430#if defined(MBEDTLS_SSL_PROTO_DTLS)
5431 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005432
Hanno Becker40f50842018-08-15 14:48:01 +01005433 /* We only check for buffered messages if the
5434 * current datagram is fully consumed. */
5435 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005436 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005437 {
Hanno Becker40f50842018-08-15 14:48:01 +01005438 if( ssl_load_buffered_message( ssl ) == 0 )
5439 have_buffered = 1;
5440 }
5441
5442 if( have_buffered == 0 )
5443#endif /* MBEDTLS_SSL_PROTO_DTLS */
5444 {
5445 ret = ssl_get_next_record( ssl );
5446 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5447 continue;
5448
5449 if( ret != 0 )
5450 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005451 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005452 return( ret );
5453 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005454 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005455 }
5456
5457 ret = mbedtls_ssl_handle_message_type( ssl );
5458
Hanno Becker40f50842018-08-15 14:48:01 +01005459#if defined(MBEDTLS_SSL_PROTO_DTLS)
5460 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5461 {
5462 /* Buffer future message */
5463 ret = ssl_buffer_message( ssl );
5464 if( ret != 0 )
5465 return( ret );
5466
5467 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5468 }
5469#endif /* MBEDTLS_SSL_PROTO_DTLS */
5470
Hanno Becker90333da2017-10-10 11:27:13 +01005471 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5472 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005473
5474 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005475 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005476 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005477 return( ret );
5478 }
5479
Hanno Becker327c93b2018-08-15 13:56:18 +01005480 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005481 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005482 {
5483 mbedtls_ssl_update_handshake_status( ssl );
5484 }
Simon Butcher99000142016-10-13 17:21:01 +01005485 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005486 else
Simon Butcher99000142016-10-13 17:21:01 +01005487 {
Hanno Becker02f59072018-08-15 14:00:24 +01005488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005489 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005490 }
5491
5492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5493
5494 return( 0 );
5495}
5496
Hanno Becker40f50842018-08-15 14:48:01 +01005497#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005498static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005499{
Hanno Becker40f50842018-08-15 14:48:01 +01005500 if( ssl->in_left > ssl->next_record_offset )
5501 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005502
Hanno Becker40f50842018-08-15 14:48:01 +01005503 return( 0 );
5504}
5505
5506static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5507{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005508 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005509 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005510 int ret = 0;
5511
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005512 if( hs == NULL )
5513 return( -1 );
5514
Hanno Beckere00ae372018-08-20 09:39:42 +01005515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5516
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005517 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5518 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5519 {
5520 /* Check if we have seen a ChangeCipherSpec before.
5521 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005522 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005523 {
5524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5525 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005526 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005527 }
5528
Hanno Becker39b8bc92018-08-28 17:17:13 +01005529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005530 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5531 ssl->in_msglen = 1;
5532 ssl->in_msg[0] = 1;
5533
5534 /* As long as they are equal, the exact value doesn't matter. */
5535 ssl->in_left = 0;
5536 ssl->next_record_offset = 0;
5537
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005538 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005539 goto exit;
5540 }
Hanno Becker37f95322018-08-16 13:55:32 +01005541
Hanno Beckerb8f50142018-08-28 10:01:34 +01005542#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005543 /* Debug only */
5544 {
5545 unsigned offset;
5546 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5547 {
5548 hs_buf = &hs->buffering.hs[offset];
5549 if( hs_buf->is_valid == 1 )
5550 {
5551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5552 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005553 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005554 }
5555 }
5556 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005557#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005558
5559 /* Check if we have buffered and/or fully reassembled the
5560 * next handshake message. */
5561 hs_buf = &hs->buffering.hs[0];
5562 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5563 {
5564 /* Synthesize a record containing the buffered HS message. */
5565 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5566 ( hs_buf->data[2] << 8 ) |
5567 hs_buf->data[3];
5568
5569 /* Double-check that we haven't accidentally buffered
5570 * a message that doesn't fit into the input buffer. */
5571 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5572 {
5573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5575 }
5576
5577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5578 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5579 hs_buf->data, msg_len + 12 );
5580
5581 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5582 ssl->in_hslen = msg_len + 12;
5583 ssl->in_msglen = msg_len + 12;
5584 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5585
5586 ret = 0;
5587 goto exit;
5588 }
5589 else
5590 {
5591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5592 hs->in_msg_seq ) );
5593 }
5594
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005595 ret = -1;
5596
5597exit:
5598
5599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5600 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005601}
5602
Hanno Beckera02b0b42018-08-21 17:20:27 +01005603static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5604 size_t desired )
5605{
5606 int offset;
5607 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5609 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005610
Hanno Becker01315ea2018-08-21 17:22:17 +01005611 /* Get rid of future records epoch first, if such exist. */
5612 ssl_free_buffered_record( ssl );
5613
5614 /* Check if we have enough space available now. */
5615 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5616 hs->buffering.total_bytes_buffered ) )
5617 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005619 return( 0 );
5620 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005621
Hanno Becker4f432ad2018-08-28 10:02:32 +01005622 /* We don't have enough space to buffer the next expected handshake
5623 * message. Remove buffers used for future messages to gain space,
5624 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005625 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5626 offset >= 0; offset-- )
5627 {
5628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5629 offset ) );
5630
Hanno Beckerb309b922018-08-23 13:18:05 +01005631 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005632
5633 /* Check if we have enough space available now. */
5634 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5635 hs->buffering.total_bytes_buffered ) )
5636 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005638 return( 0 );
5639 }
5640 }
5641
5642 return( -1 );
5643}
5644
Hanno Becker40f50842018-08-15 14:48:01 +01005645static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5646{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005647 int ret = 0;
5648 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5649
5650 if( hs == NULL )
5651 return( 0 );
5652
5653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5654
5655 switch( ssl->in_msgtype )
5656 {
5657 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005659
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005660 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005661 break;
5662
5663 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005664 {
5665 unsigned recv_msg_seq_offset;
5666 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5667 mbedtls_ssl_hs_buffer *hs_buf;
5668 size_t msg_len = ssl->in_hslen - 12;
5669
5670 /* We should never receive an old handshake
5671 * message - double-check nonetheless. */
5672 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5673 {
5674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5675 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5676 }
5677
5678 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5679 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5680 {
5681 /* Silently ignore -- message too far in the future */
5682 MBEDTLS_SSL_DEBUG_MSG( 2,
5683 ( "Ignore future HS message with sequence number %u, "
5684 "buffering window %u - %u",
5685 recv_msg_seq, ssl->handshake->in_msg_seq,
5686 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5687
5688 goto exit;
5689 }
5690
5691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5692 recv_msg_seq, recv_msg_seq_offset ) );
5693
5694 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5695
5696 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005697 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005698 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005699 size_t reassembly_buf_sz;
5700
Hanno Becker37f95322018-08-16 13:55:32 +01005701 hs_buf->is_fragmented =
5702 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5703
5704 /* We copy the message back into the input buffer
5705 * after reassembly, so check that it's not too large.
5706 * This is an implementation-specific limitation
5707 * and not one from the standard, hence it is not
5708 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005709 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005710 {
5711 /* Ignore message */
5712 goto exit;
5713 }
5714
Hanno Beckere0b150f2018-08-21 15:51:03 +01005715 /* Check if we have enough space to buffer the message. */
5716 if( hs->buffering.total_bytes_buffered >
5717 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5718 {
5719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5721 }
5722
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005723 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5724 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005725
5726 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5727 hs->buffering.total_bytes_buffered ) )
5728 {
5729 if( recv_msg_seq_offset > 0 )
5730 {
5731 /* If we can't buffer a future message because
5732 * of space limitations -- ignore. */
5733 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",
5734 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5735 (unsigned) hs->buffering.total_bytes_buffered ) );
5736 goto exit;
5737 }
Hanno Beckere1801392018-08-21 16:51:05 +01005738 else
5739 {
5740 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",
5741 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5742 (unsigned) hs->buffering.total_bytes_buffered ) );
5743 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005744
Hanno Beckera02b0b42018-08-21 17:20:27 +01005745 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005746 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005747 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",
5748 (unsigned) msg_len,
5749 (unsigned) reassembly_buf_sz,
5750 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005751 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005752 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5753 goto exit;
5754 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005755 }
5756
5757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5758 msg_len ) );
5759
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005760 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5761 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005762 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005763 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005764 goto exit;
5765 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005766 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005767
5768 /* Prepare final header: copy msg_type, length and message_seq,
5769 * then add standardised fragment_offset and fragment_length */
5770 memcpy( hs_buf->data, ssl->in_msg, 6 );
5771 memset( hs_buf->data + 6, 0, 3 );
5772 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5773
5774 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005775
5776 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005777 }
5778 else
5779 {
5780 /* Make sure msg_type and length are consistent */
5781 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5782 {
5783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5784 /* Ignore */
5785 goto exit;
5786 }
5787 }
5788
Hanno Becker4422bbb2018-08-20 09:40:19 +01005789 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005790 {
5791 size_t frag_len, frag_off;
5792 unsigned char * const msg = hs_buf->data + 12;
5793
5794 /*
5795 * Check and copy current fragment
5796 */
5797
5798 /* Validation of header fields already done in
5799 * mbedtls_ssl_prepare_handshake_record(). */
5800 frag_off = ssl_get_hs_frag_off( ssl );
5801 frag_len = ssl_get_hs_frag_len( ssl );
5802
5803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5804 frag_off, frag_len ) );
5805 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5806
5807 if( hs_buf->is_fragmented )
5808 {
5809 unsigned char * const bitmask = msg + msg_len;
5810 ssl_bitmask_set( bitmask, frag_off, frag_len );
5811 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5812 msg_len ) == 0 );
5813 }
5814 else
5815 {
5816 hs_buf->is_complete = 1;
5817 }
5818
5819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5820 hs_buf->is_complete ? "" : "not yet " ) );
5821 }
5822
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005823 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005824 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005825
5826 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005827 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005828 break;
5829 }
5830
5831exit:
5832
5833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5834 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005835}
5836#endif /* MBEDTLS_SSL_PROTO_DTLS */
5837
Hanno Becker1097b342018-08-15 14:09:41 +01005838static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005839{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005840 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005841 * Consume last content-layer message and potentially
5842 * update in_msglen which keeps track of the contents'
5843 * consumption state.
5844 *
5845 * (1) Handshake messages:
5846 * Remove last handshake message, move content
5847 * and adapt in_msglen.
5848 *
5849 * (2) Alert messages:
5850 * Consume whole record content, in_msglen = 0.
5851 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005852 * (3) Change cipher spec:
5853 * Consume whole record content, in_msglen = 0.
5854 *
5855 * (4) Application data:
5856 * Don't do anything - the record layer provides
5857 * the application data as a stream transport
5858 * and consumes through mbedtls_ssl_read only.
5859 *
5860 */
5861
5862 /* Case (1): Handshake messages */
5863 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005864 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005865 /* Hard assertion to be sure that no application data
5866 * is in flight, as corrupting ssl->in_msglen during
5867 * ssl->in_offt != NULL is fatal. */
5868 if( ssl->in_offt != NULL )
5869 {
5870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5871 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5872 }
5873
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005874 /*
5875 * Get next Handshake message in the current record
5876 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005877
Hanno Becker4a810fb2017-05-24 16:27:30 +01005878 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005879 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005880 * current handshake content: If DTLS handshake
5881 * fragmentation is used, that's the fragment
5882 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005883 * size here is faulty and should be changed at
5884 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005885 * (2) While it doesn't seem to cause problems, one
5886 * has to be very careful not to assume that in_hslen
5887 * is always <= in_msglen in a sensible communication.
5888 * Again, it's wrong for DTLS handshake fragmentation.
5889 * The following check is therefore mandatory, and
5890 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005891 * Additionally, ssl->in_hslen might be arbitrarily out of
5892 * bounds after handling a DTLS message with an unexpected
5893 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005894 */
5895 if( ssl->in_hslen < ssl->in_msglen )
5896 {
5897 ssl->in_msglen -= ssl->in_hslen;
5898 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5899 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005900
Hanno Becker4a810fb2017-05-24 16:27:30 +01005901 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5902 ssl->in_msg, ssl->in_msglen );
5903 }
5904 else
5905 {
5906 ssl->in_msglen = 0;
5907 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005908
Hanno Becker4a810fb2017-05-24 16:27:30 +01005909 ssl->in_hslen = 0;
5910 }
5911 /* Case (4): Application data */
5912 else if( ssl->in_offt != NULL )
5913 {
5914 return( 0 );
5915 }
5916 /* Everything else (CCS & Alerts) */
5917 else
5918 {
5919 ssl->in_msglen = 0;
5920 }
5921
Hanno Becker1097b342018-08-15 14:09:41 +01005922 return( 0 );
5923}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005924
Hanno Beckere74d5562018-08-15 14:26:08 +01005925static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5926{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005927 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005928 return( 1 );
5929
5930 return( 0 );
5931}
5932
Hanno Becker5f066e72018-08-16 14:56:31 +01005933#if defined(MBEDTLS_SSL_PROTO_DTLS)
5934
5935static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5936{
5937 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5938 if( hs == NULL )
5939 return;
5940
Hanno Becker01315ea2018-08-21 17:22:17 +01005941 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005942 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005943 hs->buffering.total_bytes_buffered -=
5944 hs->buffering.future_record.len;
5945
5946 mbedtls_free( hs->buffering.future_record.data );
5947 hs->buffering.future_record.data = NULL;
5948 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005949}
5950
5951static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5952{
5953 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5954 unsigned char * rec;
5955 size_t rec_len;
5956 unsigned rec_epoch;
5957
5958 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5959 return( 0 );
5960
5961 if( hs == NULL )
5962 return( 0 );
5963
Hanno Becker5f066e72018-08-16 14:56:31 +01005964 rec = hs->buffering.future_record.data;
5965 rec_len = hs->buffering.future_record.len;
5966 rec_epoch = hs->buffering.future_record.epoch;
5967
5968 if( rec == NULL )
5969 return( 0 );
5970
Hanno Becker4cb782d2018-08-20 11:19:05 +01005971 /* Only consider loading future records if the
5972 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005973 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005974 return( 0 );
5975
Hanno Becker5f066e72018-08-16 14:56:31 +01005976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5977
5978 if( rec_epoch != ssl->in_epoch )
5979 {
5980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5981 goto exit;
5982 }
5983
5984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5985
5986 /* Double-check that the record is not too large */
5987 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5988 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5989 {
5990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5992 }
5993
5994 memcpy( ssl->in_hdr, rec, rec_len );
5995 ssl->in_left = rec_len;
5996 ssl->next_record_offset = 0;
5997
5998 ssl_free_buffered_record( ssl );
5999
6000exit:
6001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6002 return( 0 );
6003}
6004
Hanno Becker519f15d2019-07-11 12:43:20 +01006005static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6006 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006007{
6008 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006009
6010 /* Don't buffer future records outside handshakes. */
6011 if( hs == NULL )
6012 return( 0 );
6013
6014 /* Only buffer handshake records (we are only interested
6015 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006016 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006017 return( 0 );
6018
6019 /* Don't buffer more than one future epoch record. */
6020 if( hs->buffering.future_record.data != NULL )
6021 return( 0 );
6022
Hanno Becker01315ea2018-08-21 17:22:17 +01006023 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006024 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006025 hs->buffering.total_bytes_buffered ) )
6026 {
6027 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 +01006028 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006029 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006030 return( 0 );
6031 }
6032
Hanno Becker5f066e72018-08-16 14:56:31 +01006033 /* Buffer record */
6034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6035 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006036 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006037
6038 /* ssl_parse_record_header() only considers records
6039 * of the next epoch as candidates for buffering. */
6040 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006041 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006042
6043 hs->buffering.future_record.data =
6044 mbedtls_calloc( 1, hs->buffering.future_record.len );
6045 if( hs->buffering.future_record.data == NULL )
6046 {
6047 /* If we run out of RAM trying to buffer a
6048 * record from the next epoch, just ignore. */
6049 return( 0 );
6050 }
6051
Hanno Becker519f15d2019-07-11 12:43:20 +01006052 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006053
Hanno Becker519f15d2019-07-11 12:43:20 +01006054 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006055 return( 0 );
6056}
6057
6058#endif /* MBEDTLS_SSL_PROTO_DTLS */
6059
Hanno Beckere74d5562018-08-15 14:26:08 +01006060static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006061{
6062 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006063 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006064
Hanno Becker5f066e72018-08-16 14:56:31 +01006065#if defined(MBEDTLS_SSL_PROTO_DTLS)
6066 /* We might have buffered a future record; if so,
6067 * and if the epoch matches now, load it.
6068 * On success, this call will set ssl->in_left to
6069 * the length of the buffered record, so that
6070 * the calls to ssl_fetch_input() below will
6071 * essentially be no-ops. */
6072 ret = ssl_load_buffered_record( ssl );
6073 if( ret != 0 )
6074 return( ret );
6075#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006076
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006077 /* Ensure that we have enough space available for the default form
6078 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6079 * with no space for CIDs counted in). */
6080 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6081 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006084 return( ret );
6085 }
6086
Hanno Beckere5e7e782019-07-11 12:29:35 +01006087 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6088 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006091 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006092 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006093 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6094 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006095 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006096 if( ret != 0 )
6097 return( ret );
6098
6099 /* Fall through to handling of unexpected records */
6100 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6101 }
6102
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006103 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6104 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006105#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006106 /* Reset in pointers to default state for TLS/DTLS records,
6107 * assuming no CID and no offset between record content and
6108 * record plaintext. */
6109 ssl_update_in_pointers( ssl );
6110
Hanno Becker7ae20e02019-07-12 08:33:49 +01006111 /* Setup internal message pointers from record structure. */
6112 ssl->in_msgtype = rec.type;
6113#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6114 ssl->in_len = ssl->in_cid + rec.cid_len;
6115#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6116 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6117 ssl->in_msglen = rec.data_len;
6118
Hanno Becker2fddd372019-07-10 14:37:41 +01006119 ret = ssl_check_client_reconnect( ssl );
6120 if( ret != 0 )
6121 return( ret );
6122#endif
6123
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006124 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006125 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006126
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6128 "(header)" ) );
6129 }
6130 else
6131 {
6132 /* Skip invalid record and the rest of the datagram */
6133 ssl->next_record_offset = 0;
6134 ssl->in_left = 0;
6135
6136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6137 "(header)" ) );
6138 }
6139
6140 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006141 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006142 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006143 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006144#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006145 {
6146 return( ret );
6147 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006148 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006151 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006152 {
Hanno Beckera8814792019-07-10 15:01:45 +01006153 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006154 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006155 if( ssl->next_record_offset < ssl->in_left )
6156 {
6157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6158 }
6159 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006160 else
6161#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006162 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006163 /*
6164 * Fetch record contents from underlying transport.
6165 */
Hanno Beckera3175662019-07-11 12:50:29 +01006166 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006167 if( ret != 0 )
6168 {
6169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6170 return( ret );
6171 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006172
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006173 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006174 }
6175
6176 /*
6177 * Decrypt record contents.
6178 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006179
Hanno Beckerfdf66042019-07-11 13:07:45 +01006180 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006183 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006184 {
6185 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006186 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006187 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006188 /* Except when waiting for Finished as a bad mac here
6189 * probably means something went wrong in the handshake
6190 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6191 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6192 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6193 {
6194#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6195 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6196 {
6197 mbedtls_ssl_send_alert_message( ssl,
6198 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6199 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6200 }
6201#endif
6202 return( ret );
6203 }
6204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006206 if( ssl->conf->badmac_limit != 0 &&
6207 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6210 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006211 }
6212#endif
6213
Hanno Becker4a810fb2017-05-24 16:27:30 +01006214 /* As above, invalid records cause
6215 * dismissal of the whole datagram. */
6216
6217 ssl->next_record_offset = 0;
6218 ssl->in_left = 0;
6219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006221 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006222 }
6223
6224 return( ret );
6225 }
6226 else
6227#endif
6228 {
6229 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6231 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 mbedtls_ssl_send_alert_message( ssl,
6234 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6235 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006236 }
6237#endif
6238 return( ret );
6239 }
6240 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006241
Hanno Becker44d89b22019-07-12 09:40:44 +01006242
6243 /* Reset in pointers to default state for TLS/DTLS records,
6244 * assuming no CID and no offset between record content and
6245 * record plaintext. */
6246 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006247#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6248 ssl->in_len = ssl->in_cid + rec.cid_len;
6249#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6250 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006251
Hanno Becker8685c822019-07-12 09:37:30 +01006252 /* The record content type may change during decryption,
6253 * so re-read it. */
6254 ssl->in_msgtype = rec.type;
6255 /* Also update the input buffer, because unfortunately
6256 * the server-side ssl_parse_client_hello() reparses the
6257 * record header when receiving a ClientHello initiating
6258 * a renegotiation. */
6259 ssl->in_hdr[0] = rec.type;
6260 ssl->in_msg = rec.buf + rec.data_offset;
6261 ssl->in_msglen = rec.data_len;
6262 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6263 ssl->in_len[1] = (unsigned char)( rec.data_len );
6264
Simon Butcher99000142016-10-13 17:21:01 +01006265 return( 0 );
6266}
6267
6268int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6269{
6270 int ret;
6271
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006272 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006273 * Handle particular types of records
6274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006276 {
Simon Butcher99000142016-10-13 17:21:01 +01006277 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6278 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006279 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006280 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006281 }
6282
Hanno Beckere678eaa2018-08-21 14:57:46 +01006283 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006284 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006285 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006286 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6288 ssl->in_msglen ) );
6289 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006290 }
6291
Hanno Beckere678eaa2018-08-21 14:57:46 +01006292 if( ssl->in_msg[0] != 1 )
6293 {
6294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6295 ssl->in_msg[0] ) );
6296 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6297 }
6298
6299#if defined(MBEDTLS_SSL_PROTO_DTLS)
6300 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6301 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6302 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6303 {
6304 if( ssl->handshake == NULL )
6305 {
6306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6307 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6308 }
6309
6310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6311 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6312 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006313#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006314 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006317 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006318 if( ssl->in_msglen != 2 )
6319 {
6320 /* Note: Standard allows for more than one 2 byte alert
6321 to be packed in a single message, but Mbed TLS doesn't
6322 currently support this. */
6323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6324 ssl->in_msglen ) );
6325 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6326 }
6327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006329 ssl->in_msg[0], ssl->in_msg[1] ) );
6330
6331 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006332 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006337 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006339 }
6340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6342 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6345 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006346 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006347
6348#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6349 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6350 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6351 {
Hanno Becker90333da2017-10-10 11:27:13 +01006352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006353 /* Will be handled when trying to parse ServerHello */
6354 return( 0 );
6355 }
6356#endif
6357
6358#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6359 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6360 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6361 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6362 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6363 {
6364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6365 /* Will be handled in mbedtls_ssl_parse_certificate() */
6366 return( 0 );
6367 }
6368#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6369
6370 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006371 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006372 }
6373
Hanno Beckerc76c6192017-06-06 10:03:17 +01006374#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006375 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006376 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006377 /* Drop unexpected ApplicationData records,
6378 * except at the beginning of renegotiations */
6379 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6380 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6381#if defined(MBEDTLS_SSL_RENEGOTIATION)
6382 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6383 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006384#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006385 )
6386 {
6387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6388 return( MBEDTLS_ERR_SSL_NON_FATAL );
6389 }
6390
6391 if( ssl->handshake != NULL &&
6392 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6393 {
6394 ssl_handshake_wrapup_free_hs_transform( ssl );
6395 }
6396 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006397#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006398
Paul Bakker5121ce52009-01-03 21:22:43 +00006399 return( 0 );
6400}
6401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006403{
6404 int ret;
6405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6407 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6408 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006409 {
6410 return( ret );
6411 }
6412
6413 return( 0 );
6414}
6415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006417 unsigned char level,
6418 unsigned char message )
6419{
6420 int ret;
6421
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006422 if( ssl == NULL || ssl->conf == NULL )
6423 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006426 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006429 ssl->out_msglen = 2;
6430 ssl->out_msg[0] = level;
6431 ssl->out_msg[1] = message;
6432
Hanno Becker67bc7c32018-08-06 11:33:50 +01006433 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006436 return( ret );
6437 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006439
6440 return( 0 );
6441}
6442
Hanno Beckerb9d44792019-02-08 07:19:04 +00006443#if defined(MBEDTLS_X509_CRT_PARSE_C)
6444static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6445{
6446#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6447 if( session->peer_cert != NULL )
6448 {
6449 mbedtls_x509_crt_free( session->peer_cert );
6450 mbedtls_free( session->peer_cert );
6451 session->peer_cert = NULL;
6452 }
6453#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6454 if( session->peer_cert_digest != NULL )
6455 {
6456 /* Zeroization is not necessary. */
6457 mbedtls_free( session->peer_cert_digest );
6458 session->peer_cert_digest = NULL;
6459 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6460 session->peer_cert_digest_len = 0;
6461 }
6462#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6463}
6464#endif /* MBEDTLS_X509_CRT_PARSE_C */
6465
Paul Bakker5121ce52009-01-03 21:22:43 +00006466/*
6467 * Handshake functions
6468 */
Hanno Becker21489932019-02-05 13:20:55 +00006469#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006470/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006472{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006473 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6474 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006477
Hanno Becker7177a882019-02-05 13:36:46 +00006478 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006481 ssl->state++;
6482 return( 0 );
6483 }
6484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6486 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006487}
6488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006490{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006491 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6492 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006495
Hanno Becker7177a882019-02-05 13:36:46 +00006496 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006499 ssl->state++;
6500 return( 0 );
6501 }
6502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6504 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006505}
Gilles Peskinef9828522017-05-03 12:28:43 +02006506
Hanno Becker21489932019-02-05 13:20:55 +00006507#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006508/* Some certificate support -> implement write and parse */
6509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006510int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006511{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006513 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006515 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6516 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006519
Hanno Becker7177a882019-02-05 13:36:46 +00006520 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006523 ssl->state++;
6524 return( 0 );
6525 }
6526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006528 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006529 {
6530 if( ssl->client_auth == 0 )
6531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006533 ssl->state++;
6534 return( 0 );
6535 }
6536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006538 /*
6539 * If using SSLv3 and got no cert, send an Alert message
6540 * (otherwise an empty Certificate message will be sent).
6541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6543 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006544 {
6545 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6547 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6548 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006551 goto write_msg;
6552 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006554 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555#endif /* MBEDTLS_SSL_CLI_C */
6556#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006557 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6562 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006563 }
6564 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006565#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006568
6569 /*
6570 * 0 . 0 handshake type
6571 * 1 . 3 handshake length
6572 * 4 . 6 length of all certs
6573 * 7 . 9 length of cert. 1
6574 * 10 . n-1 peer certificate
6575 * n . n+2 length of cert. 2
6576 * n+3 . ... upper level cert, etc.
6577 */
6578 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006580
Paul Bakker29087132010-03-21 21:03:34 +00006581 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006582 {
6583 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006584 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006587 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006589 }
6590
6591 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6592 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6593 ssl->out_msg[i + 2] = (unsigned char)( n );
6594
6595 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6596 i += n; crt = crt->next;
6597 }
6598
6599 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6600 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6601 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6602
6603 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6605 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006606
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006607#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006608write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006609#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006610
6611 ssl->state++;
6612
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006613 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006614 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006616 return( ret );
6617 }
6618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006620
Paul Bakkered27a042013-04-18 22:46:23 +02006621 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006622}
6623
Hanno Becker84879e32019-01-31 07:44:03 +00006624#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006625
6626#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006627static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6628 unsigned char *crt_buf,
6629 size_t crt_buf_len )
6630{
6631 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6632
6633 if( peer_crt == NULL )
6634 return( -1 );
6635
6636 if( peer_crt->raw.len != crt_buf_len )
6637 return( -1 );
6638
Hanno Becker46f34d02019-02-08 14:00:04 +00006639 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006640}
Hanno Becker177475a2019-02-05 17:02:46 +00006641#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6642static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6643 unsigned char *crt_buf,
6644 size_t crt_buf_len )
6645{
6646 int ret;
6647 unsigned char const * const peer_cert_digest =
6648 ssl->session->peer_cert_digest;
6649 mbedtls_md_type_t const peer_cert_digest_type =
6650 ssl->session->peer_cert_digest_type;
6651 mbedtls_md_info_t const * const digest_info =
6652 mbedtls_md_info_from_type( peer_cert_digest_type );
6653 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6654 size_t digest_len;
6655
6656 if( peer_cert_digest == NULL || digest_info == NULL )
6657 return( -1 );
6658
6659 digest_len = mbedtls_md_get_size( digest_info );
6660 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6661 return( -1 );
6662
6663 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6664 if( ret != 0 )
6665 return( -1 );
6666
6667 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6668}
6669#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006670#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006671
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006672/*
6673 * Once the certificate message is read, parse it into a cert chain and
6674 * perform basic checks, but leave actual verification to the caller
6675 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006676static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6677 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006678{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006679 int ret;
6680#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6681 int crt_cnt=0;
6682#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006683 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006684 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006689 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6690 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006692 }
6693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6695 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006698 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6699 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006701 }
6702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006704
Paul Bakker5121ce52009-01-03 21:22:43 +00006705 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006707 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006708 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006709
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006710 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006714 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6715 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006717 }
6718
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006719 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6720 i += 3;
6721
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006722 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006723 while( i < ssl->in_hslen )
6724 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006725 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006726 if ( i + 3 > ssl->in_hslen ) {
6727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006728 mbedtls_ssl_send_alert_message( ssl,
6729 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6730 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006731 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6732 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006733 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6734 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006735 if( ssl->in_msg[i] != 0 )
6736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006738 mbedtls_ssl_send_alert_message( ssl,
6739 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6740 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006742 }
6743
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006744 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006745 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6746 | (unsigned int) ssl->in_msg[i + 2];
6747 i += 3;
6748
6749 if( n < 128 || i + n > ssl->in_hslen )
6750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006752 mbedtls_ssl_send_alert_message( ssl,
6753 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6754 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006756 }
6757
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006758 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006759#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6760 if( crt_cnt++ == 0 &&
6761 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6762 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006763 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006764 /* During client-side renegotiation, check that the server's
6765 * end-CRTs hasn't changed compared to the initial handshake,
6766 * mitigating the triple handshake attack. On success, reuse
6767 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006768 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6769 if( ssl_check_peer_crt_unchanged( ssl,
6770 &ssl->in_msg[i],
6771 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006772 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6774 mbedtls_ssl_send_alert_message( ssl,
6775 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6776 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6777 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006778 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006779
6780 /* Now we can safely free the original chain. */
6781 ssl_clear_peer_cert( ssl->session );
6782 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006783#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6784
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006785 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006786#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006787 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006788#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006789 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006790 * it in-place from the input buffer instead of making a copy. */
6791 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6792#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006793 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006794 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006795 case 0: /*ok*/
6796 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6797 /* Ignore certificate with an unknown algorithm: maybe a
6798 prior certificate was already trusted. */
6799 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006800
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006801 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6802 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6803 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006804
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006805 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6806 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6807 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006808
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006809 default:
6810 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6811 crt_parse_der_failed:
6812 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6813 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6814 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006815 }
6816
6817 i += n;
6818 }
6819
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006820 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006821 return( 0 );
6822}
6823
Hanno Becker4a55f632019-02-05 12:49:06 +00006824#if defined(MBEDTLS_SSL_SRV_C)
6825static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6826{
6827 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6828 return( -1 );
6829
6830#if defined(MBEDTLS_SSL_PROTO_SSL3)
6831 /*
6832 * Check if the client sent an empty certificate
6833 */
6834 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6835 {
6836 if( ssl->in_msglen == 2 &&
6837 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6838 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6839 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6840 {
6841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6842 return( 0 );
6843 }
6844
6845 return( -1 );
6846 }
6847#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6848
6849#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6850 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6851 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6852 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6853 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6854 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6855 {
6856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6857 return( 0 );
6858 }
6859
6860 return( -1 );
6861#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6862 MBEDTLS_SSL_PROTO_TLS1_2 */
6863}
6864#endif /* MBEDTLS_SSL_SRV_C */
6865
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006866/* Check if a certificate message is expected.
6867 * Return either
6868 * - SSL_CERTIFICATE_EXPECTED, or
6869 * - SSL_CERTIFICATE_SKIP
6870 * indicating whether a Certificate message is expected or not.
6871 */
6872#define SSL_CERTIFICATE_EXPECTED 0
6873#define SSL_CERTIFICATE_SKIP 1
6874static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6875 int authmode )
6876{
6877 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006878 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006879
6880 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6881 return( SSL_CERTIFICATE_SKIP );
6882
6883#if defined(MBEDTLS_SSL_SRV_C)
6884 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6885 {
6886 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6887 return( SSL_CERTIFICATE_SKIP );
6888
6889 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6890 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006891 ssl->session_negotiate->verify_result =
6892 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6893 return( SSL_CERTIFICATE_SKIP );
6894 }
6895 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006896#else
6897 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006898#endif /* MBEDTLS_SSL_SRV_C */
6899
6900 return( SSL_CERTIFICATE_EXPECTED );
6901}
6902
Hanno Becker68636192019-02-05 14:36:34 +00006903static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6904 int authmode,
6905 mbedtls_x509_crt *chain,
6906 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006907{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006908 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006909 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006910 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006911 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006912
Hanno Becker8927c832019-04-03 12:52:50 +01006913 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6914 void *p_vrfy;
6915
Hanno Becker68636192019-02-05 14:36:34 +00006916 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6917 return( 0 );
6918
Hanno Becker8927c832019-04-03 12:52:50 +01006919 if( ssl->f_vrfy != NULL )
6920 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006921 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006922 f_vrfy = ssl->f_vrfy;
6923 p_vrfy = ssl->p_vrfy;
6924 }
6925 else
6926 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006928 f_vrfy = ssl->conf->f_vrfy;
6929 p_vrfy = ssl->conf->p_vrfy;
6930 }
6931
Hanno Becker68636192019-02-05 14:36:34 +00006932 /*
6933 * Main check: verify certificate
6934 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006935#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6936 if( ssl->conf->f_ca_cb != NULL )
6937 {
6938 ((void) rs_ctx);
6939 have_ca_chain = 1;
6940
6941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006942 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006943 chain,
6944 ssl->conf->f_ca_cb,
6945 ssl->conf->p_ca_cb,
6946 ssl->conf->cert_profile,
6947 ssl->hostname,
6948 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006949 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006950 }
6951 else
6952#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6953 {
6954 mbedtls_x509_crt *ca_chain;
6955 mbedtls_x509_crl *ca_crl;
6956
6957#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6958 if( ssl->handshake->sni_ca_chain != NULL )
6959 {
6960 ca_chain = ssl->handshake->sni_ca_chain;
6961 ca_crl = ssl->handshake->sni_ca_crl;
6962 }
6963 else
6964#endif
6965 {
6966 ca_chain = ssl->conf->ca_chain;
6967 ca_crl = ssl->conf->ca_crl;
6968 }
6969
6970 if( ca_chain != NULL )
6971 have_ca_chain = 1;
6972
6973 ret = mbedtls_x509_crt_verify_restartable(
6974 chain,
6975 ca_chain, ca_crl,
6976 ssl->conf->cert_profile,
6977 ssl->hostname,
6978 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006979 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006980 }
Hanno Becker68636192019-02-05 14:36:34 +00006981
6982 if( ret != 0 )
6983 {
6984 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6985 }
6986
6987#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6988 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6989 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6990#endif
6991
6992 /*
6993 * Secondary checks: always done, but change 'ret' only if it was 0
6994 */
6995
6996#if defined(MBEDTLS_ECP_C)
6997 {
6998 const mbedtls_pk_context *pk = &chain->pk;
6999
7000 /* If certificate uses an EC key, make sure the curve is OK */
7001 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7002 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7003 {
7004 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7005
7006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7007 if( ret == 0 )
7008 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7009 }
7010 }
7011#endif /* MBEDTLS_ECP_C */
7012
7013 if( mbedtls_ssl_check_cert_usage( chain,
7014 ciphersuite_info,
7015 ! ssl->conf->endpoint,
7016 &ssl->session_negotiate->verify_result ) != 0 )
7017 {
7018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7019 if( ret == 0 )
7020 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7021 }
7022
7023 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7024 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7025 * with details encoded in the verification flags. All other kinds
7026 * of error codes, including those from the user provided f_vrfy
7027 * functions, are treated as fatal and lead to a failure of
7028 * ssl_parse_certificate even if verification was optional. */
7029 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7030 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7031 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7032 {
7033 ret = 0;
7034 }
7035
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007036 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007037 {
7038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7039 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7040 }
7041
7042 if( ret != 0 )
7043 {
7044 uint8_t alert;
7045
7046 /* The certificate may have been rejected for several reasons.
7047 Pick one and send the corresponding alert. Which alert to send
7048 may be a subject of debate in some cases. */
7049 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7050 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7051 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7052 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7053 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7054 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7055 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7056 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7057 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7058 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7059 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7060 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7061 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7062 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7063 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7064 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7065 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7066 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7067 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7068 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7069 else
7070 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7071 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7072 alert );
7073 }
7074
7075#if defined(MBEDTLS_DEBUG_C)
7076 if( ssl->session_negotiate->verify_result != 0 )
7077 {
7078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7079 ssl->session_negotiate->verify_result ) );
7080 }
7081 else
7082 {
7083 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7084 }
7085#endif /* MBEDTLS_DEBUG_C */
7086
7087 return( ret );
7088}
7089
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007090#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7091static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7092 unsigned char *start, size_t len )
7093{
7094 int ret;
7095 /* Remember digest of the peer's end-CRT. */
7096 ssl->session_negotiate->peer_cert_digest =
7097 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7098 if( ssl->session_negotiate->peer_cert_digest == NULL )
7099 {
7100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7101 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7102 mbedtls_ssl_send_alert_message( ssl,
7103 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7104 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7105
7106 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7107 }
7108
7109 ret = mbedtls_md( mbedtls_md_info_from_type(
7110 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7111 start, len,
7112 ssl->session_negotiate->peer_cert_digest );
7113
7114 ssl->session_negotiate->peer_cert_digest_type =
7115 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7116 ssl->session_negotiate->peer_cert_digest_len =
7117 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7118
7119 return( ret );
7120}
7121
7122static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7123 unsigned char *start, size_t len )
7124{
7125 unsigned char *end = start + len;
7126 int ret;
7127
7128 /* Make a copy of the peer's raw public key. */
7129 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7130 ret = mbedtls_pk_parse_subpubkey( &start, end,
7131 &ssl->handshake->peer_pubkey );
7132 if( ret != 0 )
7133 {
7134 /* We should have parsed the public key before. */
7135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7136 }
7137
7138 return( 0 );
7139}
7140#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7141
Hanno Becker68636192019-02-05 14:36:34 +00007142int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7143{
7144 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007145 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007146#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7147 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7148 ? ssl->handshake->sni_authmode
7149 : ssl->conf->authmode;
7150#else
7151 const int authmode = ssl->conf->authmode;
7152#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007153 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007154 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007155
7156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7157
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007158 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7159 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007160 {
7161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007162 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007163 }
7164
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007165#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7166 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007167 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007168 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007169 chain = ssl->handshake->ecrs_peer_cert;
7170 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007171 goto crt_verify;
7172 }
7173#endif
7174
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007175 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007176 {
7177 /* mbedtls_ssl_read_record may have sent an alert already. We
7178 let it decide whether to alert. */
7179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007180 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007181 }
7182
Hanno Becker4a55f632019-02-05 12:49:06 +00007183#if defined(MBEDTLS_SSL_SRV_C)
7184 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7185 {
7186 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007187
7188 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007189 ret = 0;
7190 else
7191 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007192
Hanno Becker6bdfab22019-02-05 13:11:17 +00007193 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007194 }
7195#endif /* MBEDTLS_SSL_SRV_C */
7196
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007197 /* Clear existing peer CRT structure in case we tried to
7198 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007199 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007200
7201 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7202 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007203 {
7204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7205 sizeof( mbedtls_x509_crt ) ) );
7206 mbedtls_ssl_send_alert_message( ssl,
7207 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7208 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007209
Hanno Becker3dad3112019-02-05 17:19:52 +00007210 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7211 goto exit;
7212 }
7213 mbedtls_x509_crt_init( chain );
7214
7215 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007216 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007217 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007218
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007219#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7220 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007221 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007222
7223crt_verify:
7224 if( ssl->handshake->ecrs_enabled)
7225 rs_ctx = &ssl->handshake->ecrs_ctx;
7226#endif
7227
Hanno Becker68636192019-02-05 14:36:34 +00007228 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007229 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007230 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007231 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007232
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007233#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007234 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007235 unsigned char *crt_start, *pk_start;
7236 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007237
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007238 /* We parse the CRT chain without copying, so
7239 * these pointers point into the input buffer,
7240 * and are hence still valid after freeing the
7241 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007242
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007243 crt_start = chain->raw.p;
7244 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007245
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007246 pk_start = chain->pk_raw.p;
7247 pk_len = chain->pk_raw.len;
7248
7249 /* Free the CRT structures before computing
7250 * digest and copying the peer's public key. */
7251 mbedtls_x509_crt_free( chain );
7252 mbedtls_free( chain );
7253 chain = NULL;
7254
7255 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007256 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007257 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007258
7259 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7260 if( ret != 0 )
7261 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007262 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007263#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7264 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007265 ssl->session_negotiate->peer_cert = chain;
7266 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007267#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007270
Hanno Becker6bdfab22019-02-05 13:11:17 +00007271exit:
7272
Hanno Becker3dad3112019-02-05 17:19:52 +00007273 if( ret == 0 )
7274 ssl->state++;
7275
7276#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7277 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7278 {
7279 ssl->handshake->ecrs_peer_cert = chain;
7280 chain = NULL;
7281 }
7282#endif
7283
7284 if( chain != NULL )
7285 {
7286 mbedtls_x509_crt_free( chain );
7287 mbedtls_free( chain );
7288 }
7289
Paul Bakker5121ce52009-01-03 21:22:43 +00007290 return( ret );
7291}
Hanno Becker21489932019-02-05 13:20:55 +00007292#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007295{
7296 int ret;
7297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007301 ssl->out_msglen = 1;
7302 ssl->out_msg[0] = 1;
7303
Paul Bakker5121ce52009-01-03 21:22:43 +00007304 ssl->state++;
7305
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007306 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007307 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007309 return( ret );
7310 }
7311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007313
7314 return( 0 );
7315}
7316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007318{
7319 int ret;
7320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007322
Hanno Becker327c93b2018-08-15 13:56:18 +01007323 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007326 return( ret );
7327 }
7328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007332 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7333 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007335 }
7336
Hanno Beckere678eaa2018-08-21 14:57:46 +01007337 /* CCS records are only accepted if they have length 1 and content '1',
7338 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007339
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007340 /*
7341 * Switch to our negotiated transform and session parameters for inbound
7342 * data.
7343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007345 ssl->transform_in = ssl->transform_negotiate;
7346 ssl->session_in = ssl->session_negotiate;
7347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007349 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007352 ssl_dtls_replay_reset( ssl );
7353#endif
7354
7355 /* Increment epoch */
7356 if( ++ssl->in_epoch == 0 )
7357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007359 /* This is highly unlikely to happen for legitimate reasons, so
7360 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007362 }
7363 }
7364 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007365#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007366 memset( ssl->in_ctr, 0, 8 );
7367
Hanno Becker79594fd2019-05-08 09:38:41 +01007368 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7371 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007376 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7377 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007379 }
7380 }
7381#endif
7382
Paul Bakker5121ce52009-01-03 21:22:43 +00007383 ssl->state++;
7384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007386
7387 return( 0 );
7388}
7389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7391 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007392{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007393 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7396 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7397 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007398 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007399 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007400#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7402#if defined(MBEDTLS_SHA512_C)
7403 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007404 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7405 else
7406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if defined(MBEDTLS_SHA256_C)
7408 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007409 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007410 else
7411#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007412#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007415 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007416 }
Paul Bakker380da532012-04-18 16:10:25 +00007417}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007420{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7422 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007423 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7424 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007425#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7427#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007428#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007429 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007430 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7431#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007432 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007433#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007434#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007436#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007437 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007438 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007439#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007440 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007441#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007442#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007444}
7445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007447 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007448{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7450 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007451 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7452 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007453#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7455#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007456#if defined(MBEDTLS_USE_PSA_CRYPTO)
7457 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7458#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007459 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007460#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007461#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007463#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007464 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007465#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007466 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007467#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007468#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}
7471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7473 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7474static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007475 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007476{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007477 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7478 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007479}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007480#endif
Paul Bakker380da532012-04-18 16:10:25 +00007481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7483#if defined(MBEDTLS_SHA256_C)
7484static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007485 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007486{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007487#if defined(MBEDTLS_USE_PSA_CRYPTO)
7488 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7489#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007490 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007491#endif
Paul Bakker380da532012-04-18 16:10:25 +00007492}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007493#endif
Paul Bakker380da532012-04-18 16:10:25 +00007494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495#if defined(MBEDTLS_SHA512_C)
7496static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007497 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007498{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007499#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007500 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007501#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007502 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007503#endif
Paul Bakker380da532012-04-18 16:10:25 +00007504}
Paul Bakker769075d2012-11-24 11:26:46 +01007505#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007509static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007511{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007512 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007513 mbedtls_md5_context md5;
7514 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007515
Paul Bakker5121ce52009-01-03 21:22:43 +00007516 unsigned char padbuf[48];
7517 unsigned char md5sum[16];
7518 unsigned char sha1sum[20];
7519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007521 if( !session )
7522 session = ssl->session;
7523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007525
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007526 mbedtls_md5_init( &md5 );
7527 mbedtls_sha1_init( &sha1 );
7528
7529 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7530 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007531
7532 /*
7533 * SSLv3:
7534 * hash =
7535 * MD5( master + pad2 +
7536 * MD5( handshake + sender + master + pad1 ) )
7537 * + SHA1( master + pad2 +
7538 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007539 */
7540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007542 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7543 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007544#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007547 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7548 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007549#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007552 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007553
Paul Bakker1ef83d62012-04-11 12:09:53 +00007554 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007555
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007556 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7557 mbedtls_md5_update_ret( &md5, session->master, 48 );
7558 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7559 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007560
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007561 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7562 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7563 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7564 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007565
Paul Bakker1ef83d62012-04-11 12:09:53 +00007566 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007567
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007568 mbedtls_md5_starts_ret( &md5 );
7569 mbedtls_md5_update_ret( &md5, session->master, 48 );
7570 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7571 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7572 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007573
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007574 mbedtls_sha1_starts_ret( &sha1 );
7575 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7576 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7577 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7578 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007581
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007582 mbedtls_md5_free( &md5 );
7583 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007584
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007585 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7586 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7587 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007590}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007594static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007596{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007597 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007598 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007599 mbedtls_md5_context md5;
7600 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007601 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007604 if( !session )
7605 session = ssl->session;
7606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007608
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007609 mbedtls_md5_init( &md5 );
7610 mbedtls_sha1_init( &sha1 );
7611
7612 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7613 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007614
Paul Bakker1ef83d62012-04-11 12:09:53 +00007615 /*
7616 * TLSv1:
7617 * hash = PRF( master, finished_label,
7618 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7619 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007622 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7623 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007624#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007627 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7628 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007629#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007632 ? "client finished"
7633 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007634
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007635 mbedtls_md5_finish_ret( &md5, padbuf );
7636 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007637
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007638 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007639 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007642
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007643 mbedtls_md5_free( &md5 );
7644 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007645
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007646 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7653#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007654static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007656{
7657 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007658 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007659 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007660#if defined(MBEDTLS_USE_PSA_CRYPTO)
7661 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007662 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007663 psa_status_t status;
7664#else
7665 mbedtls_sha256_context sha256;
7666#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007669 if( !session )
7670 session = ssl->session;
7671
Andrzej Kurekeb342242019-01-29 09:14:33 -05007672 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7673 ? "client finished"
7674 : "server finished";
7675
7676#if defined(MBEDTLS_USE_PSA_CRYPTO)
7677 sha256_psa = psa_hash_operation_init();
7678
7679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7680
7681 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7682 if( status != PSA_SUCCESS )
7683 {
7684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7685 return;
7686 }
7687
7688 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7689 if( status != PSA_SUCCESS )
7690 {
7691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7692 return;
7693 }
7694 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7695#else
7696
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007697 mbedtls_sha256_init( &sha256 );
7698
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007700
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007701 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007702
7703 /*
7704 * TLSv1.2:
7705 * hash = PRF( master, finished_label,
7706 * Hash( handshake ) )[0.11]
7707 */
7708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709#if !defined(MBEDTLS_SHA256_ALT)
7710 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007711 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007712#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007713
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007714 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007715 mbedtls_sha256_free( &sha256 );
7716#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007717
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007718 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007719 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007722
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007723 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007726}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007730static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007732{
7733 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007734 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007735 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007736#if defined(MBEDTLS_USE_PSA_CRYPTO)
7737 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007738 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007739 psa_status_t status;
7740#else
7741 mbedtls_sha512_context sha512;
7742#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007745 if( !session )
7746 session = ssl->session;
7747
Andrzej Kurekeb342242019-01-29 09:14:33 -05007748 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7749 ? "client finished"
7750 : "server finished";
7751
7752#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007753 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007754
7755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7756
Andrzej Kurek972fba52019-01-30 03:29:12 -05007757 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007758 if( status != PSA_SUCCESS )
7759 {
7760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7761 return;
7762 }
7763
Andrzej Kurek972fba52019-01-30 03:29:12 -05007764 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007765 if( status != PSA_SUCCESS )
7766 {
7767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7768 return;
7769 }
7770 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7771#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007772 mbedtls_sha512_init( &sha512 );
7773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007775
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007776 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007777
7778 /*
7779 * TLSv1.2:
7780 * hash = PRF( master, finished_label,
7781 * Hash( handshake ) )[0.11]
7782 */
7783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007785 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7786 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007787#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007788
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007789 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007790 mbedtls_sha512_free( &sha512 );
7791#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007792
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007793 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007794 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007797
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007798 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007801}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802#endif /* MBEDTLS_SHA512_C */
7803#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007806{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007808
7809 /*
7810 * Free our handshake params
7811 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007812 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007814 ssl->handshake = NULL;
7815
7816 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007817 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007818 */
7819 if( ssl->transform )
7820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821 mbedtls_ssl_transform_free( ssl->transform );
7822 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007823 }
7824 ssl->transform = ssl->transform_negotiate;
7825 ssl->transform_negotiate = NULL;
7826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007828}
7829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007831{
7832 int resume = ssl->handshake->resume;
7833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836#if defined(MBEDTLS_SSL_RENEGOTIATION)
7837 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007840 ssl->renego_records_seen = 0;
7841 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007842#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007843
7844 /*
7845 * Free the previous session and switch in the current one
7846 */
Paul Bakker0a597072012-09-25 21:55:46 +00007847 if( ssl->session )
7848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007850 /* RFC 7366 3.1: keep the EtM state */
7851 ssl->session_negotiate->encrypt_then_mac =
7852 ssl->session->encrypt_then_mac;
7853#endif
7854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855 mbedtls_ssl_session_free( ssl->session );
7856 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007857 }
7858 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007859 ssl->session_negotiate = NULL;
7860
Paul Bakker0a597072012-09-25 21:55:46 +00007861 /*
7862 * Add cache entry
7863 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007864 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007865 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007866 resume == 0 )
7867 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007868 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007870 }
Paul Bakker0a597072012-09-25 21:55:46 +00007871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007873 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007874 ssl->handshake->flight != NULL )
7875 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007876 /* Cancel handshake timer */
7877 ssl_set_timer( ssl, 0 );
7878
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007879 /* Keep last flight around in case we need to resend it:
7880 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007882 }
7883 else
7884#endif
7885 ssl_handshake_wrapup_free_hs_transform( ssl );
7886
Paul Bakker48916f92012-09-16 19:57:18 +00007887 ssl->state++;
7888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007890}
7891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007893{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007894 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007897
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007898 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007899
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007900 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007901
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007902 /*
7903 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7904 * may define some other value. Currently (early 2016), no defined
7905 * ciphersuite does this (and this is unlikely to change as activity has
7906 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007911 ssl->verify_data_len = hash_len;
7912 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007913#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007914
Paul Bakker5121ce52009-01-03 21:22:43 +00007915 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7917 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007918
7919 /*
7920 * In case of session resuming, invert the client and server
7921 * ChangeCipherSpec messages order.
7922 */
Paul Bakker0a597072012-09-25 21:55:46 +00007923 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007926 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007928#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007930 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007932#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007933 }
7934 else
7935 ssl->state++;
7936
Paul Bakker48916f92012-09-16 19:57:18 +00007937 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007938 * Switch to our negotiated transform and session parameters for outbound
7939 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007944 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007945 {
7946 unsigned char i;
7947
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007948 /* Remember current epoch settings for resending */
7949 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007950 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007951
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007952 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007953 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007954
7955 /* Increment epoch */
7956 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007957 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007958 break;
7959
7960 /* The loop goes to its end iff the counter is wrapping */
7961 if( i == 0 )
7962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7964 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007965 }
7966 }
7967 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007969 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007970
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007971 ssl->transform_out = ssl->transform_negotiate;
7972 ssl->session_out = ssl->session_negotiate;
7973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7975 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7980 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007981 }
7982 }
7983#endif
7984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007986 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007988#endif
7989
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007990 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007991 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007992 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007993 return( ret );
7994 }
7995
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007996#if defined(MBEDTLS_SSL_PROTO_DTLS)
7997 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7998 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7999 {
8000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8001 return( ret );
8002 }
8003#endif
8004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008006
8007 return( 0 );
8008}
8009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008011#define SSL_MAX_HASH_LEN 36
8012#else
8013#define SSL_MAX_HASH_LEN 12
8014#endif
8015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008017{
Paul Bakker23986e52011-04-24 08:57:21 +00008018 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008019 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008020 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008023
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008024 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008025
Hanno Becker327c93b2018-08-15 13:56:18 +01008026 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008029 return( ret );
8030 }
8031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008035 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8036 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008038 }
8039
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008040 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041#if defined(MBEDTLS_SSL_PROTO_SSL3)
8042 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008043 hash_len = 36;
8044 else
8045#endif
8046 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8049 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008052 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8053 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008055 }
8056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008057 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008058 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008061 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8062 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008064 }
8065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008066#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008067 ssl->verify_data_len = hash_len;
8068 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008069#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008070
Paul Bakker0a597072012-09-25 21:55:46 +00008071 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008073#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008074 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008076#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008078 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008080#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008081 }
8082 else
8083 ssl->state++;
8084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008086 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008088#endif
8089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008091
8092 return( 0 );
8093}
8094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8100 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8101 mbedtls_md5_init( &handshake->fin_md5 );
8102 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008103 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8104 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8107#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008108#if defined(MBEDTLS_USE_PSA_CRYPTO)
8109 handshake->fin_sha256_psa = psa_hash_operation_init();
8110 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8111#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008113 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008114#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008115#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008117#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008118 handshake->fin_sha384_psa = psa_hash_operation_init();
8119 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008120#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008122 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008123#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008124#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008126
8127 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008128
8129#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8130 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8131 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8132#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134#if defined(MBEDTLS_DHM_C)
8135 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137#if defined(MBEDTLS_ECDH_C)
8138 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008139#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008140#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008141 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008142#if defined(MBEDTLS_SSL_CLI_C)
8143 handshake->ecjpake_cache = NULL;
8144 handshake->ecjpake_cache_len = 0;
8145#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008146#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008147
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008148#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008149 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008150#endif
8151
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008152#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8153 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8154#endif
Hanno Becker75173122019-02-06 16:18:31 +00008155
8156#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8157 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8158 mbedtls_pk_init( &handshake->peer_pubkey );
8159#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008160}
8161
Hanno Beckera18d1322018-01-03 14:27:32 +00008162void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008163{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8167 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008168
Hanno Beckerd56ed242018-01-03 15:32:51 +00008169#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170 mbedtls_md_init( &transform->md_ctx_enc );
8171 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008172#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008173}
8174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008176{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008178}
8179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008181{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008182 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008183 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008185 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008187 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008188 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008189
8190 /*
8191 * Either the pointers are now NULL or cleared properly and can be freed.
8192 * Now allocate missing structures.
8193 */
8194 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008195 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008196 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008197 }
Paul Bakker48916f92012-09-16 19:57:18 +00008198
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008199 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008200 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008201 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008202 }
Paul Bakker48916f92012-09-16 19:57:18 +00008203
Paul Bakker82788fb2014-10-20 13:59:19 +02008204 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008205 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008206 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008207 }
Paul Bakker48916f92012-09-16 19:57:18 +00008208
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008209 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008210 if( ssl->handshake == NULL ||
8211 ssl->transform_negotiate == NULL ||
8212 ssl->session_negotiate == NULL )
8213 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 mbedtls_free( ssl->handshake );
8217 mbedtls_free( ssl->transform_negotiate );
8218 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008219
8220 ssl->handshake = NULL;
8221 ssl->transform_negotiate = NULL;
8222 ssl->session_negotiate = NULL;
8223
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008224 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008225 }
8226
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008227 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008229 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008230 ssl_handshake_params_init( ssl->handshake );
8231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008233 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8234 {
8235 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008236
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008237 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8238 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8239 else
8240 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008241
8242 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008243 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008244#endif
8245
Paul Bakker48916f92012-09-16 19:57:18 +00008246 return( 0 );
8247}
8248
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008249#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008250/* Dummy cookie callbacks for defaults */
8251static int ssl_cookie_write_dummy( void *ctx,
8252 unsigned char **p, unsigned char *end,
8253 const unsigned char *cli_id, size_t cli_id_len )
8254{
8255 ((void) ctx);
8256 ((void) p);
8257 ((void) end);
8258 ((void) cli_id);
8259 ((void) cli_id_len);
8260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008262}
8263
8264static int ssl_cookie_check_dummy( void *ctx,
8265 const unsigned char *cookie, size_t cookie_len,
8266 const unsigned char *cli_id, size_t cli_id_len )
8267{
8268 ((void) ctx);
8269 ((void) cookie);
8270 ((void) cookie_len);
8271 ((void) cli_id);
8272 ((void) cli_id_len);
8273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008275}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008276#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008277
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008278/* Once ssl->out_hdr as the address of the beginning of the
8279 * next outgoing record is set, deduce the other pointers.
8280 *
8281 * Note: For TLS, we save the implicit record sequence number
8282 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8283 * and the caller has to make sure there's space for this.
8284 */
8285
8286static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8287 mbedtls_ssl_transform *transform )
8288{
8289#if defined(MBEDTLS_SSL_PROTO_DTLS)
8290 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8291 {
8292 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008293#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008294 ssl->out_cid = ssl->out_ctr + 8;
8295 ssl->out_len = ssl->out_cid;
8296 if( transform != NULL )
8297 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008298#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008299 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008300#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008301 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008302 }
8303 else
8304#endif
8305 {
8306 ssl->out_ctr = ssl->out_hdr - 8;
8307 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008308#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008309 ssl->out_cid = ssl->out_len;
8310#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008311 ssl->out_iv = ssl->out_hdr + 5;
8312 }
8313
8314 /* Adjust out_msg to make space for explicit IV, if used. */
8315 if( transform != NULL &&
8316 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8317 {
8318 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8319 }
8320 else
8321 ssl->out_msg = ssl->out_iv;
8322}
8323
8324/* Once ssl->in_hdr as the address of the beginning of the
8325 * next incoming record is set, deduce the other pointers.
8326 *
8327 * Note: For TLS, we save the implicit record sequence number
8328 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8329 * and the caller has to make sure there's space for this.
8330 */
8331
Hanno Becker79594fd2019-05-08 09:38:41 +01008332static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008333{
Hanno Becker79594fd2019-05-08 09:38:41 +01008334 /* This function sets the pointers to match the case
8335 * of unprotected TLS/DTLS records, with both ssl->in_iv
8336 * and ssl->in_msg pointing to the beginning of the record
8337 * content.
8338 *
8339 * When decrypting a protected record, ssl->in_msg
8340 * will be shifted to point to the beginning of the
8341 * record plaintext.
8342 */
8343
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008344#if defined(MBEDTLS_SSL_PROTO_DTLS)
8345 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8346 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008347 /* This sets the header pointers to match records
8348 * without CID. When we receive a record containing
8349 * a CID, the fields are shifted accordingly in
8350 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008351 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008352#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008353 ssl->in_cid = ssl->in_ctr + 8;
8354 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008355#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008356 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008357#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008358 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008359 }
8360 else
8361#endif
8362 {
8363 ssl->in_ctr = ssl->in_hdr - 8;
8364 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008365#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008366 ssl->in_cid = ssl->in_len;
8367#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008368 ssl->in_iv = ssl->in_hdr + 5;
8369 }
8370
Hanno Becker79594fd2019-05-08 09:38:41 +01008371 /* This will be adjusted at record decryption time. */
8372 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008373}
8374
Paul Bakker5121ce52009-01-03 21:22:43 +00008375/*
8376 * Initialize an SSL context
8377 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008378void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8379{
8380 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8381}
8382
8383/*
8384 * Setup an SSL context
8385 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008386
8387static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8388{
8389 /* Set the incoming and outgoing record pointers. */
8390#if defined(MBEDTLS_SSL_PROTO_DTLS)
8391 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8392 {
8393 ssl->out_hdr = ssl->out_buf;
8394 ssl->in_hdr = ssl->in_buf;
8395 }
8396 else
8397#endif /* MBEDTLS_SSL_PROTO_DTLS */
8398 {
8399 ssl->out_hdr = ssl->out_buf + 8;
8400 ssl->in_hdr = ssl->in_buf + 8;
8401 }
8402
8403 /* Derive other internal pointers. */
8404 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008405 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008406}
8407
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008408int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008409 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008410{
Paul Bakker48916f92012-09-16 19:57:18 +00008411 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008412
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008413 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008414
8415 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008416 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008417 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008418
8419 /* Set to NULL in case of an error condition */
8420 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008421
Angus Grattond8213d02016-05-25 20:56:48 +10008422 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8423 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008424 {
Angus Grattond8213d02016-05-25 20:56:48 +10008425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008426 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008427 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008428 }
8429
8430 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8431 if( ssl->out_buf == NULL )
8432 {
8433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008434 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008435 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008436 }
8437
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008438 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008439
Paul Bakker48916f92012-09-16 19:57:18 +00008440 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008441 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008442
8443 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008444
8445error:
8446 mbedtls_free( ssl->in_buf );
8447 mbedtls_free( ssl->out_buf );
8448
8449 ssl->conf = NULL;
8450
8451 ssl->in_buf = NULL;
8452 ssl->out_buf = NULL;
8453
8454 ssl->in_hdr = NULL;
8455 ssl->in_ctr = NULL;
8456 ssl->in_len = NULL;
8457 ssl->in_iv = NULL;
8458 ssl->in_msg = NULL;
8459
8460 ssl->out_hdr = NULL;
8461 ssl->out_ctr = NULL;
8462 ssl->out_len = NULL;
8463 ssl->out_iv = NULL;
8464 ssl->out_msg = NULL;
8465
k-stachowiak9f7798e2018-07-31 16:52:32 +02008466 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008467}
8468
8469/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008470 * Reset an initialized and used SSL context for re-use while retaining
8471 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008472 *
8473 * If partial is non-zero, keep data in the input buffer and client ID.
8474 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008475 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008476static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008477{
Paul Bakker48916f92012-09-16 19:57:18 +00008478 int ret;
8479
Hanno Becker7e772132018-08-10 12:38:21 +01008480#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8481 !defined(MBEDTLS_SSL_SRV_C)
8482 ((void) partial);
8483#endif
8484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008485 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008486
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008487 /* Cancel any possibly running timer */
8488 ssl_set_timer( ssl, 0 );
8489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008490#if defined(MBEDTLS_SSL_RENEGOTIATION)
8491 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008492 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008493
8494 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008495 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8496 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008497#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008499
Paul Bakker7eb013f2011-10-06 12:37:39 +00008500 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008501 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008502
8503 ssl->in_msgtype = 0;
8504 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008506 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008507 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008508#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008510 ssl_dtls_replay_reset( ssl );
8511#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008512
8513 ssl->in_hslen = 0;
8514 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008515
8516 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008517
8518 ssl->out_msgtype = 0;
8519 ssl->out_msglen = 0;
8520 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008521#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8522 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008523 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008524#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008525
Hanno Becker19859472018-08-06 09:40:20 +01008526 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8527
Paul Bakker48916f92012-09-16 19:57:18 +00008528 ssl->transform_in = NULL;
8529 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008530
Hanno Becker78640902018-08-13 16:35:15 +01008531 ssl->session_in = NULL;
8532 ssl->session_out = NULL;
8533
Angus Grattond8213d02016-05-25 20:56:48 +10008534 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008535
8536#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008537 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008538#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8539 {
8540 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008541 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008542 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8545 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8548 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8551 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008552 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008553 }
8554#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008555
Paul Bakker48916f92012-09-16 19:57:18 +00008556 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558 mbedtls_ssl_transform_free( ssl->transform );
8559 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008560 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008561 }
Paul Bakker48916f92012-09-16 19:57:18 +00008562
Paul Bakkerc0463502013-02-14 11:19:38 +01008563 if( ssl->session )
8564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008565 mbedtls_ssl_session_free( ssl->session );
8566 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008567 ssl->session = NULL;
8568 }
8569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008571 ssl->alpn_chosen = NULL;
8572#endif
8573
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008574#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008575#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008576 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008577#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008578 {
8579 mbedtls_free( ssl->cli_id );
8580 ssl->cli_id = NULL;
8581 ssl->cli_id_len = 0;
8582 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008583#endif
8584
Paul Bakker48916f92012-09-16 19:57:18 +00008585 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8586 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008587
8588 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008589}
8590
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008591/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008592 * Reset an initialized and used SSL context for re-use while retaining
8593 * all application-set variables, function pointers and data.
8594 */
8595int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8596{
8597 return( ssl_session_reset_int( ssl, 0 ) );
8598}
8599
8600/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008601 * SSL set accessors
8602 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008603void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008604{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008605 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008606}
8607
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008608void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008609{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008610 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008611}
8612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008614void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008615{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008616 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008617}
8618#endif
8619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008621void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008622{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008623 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008624}
8625#endif
8626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008628
Hanno Becker1841b0a2018-08-24 11:13:57 +01008629void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8630 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008631{
8632 ssl->disable_datagram_packing = !allow_packing;
8633}
8634
8635void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8636 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008637{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008638 conf->hs_timeout_min = min;
8639 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008640}
8641#endif
8642
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008643void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008644{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008645 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008646}
8647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008649void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008650 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008651 void *p_vrfy )
8652{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008653 conf->f_vrfy = f_vrfy;
8654 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008655}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008657
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008658void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008659 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008660 void *p_rng )
8661{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008662 conf->f_rng = f_rng;
8663 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008664}
8665
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008666void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008667 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008668 void *p_dbg )
8669{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008670 conf->f_dbg = f_dbg;
8671 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008672}
8673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008675 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008676 mbedtls_ssl_send_t *f_send,
8677 mbedtls_ssl_recv_t *f_recv,
8678 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008679{
8680 ssl->p_bio = p_bio;
8681 ssl->f_send = f_send;
8682 ssl->f_recv = f_recv;
8683 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008684}
8685
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008686#if defined(MBEDTLS_SSL_PROTO_DTLS)
8687void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8688{
8689 ssl->mtu = mtu;
8690}
8691#endif
8692
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008693void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008694{
8695 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008696}
8697
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008698void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8699 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008700 mbedtls_ssl_set_timer_t *f_set_timer,
8701 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008702{
8703 ssl->p_timer = p_timer;
8704 ssl->f_set_timer = f_set_timer;
8705 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008706
8707 /* Make sure we start with no timer running */
8708 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008709}
8710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008711#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008712void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008713 void *p_cache,
8714 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8715 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008716{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008717 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008718 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008719 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008720}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723#if defined(MBEDTLS_SSL_CLI_C)
8724int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008725{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008726 int ret;
8727
8728 if( ssl == NULL ||
8729 session == NULL ||
8730 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008731 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008734 }
8735
Hanno Becker52055ae2019-02-06 14:30:46 +00008736 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8737 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008738 return( ret );
8739
Paul Bakker0a597072012-09-25 21:55:46 +00008740 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008741
8742 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008745
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008746void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008747 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008748{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008749 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8750 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8751 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8752 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008753}
8754
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008755void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008756 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008757 int major, int minor )
8758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008759 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008760 return;
8761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008762 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008763 return;
8764
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008765 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008766}
8767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008769void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008770 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008771{
8772 conf->cert_profile = profile;
8773}
8774
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008775/* Append a new keycert entry to a (possibly empty) list */
8776static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8777 mbedtls_x509_crt *cert,
8778 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008779{
niisato8ee24222018-06-25 19:05:48 +09008780 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008781
niisato8ee24222018-06-25 19:05:48 +09008782 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8783 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008784 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008785
niisato8ee24222018-06-25 19:05:48 +09008786 new_cert->cert = cert;
8787 new_cert->key = key;
8788 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008789
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008790 /* Update head is the list was null, else add to the end */
8791 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008792 {
niisato8ee24222018-06-25 19:05:48 +09008793 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008794 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008795 else
8796 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008797 mbedtls_ssl_key_cert *cur = *head;
8798 while( cur->next != NULL )
8799 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008800 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008801 }
8802
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008803 return( 0 );
8804}
8805
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008806int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008807 mbedtls_x509_crt *own_cert,
8808 mbedtls_pk_context *pk_key )
8809{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008810 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008811}
8812
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008813void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008814 mbedtls_x509_crt *ca_chain,
8815 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008816{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008817 conf->ca_chain = ca_chain;
8818 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008819
8820#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8821 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8822 * cannot be used together. */
8823 conf->f_ca_cb = NULL;
8824 conf->p_ca_cb = NULL;
8825#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008826}
Hanno Becker5adaad92019-03-27 16:54:37 +00008827
8828#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8829void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008830 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008831 void *p_ca_cb )
8832{
8833 conf->f_ca_cb = f_ca_cb;
8834 conf->p_ca_cb = p_ca_cb;
8835
8836 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8837 * cannot be used together. */
8838 conf->ca_chain = NULL;
8839 conf->ca_crl = NULL;
8840}
8841#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008843
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008844#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8845int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8846 mbedtls_x509_crt *own_cert,
8847 mbedtls_pk_context *pk_key )
8848{
8849 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8850 own_cert, pk_key ) );
8851}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008852
8853void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8854 mbedtls_x509_crt *ca_chain,
8855 mbedtls_x509_crl *ca_crl )
8856{
8857 ssl->handshake->sni_ca_chain = ca_chain;
8858 ssl->handshake->sni_ca_crl = ca_crl;
8859}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008860
8861void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8862 int authmode )
8863{
8864 ssl->handshake->sni_authmode = authmode;
8865}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008866#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8867
Hanno Becker8927c832019-04-03 12:52:50 +01008868#if defined(MBEDTLS_X509_CRT_PARSE_C)
8869void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8870 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8871 void *p_vrfy )
8872{
8873 ssl->f_vrfy = f_vrfy;
8874 ssl->p_vrfy = p_vrfy;
8875}
8876#endif
8877
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008878#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008879/*
8880 * Set EC J-PAKE password for current handshake
8881 */
8882int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8883 const unsigned char *pw,
8884 size_t pw_len )
8885{
8886 mbedtls_ecjpake_role role;
8887
Janos Follath8eb64132016-06-03 15:40:57 +01008888 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8890
8891 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8892 role = MBEDTLS_ECJPAKE_SERVER;
8893 else
8894 role = MBEDTLS_ECJPAKE_CLIENT;
8895
8896 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8897 role,
8898 MBEDTLS_MD_SHA256,
8899 MBEDTLS_ECP_DP_SECP256R1,
8900 pw, pw_len ) );
8901}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008902#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008904#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008905
8906static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8907{
8908 /* Remove reference to existing PSK, if any. */
8909#if defined(MBEDTLS_USE_PSA_CRYPTO)
8910 if( conf->psk_opaque != 0 )
8911 {
8912 /* The maintenance of the PSK key slot is the
8913 * user's responsibility. */
8914 conf->psk_opaque = 0;
8915 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008916 /* This and the following branch should never
8917 * be taken simultaenously as we maintain the
8918 * invariant that raw and opaque PSKs are never
8919 * configured simultaneously. As a safeguard,
8920 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008921#endif /* MBEDTLS_USE_PSA_CRYPTO */
8922 if( conf->psk != NULL )
8923 {
8924 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8925
8926 mbedtls_free( conf->psk );
8927 conf->psk = NULL;
8928 conf->psk_len = 0;
8929 }
8930
8931 /* Remove reference to PSK identity, if any. */
8932 if( conf->psk_identity != NULL )
8933 {
8934 mbedtls_free( conf->psk_identity );
8935 conf->psk_identity = NULL;
8936 conf->psk_identity_len = 0;
8937 }
8938}
8939
Hanno Becker7390c712018-11-15 13:33:04 +00008940/* This function assumes that PSK identity in the SSL config is unset.
8941 * It checks that the provided identity is well-formed and attempts
8942 * to make a copy of it in the SSL config.
8943 * On failure, the PSK identity in the config remains unset. */
8944static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8945 unsigned char const *psk_identity,
8946 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008947{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008948 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008949 if( psk_identity == NULL ||
8950 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008951 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008952 {
8953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8954 }
8955
Hanno Becker7390c712018-11-15 13:33:04 +00008956 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8957 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008958 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008959
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008960 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008961 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008962
8963 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008964}
8965
Hanno Becker7390c712018-11-15 13:33:04 +00008966int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8967 const unsigned char *psk, size_t psk_len,
8968 const unsigned char *psk_identity, size_t psk_identity_len )
8969{
8970 int ret;
8971 /* Remove opaque/raw PSK + PSK Identity */
8972 ssl_conf_remove_psk( conf );
8973
8974 /* Check and set raw PSK */
8975 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8976 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8977 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8978 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8979 conf->psk_len = psk_len;
8980 memcpy( conf->psk, psk, conf->psk_len );
8981
8982 /* Check and set PSK Identity */
8983 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8984 if( ret != 0 )
8985 ssl_conf_remove_psk( conf );
8986
8987 return( ret );
8988}
8989
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008990static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8991{
8992#if defined(MBEDTLS_USE_PSA_CRYPTO)
8993 if( ssl->handshake->psk_opaque != 0 )
8994 {
8995 ssl->handshake->psk_opaque = 0;
8996 }
8997 else
8998#endif /* MBEDTLS_USE_PSA_CRYPTO */
8999 if( ssl->handshake->psk != NULL )
9000 {
9001 mbedtls_platform_zeroize( ssl->handshake->psk,
9002 ssl->handshake->psk_len );
9003 mbedtls_free( ssl->handshake->psk );
9004 ssl->handshake->psk_len = 0;
9005 }
9006}
9007
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009008int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9009 const unsigned char *psk, size_t psk_len )
9010{
9011 if( psk == NULL || ssl->handshake == NULL )
9012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9013
9014 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9016
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009017 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009018
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009019 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009020 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009021
9022 ssl->handshake->psk_len = psk_len;
9023 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9024
9025 return( 0 );
9026}
9027
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009028#if defined(MBEDTLS_USE_PSA_CRYPTO)
9029int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009030 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009031 const unsigned char *psk_identity,
9032 size_t psk_identity_len )
9033{
Hanno Becker7390c712018-11-15 13:33:04 +00009034 int ret;
9035 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009036 ssl_conf_remove_psk( conf );
9037
Hanno Becker7390c712018-11-15 13:33:04 +00009038 /* Check and set opaque PSK */
9039 if( psk_slot == 0 )
9040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009041 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009042
9043 /* Check and set PSK Identity */
9044 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9045 psk_identity_len );
9046 if( ret != 0 )
9047 ssl_conf_remove_psk( conf );
9048
9049 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009050}
9051
9052int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009053 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009054{
9055 if( psk_slot == 0 || ssl->handshake == NULL )
9056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9057
9058 ssl_remove_psk( ssl );
9059 ssl->handshake->psk_opaque = psk_slot;
9060 return( 0 );
9061}
9062#endif /* MBEDTLS_USE_PSA_CRYPTO */
9063
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009064void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009066 size_t),
9067 void *p_psk )
9068{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009069 conf->f_psk = f_psk;
9070 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009071}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009073
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009074#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009075
9076#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009077int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009078{
9079 int ret;
9080
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009081 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9082 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9083 {
9084 mbedtls_mpi_free( &conf->dhm_P );
9085 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009086 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009087 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009088
9089 return( 0 );
9090}
Hanno Becker470a8c42017-10-04 15:28:46 +01009091#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009092
Hanno Beckera90658f2017-10-04 15:29:08 +01009093int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9094 const unsigned char *dhm_P, size_t P_len,
9095 const unsigned char *dhm_G, size_t G_len )
9096{
9097 int ret;
9098
9099 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9100 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9101 {
9102 mbedtls_mpi_free( &conf->dhm_P );
9103 mbedtls_mpi_free( &conf->dhm_G );
9104 return( ret );
9105 }
9106
9107 return( 0 );
9108}
Paul Bakker5121ce52009-01-03 21:22:43 +00009109
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009110int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009111{
9112 int ret;
9113
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009114 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9115 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9116 {
9117 mbedtls_mpi_free( &conf->dhm_P );
9118 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009119 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009120 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009121
9122 return( 0 );
9123}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009124#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009125
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009126#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9127/*
9128 * Set the minimum length for Diffie-Hellman parameters
9129 */
9130void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9131 unsigned int bitlen )
9132{
9133 conf->dhm_min_bitlen = bitlen;
9134}
9135#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9136
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009137#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009138/*
9139 * Set allowed/preferred hashes for handshake signatures
9140 */
9141void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9142 const int *hashes )
9143{
9144 conf->sig_hashes = hashes;
9145}
Hanno Becker947194e2017-04-07 13:25:49 +01009146#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009147
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009148#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009149/*
9150 * Set the allowed elliptic curves
9151 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009152void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009153 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009154{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009155 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009156}
Hanno Becker947194e2017-04-07 13:25:49 +01009157#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009158
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009159#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009160int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009161{
Hanno Becker947194e2017-04-07 13:25:49 +01009162 /* Initialize to suppress unnecessary compiler warning */
9163 size_t hostname_len = 0;
9164
9165 /* Check if new hostname is valid before
9166 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009167 if( hostname != NULL )
9168 {
9169 hostname_len = strlen( hostname );
9170
9171 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9173 }
9174
9175 /* Now it's clear that we will overwrite the old hostname,
9176 * so we can free it safely */
9177
9178 if( ssl->hostname != NULL )
9179 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009180 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009181 mbedtls_free( ssl->hostname );
9182 }
9183
9184 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009185
Paul Bakker5121ce52009-01-03 21:22:43 +00009186 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009187 {
9188 ssl->hostname = NULL;
9189 }
9190 else
9191 {
9192 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009193 if( ssl->hostname == NULL )
9194 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009195
Hanno Becker947194e2017-04-07 13:25:49 +01009196 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009197
Hanno Becker947194e2017-04-07 13:25:49 +01009198 ssl->hostname[hostname_len] = '\0';
9199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009200
9201 return( 0 );
9202}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009203#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009204
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009205#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009206void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009207 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009208 const unsigned char *, size_t),
9209 void *p_sni )
9210{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009211 conf->f_sni = f_sni;
9212 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009213}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009216#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009217int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009218{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009219 size_t cur_len, tot_len;
9220 const char **p;
9221
9222 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009223 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9224 * MUST NOT be truncated."
9225 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009226 */
9227 tot_len = 0;
9228 for( p = protos; *p != NULL; p++ )
9229 {
9230 cur_len = strlen( *p );
9231 tot_len += cur_len;
9232
9233 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009235 }
9236
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009237 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009238
9239 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009240}
9241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009242const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009243{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009244 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009245}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009247
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009248void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009249{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009250 conf->max_major_ver = major;
9251 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009252}
9253
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009254void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009255{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009256 conf->min_major_ver = major;
9257 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009258}
9259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009261void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009262{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009263 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009264}
9265#endif
9266
Janos Follath088ce432017-04-10 12:42:31 +01009267#if defined(MBEDTLS_SSL_SRV_C)
9268void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9269 char cert_req_ca_list )
9270{
9271 conf->cert_req_ca_list = cert_req_ca_list;
9272}
9273#endif
9274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009275#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009276void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009277{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009278 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009279}
9280#endif
9281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009283void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009284{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009285 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009286}
9287#endif
9288
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009289#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009290void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009291{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009292 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009293}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009294#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009297int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009298{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009300 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009303 }
9304
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009305 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009306
9307 return( 0 );
9308}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009312void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009313{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009314 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009315}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009319void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009320{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009321 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009322}
9323#endif
9324
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009325void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009326{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009327 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009328}
9329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009331void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009332{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009333 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009334}
9335
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009336void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009337{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009338 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009339}
9340
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009341void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009342 const unsigned char period[8] )
9343{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009344 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009345}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009349#if defined(MBEDTLS_SSL_CLI_C)
9350void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009351{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009352 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009353}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009354#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009355
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009356#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009357void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9358 mbedtls_ssl_ticket_write_t *f_ticket_write,
9359 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9360 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009361{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009362 conf->f_ticket_write = f_ticket_write;
9363 conf->f_ticket_parse = f_ticket_parse;
9364 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009365}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009368
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009369#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9370void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9371 mbedtls_ssl_export_keys_t *f_export_keys,
9372 void *p_export_keys )
9373{
9374 conf->f_export_keys = f_export_keys;
9375 conf->p_export_keys = p_export_keys;
9376}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009377
9378void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9379 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9380 void *p_export_keys )
9381{
9382 conf->f_export_keys_ext = f_export_keys_ext;
9383 conf->p_export_keys = p_export_keys;
9384}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009385#endif
9386
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009387#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009388void mbedtls_ssl_conf_async_private_cb(
9389 mbedtls_ssl_config *conf,
9390 mbedtls_ssl_async_sign_t *f_async_sign,
9391 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9392 mbedtls_ssl_async_resume_t *f_async_resume,
9393 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009394 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009395{
9396 conf->f_async_sign_start = f_async_sign;
9397 conf->f_async_decrypt_start = f_async_decrypt;
9398 conf->f_async_resume = f_async_resume;
9399 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009400 conf->p_async_config_data = async_config_data;
9401}
9402
Gilles Peskine8f97af72018-04-26 11:46:10 +02009403void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9404{
9405 return( conf->p_async_config_data );
9406}
9407
Gilles Peskine1febfef2018-04-30 11:54:39 +02009408void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009409{
9410 if( ssl->handshake == NULL )
9411 return( NULL );
9412 else
9413 return( ssl->handshake->user_async_ctx );
9414}
9415
Gilles Peskine1febfef2018-04-30 11:54:39 +02009416void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009417 void *ctx )
9418{
9419 if( ssl->handshake != NULL )
9420 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009421}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009422#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009423
Paul Bakker5121ce52009-01-03 21:22:43 +00009424/*
9425 * SSL get accessors
9426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009428{
9429 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9430}
9431
Hanno Becker8b170a02017-10-10 11:51:19 +01009432int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9433{
9434 /*
9435 * Case A: We're currently holding back
9436 * a message for further processing.
9437 */
9438
9439 if( ssl->keep_current_message == 1 )
9440 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009442 return( 1 );
9443 }
9444
9445 /*
9446 * Case B: Further records are pending in the current datagram.
9447 */
9448
9449#if defined(MBEDTLS_SSL_PROTO_DTLS)
9450 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9451 ssl->in_left > ssl->next_record_offset )
9452 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009453 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009454 return( 1 );
9455 }
9456#endif /* MBEDTLS_SSL_PROTO_DTLS */
9457
9458 /*
9459 * Case C: A handshake message is being processed.
9460 */
9461
Hanno Becker8b170a02017-10-10 11:51:19 +01009462 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9463 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009464 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009465 return( 1 );
9466 }
9467
9468 /*
9469 * Case D: An application data message is being processed
9470 */
9471 if( ssl->in_offt != NULL )
9472 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009474 return( 1 );
9475 }
9476
9477 /*
9478 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009479 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009480 * we implement support for multiple alerts in single records.
9481 */
9482
9483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9484 return( 0 );
9485}
9486
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009487uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009488{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009489 if( ssl->session != NULL )
9490 return( ssl->session->verify_result );
9491
9492 if( ssl->session_negotiate != NULL )
9493 return( ssl->session_negotiate->verify_result );
9494
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009495 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009496}
9497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009499{
Paul Bakker926c8e42013-03-06 10:23:34 +01009500 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009501 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009504}
9505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009509 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009510 {
9511 switch( ssl->minor_ver )
9512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009514 return( "DTLSv1.0" );
9515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009517 return( "DTLSv1.2" );
9518
9519 default:
9520 return( "unknown (DTLS)" );
9521 }
9522 }
9523#endif
9524
Paul Bakker43ca69c2011-01-15 17:35:19 +00009525 switch( ssl->minor_ver )
9526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009528 return( "SSLv3.0" );
9529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009531 return( "TLSv1.0" );
9532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009533 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009534 return( "TLSv1.1" );
9535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009537 return( "TLSv1.2" );
9538
Paul Bakker43ca69c2011-01-15 17:35:19 +00009539 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009540 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009541 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009542}
9543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009545{
Hanno Becker3136ede2018-08-17 15:28:19 +01009546 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009548 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009549
Hanno Becker5903de42019-05-03 14:46:38 +01009550 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9551
Hanno Becker78640902018-08-13 16:35:15 +01009552 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009553 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555#if defined(MBEDTLS_ZLIB_SUPPORT)
9556 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9557 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009558#endif
9559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009562 case MBEDTLS_MODE_GCM:
9563 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009564 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009566 transform_expansion = transform->minlen;
9567 break;
9568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009570
9571 block_size = mbedtls_cipher_get_block_size(
9572 &transform->cipher_ctx_enc );
9573
Hanno Becker3136ede2018-08-17 15:28:19 +01009574 /* Expansion due to the addition of the MAC. */
9575 transform_expansion += transform->maclen;
9576
9577 /* Expansion due to the addition of CBC padding;
9578 * Theoretically up to 256 bytes, but we never use
9579 * more than the block size of the underlying cipher. */
9580 transform_expansion += block_size;
9581
9582 /* For TLS 1.1 or higher, an explicit IV is added
9583 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009584#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9585 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009586 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009587#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009588
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009589 break;
9590
9591 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009594 }
9595
Hanno Beckera0e20d02019-05-15 14:03:01 +01009596#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009597 if( transform->out_cid_len != 0 )
9598 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009599#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009600
Hanno Becker5903de42019-05-03 14:46:38 +01009601 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009602}
9603
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009604#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9605size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9606{
9607 size_t max_len;
9608
9609 /*
9610 * Assume mfl_code is correct since it was checked when set
9611 */
Angus Grattond8213d02016-05-25 20:56:48 +10009612 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009613
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009614 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009615 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009616 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009617 {
Angus Grattond8213d02016-05-25 20:56:48 +10009618 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009619 }
9620
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009621 /* During a handshake, use the value being negotiated */
9622 if( ssl->session_negotiate != NULL &&
9623 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9624 {
9625 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9626 }
9627
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009628 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009629}
9630#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9631
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009632#if defined(MBEDTLS_SSL_PROTO_DTLS)
9633static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9634{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009635 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9636 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9637 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9638 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9639 return ( 0 );
9640
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009641 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9642 return( ssl->mtu );
9643
9644 if( ssl->mtu == 0 )
9645 return( ssl->handshake->mtu );
9646
9647 return( ssl->mtu < ssl->handshake->mtu ?
9648 ssl->mtu : ssl->handshake->mtu );
9649}
9650#endif /* MBEDTLS_SSL_PROTO_DTLS */
9651
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009652int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9653{
9654 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9655
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009656#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9657 !defined(MBEDTLS_SSL_PROTO_DTLS)
9658 (void) ssl;
9659#endif
9660
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009661#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9662 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9663
9664 if( max_len > mfl )
9665 max_len = mfl;
9666#endif
9667
9668#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009669 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009670 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009671 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009672 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9673 const size_t overhead = (size_t) ret;
9674
9675 if( ret < 0 )
9676 return( ret );
9677
9678 if( mtu <= overhead )
9679 {
9680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9681 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9682 }
9683
9684 if( max_len > mtu - overhead )
9685 max_len = mtu - overhead;
9686 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009687#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009688
Hanno Becker0defedb2018-08-10 12:35:02 +01009689#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9690 !defined(MBEDTLS_SSL_PROTO_DTLS)
9691 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009692#endif
9693
9694 return( (int) max_len );
9695}
9696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009697#if defined(MBEDTLS_X509_CRT_PARSE_C)
9698const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009699{
9700 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009701 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009702
Hanno Beckere6824572019-02-07 13:18:46 +00009703#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009704 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009705#else
9706 return( NULL );
9707#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009709#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009712int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9713 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009714{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009715 if( ssl == NULL ||
9716 dst == NULL ||
9717 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009718 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009720 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009721 }
9722
Hanno Becker52055ae2019-02-06 14:30:46 +00009723 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009724}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009725#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009726
Paul Bakker5121ce52009-01-03 21:22:43 +00009727/*
Paul Bakker1961b702013-01-25 14:49:24 +01009728 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009730int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009731{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009733
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009734 if( ssl == NULL || ssl->conf == NULL )
9735 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009738 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009740#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009742 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009744#endif
9745
Paul Bakker1961b702013-01-25 14:49:24 +01009746 return( ret );
9747}
9748
9749/*
9750 * Perform the SSL handshake
9751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009752int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009753{
9754 int ret = 0;
9755
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009756 if( ssl == NULL || ssl->conf == NULL )
9757 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009764
9765 if( ret != 0 )
9766 break;
9767 }
9768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009770
9771 return( ret );
9772}
9773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774#if defined(MBEDTLS_SSL_RENEGOTIATION)
9775#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009776/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009777 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009780{
9781 int ret;
9782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009784
9785 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9787 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009788
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009789 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009790 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009791 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009792 return( ret );
9793 }
9794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009796
9797 return( 0 );
9798}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009800
9801/*
9802 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803 * - any side: calling mbedtls_ssl_renegotiate(),
9804 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9805 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009806 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009807 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009811{
9812 int ret;
9813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009815
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009816 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9817 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009818
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009819 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9820 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009822 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009824 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009825 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009826 ssl->handshake->out_msg_seq = 1;
9827 else
9828 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009829 }
9830#endif
9831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9833 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009838 return( ret );
9839 }
9840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009842
9843 return( 0 );
9844}
9845
9846/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009847 * Renegotiate current connection on client,
9848 * or request renegotiation on server
9849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009853
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009854 if( ssl == NULL || ssl->conf == NULL )
9855 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009858 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009859 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9862 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009865
9866 /* Did we already try/start sending HelloRequest? */
9867 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009869
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009870 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009871 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009875 /*
9876 * On client, either start the renegotiation process or,
9877 * if already in progress, continue the handshake
9878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009881 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009883
9884 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009886 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009887 return( ret );
9888 }
9889 }
9890 else
9891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009895 return( ret );
9896 }
9897 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009898#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009899
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009900 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009901}
9902
9903/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009904 * Check record counters and renegotiate if they're above the limit.
9905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009907{
Andres AG2196c7f2016-12-15 17:01:16 +00009908 size_t ep_len = ssl_ep_len( ssl );
9909 int in_ctr_cmp;
9910 int out_ctr_cmp;
9911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9913 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009914 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009915 {
9916 return( 0 );
9917 }
9918
Andres AG2196c7f2016-12-15 17:01:16 +00009919 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9920 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009921 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009922 ssl->conf->renego_period + ep_len, 8 - ep_len );
9923
9924 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009925 {
9926 return( 0 );
9927 }
9928
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009931}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009933
9934/*
9935 * Receive application data decrypted from the SSL layer
9936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009938{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009939 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009940 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009941
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009942 if( ssl == NULL || ssl->conf == NULL )
9943 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009948 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009951 return( ret );
9952
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009953 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009954 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009955 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009956 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009957 return( ret );
9958 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009959 }
9960#endif
9961
Hanno Becker4a810fb2017-05-24 16:27:30 +01009962 /*
9963 * Check if renegotiation is necessary and/or handshake is
9964 * in process. If yes, perform/continue, and fall through
9965 * if an unexpected packet is received while the client
9966 * is waiting for the ServerHello.
9967 *
9968 * (There is no equivalent to the last condition on
9969 * the server-side as it is not treated as within
9970 * a handshake while waiting for the ClientHello
9971 * after a renegotiation request.)
9972 */
9973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009975 ret = ssl_check_ctr_renegotiate( ssl );
9976 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9977 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009979 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009980 return( ret );
9981 }
9982#endif
9983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009986 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009987 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9988 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009991 return( ret );
9992 }
9993 }
9994
Hanno Beckere41158b2017-10-23 13:30:32 +01009995 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009996 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009997 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009998 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009999 if( ssl->f_get_timer != NULL &&
10000 ssl->f_get_timer( ssl->p_timer ) == -1 )
10001 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010002 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010003 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010004
Hanno Becker327c93b2018-08-15 13:56:18 +010010005 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010006 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010007 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10008 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010009
Hanno Becker4a810fb2017-05-24 16:27:30 +010010010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10011 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010012 }
10013
10014 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010015 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010016 {
10017 /*
10018 * OpenSSL sends empty messages to randomize the IV
10019 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010020 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010022 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010023 return( 0 );
10024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010026 return( ret );
10027 }
10028 }
10029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010030 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010033
Hanno Becker4a810fb2017-05-24 16:27:30 +010010034 /*
10035 * - For client-side, expect SERVER_HELLO_REQUEST.
10036 * - For server-side, expect CLIENT_HELLO.
10037 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10038 */
10039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010041 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010042 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010043 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010046
10047 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010049 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010050 {
10051 continue;
10052 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010055 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010056#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010057
Hanno Becker4a810fb2017-05-24 16:27:30 +010010058#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010059 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010063
10064 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010065#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010066 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010067 {
10068 continue;
10069 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010072 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010073#endif /* MBEDTLS_SSL_SRV_C */
10074
Hanno Becker21df7f92017-10-17 11:03:26 +010010075#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010076 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010077 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10078 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10079 ssl->conf->allow_legacy_renegotiation ==
10080 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10081 {
10082 /*
10083 * Accept renegotiation request
10084 */
Paul Bakker48916f92012-09-16 19:57:18 +000010085
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010086 /* DTLS clients need to know renego is server-initiated */
10087#if defined(MBEDTLS_SSL_PROTO_DTLS)
10088 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10089 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10090 {
10091 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10092 }
10093#endif
10094 ret = ssl_start_renegotiation( ssl );
10095 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10096 ret != 0 )
10097 {
10098 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10099 return( ret );
10100 }
10101 }
10102 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010103#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010104 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010105 /*
10106 * Refuse renegotiation
10107 */
10108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111#if defined(MBEDTLS_SSL_PROTO_SSL3)
10112 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010113 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010114 /* SSLv3 does not have a "no_renegotiation" warning, so
10115 we send a fatal alert and abort the connection. */
10116 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10117 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10118 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010119 }
10120 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10122#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10123 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10124 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10127 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10128 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010129 {
10130 return( ret );
10131 }
Paul Bakker48916f92012-09-16 19:57:18 +000010132 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010133 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010134#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10135 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10138 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010139 }
Paul Bakker48916f92012-09-16 19:57:18 +000010140 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010141
Hanno Becker90333da2017-10-10 11:27:13 +010010142 /* At this point, we don't know whether the renegotiation has been
10143 * completed or not. The cases to consider are the following:
10144 * 1) The renegotiation is complete. In this case, no new record
10145 * has been read yet.
10146 * 2) The renegotiation is incomplete because the client received
10147 * an application data record while awaiting the ServerHello.
10148 * 3) The renegotiation is incomplete because the client received
10149 * a non-handshake, non-application data message while awaiting
10150 * the ServerHello.
10151 * In each of these case, looping will be the proper action:
10152 * - For 1), the next iteration will read a new record and check
10153 * if it's application data.
10154 * - For 2), the loop condition isn't satisfied as application data
10155 * is present, hence continue is the same as break
10156 * - For 3), the loop condition is satisfied and read_record
10157 * will re-deliver the message that was held back by the client
10158 * when expecting the ServerHello.
10159 */
10160 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010161 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010162#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010163 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010164 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010165 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010166 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010167 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010170 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010172 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010173 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010174 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010175#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010177 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10178 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010181 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010182 }
10183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10187 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010188 }
10189
10190 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010191
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010192 /* We're going to return something now, cancel timer,
10193 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010194 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010195 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010196
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010197#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010198 /* If we requested renego but received AppData, resend HelloRequest.
10199 * Do it now, after setting in_offt, to avoid taking this branch
10200 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010201#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010202 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010204 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010205 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010207 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010208 return( ret );
10209 }
10210 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010212#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010213 }
10214
10215 n = ( len < ssl->in_msglen )
10216 ? len : ssl->in_msglen;
10217
10218 memcpy( buf, ssl->in_offt, n );
10219 ssl->in_msglen -= n;
10220
10221 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010222 {
10223 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010224 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010225 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010226 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010227 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010228 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010229 /* more data available */
10230 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010231 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010234
Paul Bakker23986e52011-04-24 08:57:21 +000010235 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010236}
10237
10238/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010239 * Send application data to be encrypted by the SSL layer, taking care of max
10240 * fragment length and buffer size.
10241 *
10242 * According to RFC 5246 Section 6.2.1:
10243 *
10244 * Zero-length fragments of Application data MAY be sent as they are
10245 * potentially useful as a traffic analysis countermeasure.
10246 *
10247 * Therefore, it is possible that the input message length is 0 and the
10248 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010249 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010250static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010251 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010252{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010253 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10254 const size_t max_len = (size_t) ret;
10255
10256 if( ret < 0 )
10257 {
10258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10259 return( ret );
10260 }
10261
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010262 if( len > max_len )
10263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010265 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010268 "maximum fragment length: %d > %d",
10269 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010271 }
10272 else
10273#endif
10274 len = max_len;
10275 }
Paul Bakker887bd502011-06-08 13:10:54 +000010276
Paul Bakker5121ce52009-01-03 21:22:43 +000010277 if( ssl->out_left != 0 )
10278 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010279 /*
10280 * The user has previously tried to send the data and
10281 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10282 * written. In this case, we expect the high-level write function
10283 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010285 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010288 return( ret );
10289 }
10290 }
Paul Bakker887bd502011-06-08 13:10:54 +000010291 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010292 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010293 /*
10294 * The user is trying to send a message the first time, so we need to
10295 * copy the data into the internal buffers and setup the data structure
10296 * to keep track of partial writes
10297 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010298 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010299 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010300 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010301
Hanno Becker67bc7c32018-08-06 11:33:50 +010010302 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010305 return( ret );
10306 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010307 }
10308
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010309 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010310}
10311
10312/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010313 * Write application data, doing 1/n-1 splitting if necessary.
10314 *
10315 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010316 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010317 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010320static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010321 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010322{
10323 int ret;
10324
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010325 if( ssl->conf->cbc_record_splitting ==
10326 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010327 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10329 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10330 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010331 {
10332 return( ssl_write_real( ssl, buf, len ) );
10333 }
10334
10335 if( ssl->split_done == 0 )
10336 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010337 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010338 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010339 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010340 }
10341
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010342 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10343 return( ret );
10344 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010345
10346 return( ret + 1 );
10347}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010348#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010349
10350/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010351 * Write application data (public-facing wrapper)
10352 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010353int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010354{
10355 int ret;
10356
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010358
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010359 if( ssl == NULL || ssl->conf == NULL )
10360 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10361
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010362#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010363 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10364 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010365 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010366 return( ret );
10367 }
10368#endif
10369
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010370 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010371 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010372 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010373 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010374 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010375 return( ret );
10376 }
10377 }
10378
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010379#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010380 ret = ssl_write_split( ssl, buf, len );
10381#else
10382 ret = ssl_write_real( ssl, buf, len );
10383#endif
10384
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010386
10387 return( ret );
10388}
10389
10390/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010391 * Notify the peer that the connection is being closed
10392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010393int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010394{
10395 int ret;
10396
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010397 if( ssl == NULL || ssl->conf == NULL )
10398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010401
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010402 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010403 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010405 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10408 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10409 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010412 return( ret );
10413 }
10414 }
10415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010417
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010418 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010419}
10420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010421void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010422{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010423 if( transform == NULL )
10424 return;
10425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010426#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010427 deflateEnd( &transform->ctx_deflate );
10428 inflateEnd( &transform->ctx_inflate );
10429#endif
10430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010431 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10432 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010433
Hanno Beckerd56ed242018-01-03 15:32:51 +000010434#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010435 mbedtls_md_free( &transform->md_ctx_enc );
10436 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010437#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010438
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010439 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010440}
10441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010442#if defined(MBEDTLS_X509_CRT_PARSE_C)
10443static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010444{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010445 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010446
10447 while( cur != NULL )
10448 {
10449 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010451 cur = next;
10452 }
10453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010454#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010455
Hanno Becker0271f962018-08-16 13:23:47 +010010456#if defined(MBEDTLS_SSL_PROTO_DTLS)
10457
10458static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10459{
10460 unsigned offset;
10461 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10462
10463 if( hs == NULL )
10464 return;
10465
Hanno Becker283f5ef2018-08-24 09:34:47 +010010466 ssl_free_buffered_record( ssl );
10467
Hanno Becker0271f962018-08-16 13:23:47 +010010468 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010469 ssl_buffering_free_slot( ssl, offset );
10470}
10471
10472static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10473 uint8_t slot )
10474{
10475 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10476 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010477
10478 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10479 return;
10480
Hanno Beckere605b192018-08-21 15:59:07 +010010481 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010482 {
Hanno Beckere605b192018-08-21 15:59:07 +010010483 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010484 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010485 mbedtls_free( hs_buf->data );
10486 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010487 }
10488}
10489
10490#endif /* MBEDTLS_SSL_PROTO_DTLS */
10491
Gilles Peskine9b562d52018-04-25 20:32:43 +020010492void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010493{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010494 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10495
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010496 if( handshake == NULL )
10497 return;
10498
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010499#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10500 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10501 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010502 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010503 handshake->async_in_progress = 0;
10504 }
10505#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10506
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010507#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10508 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10509 mbedtls_md5_free( &handshake->fin_md5 );
10510 mbedtls_sha1_free( &handshake->fin_sha1 );
10511#endif
10512#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10513#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010514#if defined(MBEDTLS_USE_PSA_CRYPTO)
10515 psa_hash_abort( &handshake->fin_sha256_psa );
10516#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010517 mbedtls_sha256_free( &handshake->fin_sha256 );
10518#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010519#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010520#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010521#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010522 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010523#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010524 mbedtls_sha512_free( &handshake->fin_sha512 );
10525#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010526#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010527#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010529#if defined(MBEDTLS_DHM_C)
10530 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010531#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010532#if defined(MBEDTLS_ECDH_C)
10533 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010534#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010535#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010536 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010537#if defined(MBEDTLS_SSL_CLI_C)
10538 mbedtls_free( handshake->ecjpake_cache );
10539 handshake->ecjpake_cache = NULL;
10540 handshake->ecjpake_cache_len = 0;
10541#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010542#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010543
Janos Follath4ae5c292016-02-10 11:27:43 +000010544#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10545 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010546 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010547 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010548#endif
10549
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010550#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10551 if( handshake->psk != NULL )
10552 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010553 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010554 mbedtls_free( handshake->psk );
10555 }
10556#endif
10557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010558#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10559 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010560 /*
10561 * Free only the linked list wrapper, not the keys themselves
10562 * since the belong to the SNI callback
10563 */
10564 if( handshake->sni_key_cert != NULL )
10565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010566 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010567
10568 while( cur != NULL )
10569 {
10570 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010571 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010572 cur = next;
10573 }
10574 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010575#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010576
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010577#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010578 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010579 if( handshake->ecrs_peer_cert != NULL )
10580 {
10581 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10582 mbedtls_free( handshake->ecrs_peer_cert );
10583 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010584#endif
10585
Hanno Becker75173122019-02-06 16:18:31 +000010586#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10587 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10588 mbedtls_pk_free( &handshake->peer_pubkey );
10589#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010591#if defined(MBEDTLS_SSL_PROTO_DTLS)
10592 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010593 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010594 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010595#endif
10596
Hanno Becker4a63ed42019-01-08 11:39:35 +000010597#if defined(MBEDTLS_ECDH_C) && \
10598 defined(MBEDTLS_USE_PSA_CRYPTO)
10599 psa_destroy_key( handshake->ecdh_psa_privkey );
10600#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10601
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010602 mbedtls_platform_zeroize( handshake,
10603 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010604}
10605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010606void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010607{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010608 if( session == NULL )
10609 return;
10610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010611#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010612 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010613#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010614
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010615#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010616 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010617#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010618
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010619 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010620}
10621
Paul Bakker5121ce52009-01-03 21:22:43 +000010622/*
10623 * Free an SSL context
10624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010625void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010626{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010627 if( ssl == NULL )
10628 return;
10629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010631
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010632 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010633 {
Angus Grattond8213d02016-05-25 20:56:48 +100010634 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010636 }
10637
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010638 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010639 {
Angus Grattond8213d02016-05-25 20:56:48 +100010640 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010641 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010642 }
10643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010645 if( ssl->compress_buf != NULL )
10646 {
Angus Grattond8213d02016-05-25 20:56:48 +100010647 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010648 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010649 }
10650#endif
10651
Paul Bakker48916f92012-09-16 19:57:18 +000010652 if( ssl->transform )
10653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654 mbedtls_ssl_transform_free( ssl->transform );
10655 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010656 }
10657
10658 if( ssl->handshake )
10659 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010660 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010661 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10662 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664 mbedtls_free( ssl->handshake );
10665 mbedtls_free( ssl->transform_negotiate );
10666 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010667 }
10668
Paul Bakkerc0463502013-02-14 11:19:38 +010010669 if( ssl->session )
10670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010671 mbedtls_ssl_session_free( ssl->session );
10672 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010673 }
10674
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010675#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010676 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010677 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010678 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010680 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010681#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10684 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10687 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010688 }
10689#endif
10690
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010691#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010692 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010693#endif
10694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010696
Paul Bakker86f04f42013-02-14 11:20:09 +010010697 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010698 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010699}
10700
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010701/*
10702 * Initialze mbedtls_ssl_config
10703 */
10704void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10705{
10706 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10707}
10708
Simon Butcherc97b6972015-12-27 23:48:17 +000010709#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010710static int ssl_preset_default_hashes[] = {
10711#if defined(MBEDTLS_SHA512_C)
10712 MBEDTLS_MD_SHA512,
10713 MBEDTLS_MD_SHA384,
10714#endif
10715#if defined(MBEDTLS_SHA256_C)
10716 MBEDTLS_MD_SHA256,
10717 MBEDTLS_MD_SHA224,
10718#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010719#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010720 MBEDTLS_MD_SHA1,
10721#endif
10722 MBEDTLS_MD_NONE
10723};
Simon Butcherc97b6972015-12-27 23:48:17 +000010724#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010725
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010726static int ssl_preset_suiteb_ciphersuites[] = {
10727 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10728 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10729 0
10730};
10731
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010732#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010733static int ssl_preset_suiteb_hashes[] = {
10734 MBEDTLS_MD_SHA256,
10735 MBEDTLS_MD_SHA384,
10736 MBEDTLS_MD_NONE
10737};
10738#endif
10739
10740#if defined(MBEDTLS_ECP_C)
10741static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010742#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010743 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010744#endif
10745#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010746 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010747#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010748 MBEDTLS_ECP_DP_NONE
10749};
10750#endif
10751
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010752/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010753 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010754 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010755int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010756 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010757{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010758#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010759 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010760#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010761
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010762 /* Use the functions here so that they are covered in tests,
10763 * but otherwise access member directly for efficiency */
10764 mbedtls_ssl_conf_endpoint( conf, endpoint );
10765 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010766
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010767 /*
10768 * Things that are common to all presets
10769 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010770#if defined(MBEDTLS_SSL_CLI_C)
10771 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10772 {
10773 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10774#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10775 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10776#endif
10777 }
10778#endif
10779
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010780#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010781 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010782#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010783
10784#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10785 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10786#endif
10787
10788#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10789 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10790#endif
10791
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010792#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10793 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10794#endif
10795
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010796#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010797 conf->f_cookie_write = ssl_cookie_write_dummy;
10798 conf->f_cookie_check = ssl_cookie_check_dummy;
10799#endif
10800
10801#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10802 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10803#endif
10804
Janos Follath088ce432017-04-10 12:42:31 +010010805#if defined(MBEDTLS_SSL_SRV_C)
10806 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10807#endif
10808
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010809#if defined(MBEDTLS_SSL_PROTO_DTLS)
10810 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10811 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10812#endif
10813
10814#if defined(MBEDTLS_SSL_RENEGOTIATION)
10815 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010816 memset( conf->renego_period, 0x00, 2 );
10817 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010818#endif
10819
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010820#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10821 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10822 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010823 const unsigned char dhm_p[] =
10824 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10825 const unsigned char dhm_g[] =
10826 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10827
Hanno Beckera90658f2017-10-04 15:29:08 +010010828 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10829 dhm_p, sizeof( dhm_p ),
10830 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010831 {
10832 return( ret );
10833 }
10834 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010835#endif
10836
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010837 /*
10838 * Preset-specific defaults
10839 */
10840 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010841 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010842 /*
10843 * NSA Suite B
10844 */
10845 case MBEDTLS_SSL_PRESET_SUITEB:
10846 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10847 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10848 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10849 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10850
10851 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10852 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10853 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10854 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10855 ssl_preset_suiteb_ciphersuites;
10856
10857#if defined(MBEDTLS_X509_CRT_PARSE_C)
10858 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010859#endif
10860
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010861#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010862 conf->sig_hashes = ssl_preset_suiteb_hashes;
10863#endif
10864
10865#if defined(MBEDTLS_ECP_C)
10866 conf->curve_list = ssl_preset_suiteb_curves;
10867#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010868 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010869
10870 /*
10871 * Default
10872 */
10873 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010874 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10875 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10876 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10877 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10878 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10879 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10880 MBEDTLS_SSL_MIN_MINOR_VERSION :
10881 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010882 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10883 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10884
10885#if defined(MBEDTLS_SSL_PROTO_DTLS)
10886 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10887 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10888#endif
10889
10890 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10891 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10892 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10893 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10894 mbedtls_ssl_list_ciphersuites();
10895
10896#if defined(MBEDTLS_X509_CRT_PARSE_C)
10897 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10898#endif
10899
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010900#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010901 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010902#endif
10903
10904#if defined(MBEDTLS_ECP_C)
10905 conf->curve_list = mbedtls_ecp_grp_id_list();
10906#endif
10907
10908#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10909 conf->dhm_min_bitlen = 1024;
10910#endif
10911 }
10912
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010913 return( 0 );
10914}
10915
10916/*
10917 * Free mbedtls_ssl_config
10918 */
10919void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10920{
10921#if defined(MBEDTLS_DHM_C)
10922 mbedtls_mpi_free( &conf->dhm_P );
10923 mbedtls_mpi_free( &conf->dhm_G );
10924#endif
10925
10926#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10927 if( conf->psk != NULL )
10928 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010929 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010930 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010931 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010932 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010933 }
10934
10935 if( conf->psk_identity != NULL )
10936 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010937 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010938 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010939 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010940 conf->psk_identity_len = 0;
10941 }
10942#endif
10943
10944#if defined(MBEDTLS_X509_CRT_PARSE_C)
10945 ssl_key_cert_free( conf->key_cert );
10946#endif
10947
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010948 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010949}
10950
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010951#if defined(MBEDTLS_PK_C) && \
10952 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010953/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010954 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010956unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010957{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010958#if defined(MBEDTLS_RSA_C)
10959 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10960 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010962#if defined(MBEDTLS_ECDSA_C)
10963 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10964 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010966 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010967}
10968
Hanno Becker7e5437a2017-04-28 17:15:26 +010010969unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10970{
10971 switch( type ) {
10972 case MBEDTLS_PK_RSA:
10973 return( MBEDTLS_SSL_SIG_RSA );
10974 case MBEDTLS_PK_ECDSA:
10975 case MBEDTLS_PK_ECKEY:
10976 return( MBEDTLS_SSL_SIG_ECDSA );
10977 default:
10978 return( MBEDTLS_SSL_SIG_ANON );
10979 }
10980}
10981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010982mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010983{
10984 switch( sig )
10985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010986#if defined(MBEDTLS_RSA_C)
10987 case MBEDTLS_SSL_SIG_RSA:
10988 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010989#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010990#if defined(MBEDTLS_ECDSA_C)
10991 case MBEDTLS_SSL_SIG_ECDSA:
10992 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010993#endif
10994 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010995 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010996 }
10997}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010998#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010999
Hanno Becker7e5437a2017-04-28 17:15:26 +010011000#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11001 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11002
11003/* Find an entry in a signature-hash set matching a given hash algorithm. */
11004mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11005 mbedtls_pk_type_t sig_alg )
11006{
11007 switch( sig_alg )
11008 {
11009 case MBEDTLS_PK_RSA:
11010 return( set->rsa );
11011 case MBEDTLS_PK_ECDSA:
11012 return( set->ecdsa );
11013 default:
11014 return( MBEDTLS_MD_NONE );
11015 }
11016}
11017
11018/* Add a signature-hash-pair to a signature-hash set */
11019void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11020 mbedtls_pk_type_t sig_alg,
11021 mbedtls_md_type_t md_alg )
11022{
11023 switch( sig_alg )
11024 {
11025 case MBEDTLS_PK_RSA:
11026 if( set->rsa == MBEDTLS_MD_NONE )
11027 set->rsa = md_alg;
11028 break;
11029
11030 case MBEDTLS_PK_ECDSA:
11031 if( set->ecdsa == MBEDTLS_MD_NONE )
11032 set->ecdsa = md_alg;
11033 break;
11034
11035 default:
11036 break;
11037 }
11038}
11039
11040/* Allow exactly one hash algorithm for each signature. */
11041void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11042 mbedtls_md_type_t md_alg )
11043{
11044 set->rsa = md_alg;
11045 set->ecdsa = md_alg;
11046}
11047
11048#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11049 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11050
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011051/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011052 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011054mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011055{
11056 switch( hash )
11057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011058#if defined(MBEDTLS_MD5_C)
11059 case MBEDTLS_SSL_HASH_MD5:
11060 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011062#if defined(MBEDTLS_SHA1_C)
11063 case MBEDTLS_SSL_HASH_SHA1:
11064 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011066#if defined(MBEDTLS_SHA256_C)
11067 case MBEDTLS_SSL_HASH_SHA224:
11068 return( MBEDTLS_MD_SHA224 );
11069 case MBEDTLS_SSL_HASH_SHA256:
11070 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072#if defined(MBEDTLS_SHA512_C)
11073 case MBEDTLS_SSL_HASH_SHA384:
11074 return( MBEDTLS_MD_SHA384 );
11075 case MBEDTLS_SSL_HASH_SHA512:
11076 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011077#endif
11078 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011079 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011080 }
11081}
11082
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011083/*
11084 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11085 */
11086unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11087{
11088 switch( md )
11089 {
11090#if defined(MBEDTLS_MD5_C)
11091 case MBEDTLS_MD_MD5:
11092 return( MBEDTLS_SSL_HASH_MD5 );
11093#endif
11094#if defined(MBEDTLS_SHA1_C)
11095 case MBEDTLS_MD_SHA1:
11096 return( MBEDTLS_SSL_HASH_SHA1 );
11097#endif
11098#if defined(MBEDTLS_SHA256_C)
11099 case MBEDTLS_MD_SHA224:
11100 return( MBEDTLS_SSL_HASH_SHA224 );
11101 case MBEDTLS_MD_SHA256:
11102 return( MBEDTLS_SSL_HASH_SHA256 );
11103#endif
11104#if defined(MBEDTLS_SHA512_C)
11105 case MBEDTLS_MD_SHA384:
11106 return( MBEDTLS_SSL_HASH_SHA384 );
11107 case MBEDTLS_MD_SHA512:
11108 return( MBEDTLS_SSL_HASH_SHA512 );
11109#endif
11110 default:
11111 return( MBEDTLS_SSL_HASH_NONE );
11112 }
11113}
11114
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011115#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011116/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011117 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011118 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011119 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011120int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011122 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011123
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011124 if( ssl->conf->curve_list == NULL )
11125 return( -1 );
11126
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011127 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011128 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011129 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011130
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011131 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011132}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011133#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011134
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011135#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011136/*
11137 * Check if a hash proposed by the peer is in our list.
11138 * Return 0 if we're willing to use it, -1 otherwise.
11139 */
11140int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11141 mbedtls_md_type_t md )
11142{
11143 const int *cur;
11144
11145 if( ssl->conf->sig_hashes == NULL )
11146 return( -1 );
11147
11148 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11149 if( *cur == (int) md )
11150 return( 0 );
11151
11152 return( -1 );
11153}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011154#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011156#if defined(MBEDTLS_X509_CRT_PARSE_C)
11157int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11158 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011159 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011160 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011161{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011162 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011163#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011164 int usage = 0;
11165#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011166#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011167 const char *ext_oid;
11168 size_t ext_len;
11169#endif
11170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011171#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11172 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011173 ((void) cert);
11174 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011175 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011176#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011178#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11179 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011180 {
11181 /* Server part of the key exchange */
11182 switch( ciphersuite->key_exchange )
11183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011184 case MBEDTLS_KEY_EXCHANGE_RSA:
11185 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011186 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011187 break;
11188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011189 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11190 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11191 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11192 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011193 break;
11194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011195 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11196 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011197 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011198 break;
11199
11200 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011201 case MBEDTLS_KEY_EXCHANGE_NONE:
11202 case MBEDTLS_KEY_EXCHANGE_PSK:
11203 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11204 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011205 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011206 usage = 0;
11207 }
11208 }
11209 else
11210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011211 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11212 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011213 }
11214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011215 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011216 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011217 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011218 ret = -1;
11219 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011220#else
11221 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011222#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011224#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11225 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011227 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11228 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011229 }
11230 else
11231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011232 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11233 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011234 }
11235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011236 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011237 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011238 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011239 ret = -1;
11240 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011241#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011242
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011243 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011244}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011245#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011246
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011247/*
11248 * Convert version numbers to/from wire format
11249 * and, for DTLS, to/from TLS equivalent.
11250 *
11251 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011252 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011253 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11254 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011256void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011257 unsigned char ver[2] )
11258{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011259#if defined(MBEDTLS_SSL_PROTO_DTLS)
11260 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011262 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011263 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11264
11265 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11266 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11267 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011268 else
11269#else
11270 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011271#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011272 {
11273 ver[0] = (unsigned char) major;
11274 ver[1] = (unsigned char) minor;
11275 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011276}
11277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011278void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011279 const unsigned char ver[2] )
11280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011281#if defined(MBEDTLS_SSL_PROTO_DTLS)
11282 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011283 {
11284 *major = 255 - ver[0] + 2;
11285 *minor = 255 - ver[1] + 1;
11286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011287 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011288 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11289 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011290 else
11291#else
11292 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011293#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011294 {
11295 *major = ver[0];
11296 *minor = ver[1];
11297 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011298}
11299
Simon Butcher99000142016-10-13 17:21:01 +010011300int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11301{
11302#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11303 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11304 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11305
11306 switch( md )
11307 {
11308#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11309#if defined(MBEDTLS_MD5_C)
11310 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011311 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011312#endif
11313#if defined(MBEDTLS_SHA1_C)
11314 case MBEDTLS_SSL_HASH_SHA1:
11315 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11316 break;
11317#endif
11318#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11319#if defined(MBEDTLS_SHA512_C)
11320 case MBEDTLS_SSL_HASH_SHA384:
11321 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11322 break;
11323#endif
11324#if defined(MBEDTLS_SHA256_C)
11325 case MBEDTLS_SSL_HASH_SHA256:
11326 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11327 break;
11328#endif
11329 default:
11330 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11331 }
11332
11333 return 0;
11334#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11335 (void) ssl;
11336 (void) md;
11337
11338 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11339#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11340}
11341
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011342#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11343 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11344int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11345 unsigned char *output,
11346 unsigned char *data, size_t data_len )
11347{
11348 int ret = 0;
11349 mbedtls_md5_context mbedtls_md5;
11350 mbedtls_sha1_context mbedtls_sha1;
11351
11352 mbedtls_md5_init( &mbedtls_md5 );
11353 mbedtls_sha1_init( &mbedtls_sha1 );
11354
11355 /*
11356 * digitally-signed struct {
11357 * opaque md5_hash[16];
11358 * opaque sha_hash[20];
11359 * };
11360 *
11361 * md5_hash
11362 * MD5(ClientHello.random + ServerHello.random
11363 * + ServerParams);
11364 * sha_hash
11365 * SHA(ClientHello.random + ServerHello.random
11366 * + ServerParams);
11367 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011368 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011369 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011371 goto exit;
11372 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011373 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011374 ssl->handshake->randbytes, 64 ) ) != 0 )
11375 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011376 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011377 goto exit;
11378 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011379 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011380 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011382 goto exit;
11383 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011384 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011385 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011387 goto exit;
11388 }
11389
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011390 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011391 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011392 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011393 goto exit;
11394 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011395 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011396 ssl->handshake->randbytes, 64 ) ) != 0 )
11397 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011398 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011399 goto exit;
11400 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011401 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011402 data_len ) ) != 0 )
11403 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011405 goto exit;
11406 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011407 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011408 output + 16 ) ) != 0 )
11409 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011411 goto exit;
11412 }
11413
11414exit:
11415 mbedtls_md5_free( &mbedtls_md5 );
11416 mbedtls_sha1_free( &mbedtls_sha1 );
11417
11418 if( ret != 0 )
11419 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11420 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11421
11422 return( ret );
11423
11424}
11425#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11426 MBEDTLS_SSL_PROTO_TLS1_1 */
11427
11428#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11429 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011430
11431#if defined(MBEDTLS_USE_PSA_CRYPTO)
11432int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11433 unsigned char *hash, size_t *hashlen,
11434 unsigned char *data, size_t data_len,
11435 mbedtls_md_type_t md_alg )
11436{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011437 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011438 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011439 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11440
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011442
11443 if( ( status = psa_hash_setup( &hash_operation,
11444 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011445 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011446 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011447 goto exit;
11448 }
11449
Andrzej Kurek814feff2019-01-14 04:35:19 -050011450 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11451 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011452 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011453 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011454 goto exit;
11455 }
11456
Andrzej Kurek814feff2019-01-14 04:35:19 -050011457 if( ( status = psa_hash_update( &hash_operation,
11458 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011459 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011460 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011461 goto exit;
11462 }
11463
Andrzej Kurek814feff2019-01-14 04:35:19 -050011464 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11465 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011466 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011467 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011468 goto exit;
11469 }
11470
11471exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011472 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011473 {
11474 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11475 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011476 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011477 {
11478 case PSA_ERROR_NOT_SUPPORTED:
11479 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011480 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011481 case PSA_ERROR_BUFFER_TOO_SMALL:
11482 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11483 case PSA_ERROR_INSUFFICIENT_MEMORY:
11484 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11485 default:
11486 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11487 }
11488 }
11489 return( 0 );
11490}
11491
11492#else
11493
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011494int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011495 unsigned char *hash, size_t *hashlen,
11496 unsigned char *data, size_t data_len,
11497 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011498{
11499 int ret = 0;
11500 mbedtls_md_context_t ctx;
11501 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011502 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011503
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011505
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011506 mbedtls_md_init( &ctx );
11507
11508 /*
11509 * digitally-signed struct {
11510 * opaque client_random[32];
11511 * opaque server_random[32];
11512 * ServerDHParams params;
11513 * };
11514 */
11515 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11516 {
11517 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11518 goto exit;
11519 }
11520 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11521 {
11522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11523 goto exit;
11524 }
11525 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11526 {
11527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11528 goto exit;
11529 }
11530 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11531 {
11532 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11533 goto exit;
11534 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011535 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011536 {
11537 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11538 goto exit;
11539 }
11540
11541exit:
11542 mbedtls_md_free( &ctx );
11543
11544 if( ret != 0 )
11545 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11546 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11547
11548 return( ret );
11549}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011550#endif /* MBEDTLS_USE_PSA_CRYPTO */
11551
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011552#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11553 MBEDTLS_SSL_PROTO_TLS1_2 */
11554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011555#endif /* MBEDTLS_SSL_TLS_C */